Category Archives: Linux Utils

Git Tips and Tricks

!!! WORK IN PROGRESS !!!

Some useful commands

Setup git to use vscode has the default editor

Run this command and then add this lines:

$ git config --global -e
[core]
	editor = code --wait

[diff]
	tool = default-difftool

[difftool "default-difftool"]
  cmd = code --wait --diff $LOCAL $REMOTE

[difftool]
	prompt = false

[merge]
	tool = code

[merge "tool"]
	cmd = "code --wait $MERGED"
	prompt = false
	keepbackup = false

How to compare (diff) files from two different branches

git difftool mybranch anotherbranch -- myfile.txt

or

git diff branch1:file branch2:file

commit –amend

Edit a commit before push it, example:

$ touch test.txt
$ git add test.txt
$ git commit -m "Test file"
[master 964fa35] Test file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
$ git log --oneline
964fa35 (HEAD -> master)Test file
$ touch test2.txt
$ git add test2.txt 
$ git commit --amend -m "Test and Test2 file"
$ git log --oneline
414a06e (HEAD -> master) test and test2

git log

git log shows the current HEAD and its ancestry. That is, it prints the commit HEAD points to, then its parent, its parent, and so on.

$ git log --oneline
$ git log HEAD@{1.hour.ago}
$ git log HEAD@{1.week.ago}

git reflog

git reflog doesn’t traverse HEAD’s ancestry at all. The reflog is an ordered list of the commits that HEAD has pointed to: it’s undo history for your repo. The reflog isn’t part of the repo itself (it’s stored separately to the commits themselves) and isn’t included in pushes, fetches or clones; it’s purely local.

$ git reflog HEAD@{1.hour.ago} --oneline

How to clear unreachable commits

$ git reflog expire --expire-unreachable=now --all
$ git gc --prune=now

Find how much memory apache threaded MPM is using

The Worker MPM turns Apache into a multi-process, multi-threaded web server. Unlike Prefork, each child process under Worker can have multiple threads.

If you use top or ps you will get something like this:
top
And sometimes is hard to find how many resources this service is using.

If you want to have the memory usage grouped by process, we could use the atop tool and then press ‘m’ (memory usage) and ‘p’ (group by process name). Example:

atop

This will work with any multi-process, multi-threaded service 🙂

Correct Terminal prompt wrapping

Sometimes I get a wrong wrapping when using terminal via ssh and I find out is mostly to do with the size of the window assumed by the terminal is not the same as your actual window size.
E.g.:
Wrong Wrapping

To solve this:

$ shopt checkwinsize
And check if the checkwinsize is eanbled, if not:

$ shopt -s checkwinsize
Then just run another command or resize your window size.

You could put this command on your ~/.bashrc or /etc/bashrc

How to set up automatic filesystem checks and repair on Linux

Trigger Automatic Filesystem Check upon Boot
If you want to trigger fsck automatically upon boot, there are distro-specific ways to set up unattended fschk during boot time.

On Debian, Ubuntu or Linux Mint, edit /etc/default/rcS as follows.

$ sudo vi /etc/default/rcS
# automatically repair filesystems with inconsistencies during boot
FSCKFIX=yes
On CentOS, edit /etc/sysconfig/autofsck (or create it if it doesn’t exist) with the following content.

$ sudo vi /etc/sysconfig/autofsck
AUTOFSCK_DEF_CHECK=yes

Force One-Time Filesystem Check on the Next Reboot
If you want to trigger one-time filesystem check on your next reboot, you can use this command.

$ sudo touch /forcefsck

File Handles and Open Files

Show Kernel Limits

 sysctl fs.file-max

To determine the current usage of file handles

cat /proc/sys/fs/file-nr
1154    133     8192

The file-nr file displays three parameters:

  • the total allocated file handles.
  • the number of currently used file handles (with the 2.4 kernel); or the number of currently unused file handles (with the 2.6 kernel).
  • the maximum file handles that can be allocated (also found in /proc/sys/fs/file-max).

 

Introduction to lsof

# lsof | more
COMMAND     PID   TID                            USER   FD      TYPE             DEVICE   SIZE/OFF       NODE NAME
systemd       1                                  root  cwd       DIR              254,0       4096          2 /
systemd       1                                  root  rtd       DIR              254,0       4096          2 /
systemd       1                                  root  txt       REG              254,0    1309064   15205040 /lib/systemd/systemd
systemd       1                                  root  mem       REG              254,0      18640   15204380 /lib/x86_64-linux-gnu/libattr.so.1.1.0
systemd       1                                  root  mem       REG              254,0      14664   15204731 /lib/x86_64-linux-gnu/libdl-2.19.so
systemd       1                                  root  mem       REG              254,0     448440   15204402 /lib/x86_64-linux-gnu/libpcre.so.3.13.1
systemd       1                                  root  mem       REG              254,0      31784   15204765 /lib/x86_64-linux-gnu/librt-2.19.so
systemd       1                                  root  mem       REG              254,0      92888   15204453 /lib/x86_64-linux-gnu/libkmod.so.2.2.8
systemd       1                                  root  mem       REG              254,0      19016   15204533 /lib/x86_64-linux-gnu/libcap.so.2.24
systemd       1                                  root  mem       REG              254,0     113024   15204445 /lib/x86_64-linux-gnu/libaudit.so.1.0.0
systemd       1                                  root  mem       REG              254,0      64024   15204422 /lib/x86_64-linux-gnu/libpam.so.0.83.1
systemd       1                                  root  mem       REG              254,0     142728   15204457 /lib/x86_64-linux-gnu/libselinux.so.1
systemd       1                                  root  mem       REG              254,0    1738176   15204667 /lib/x86_64-linux-gnu/libc-2.19.so

By default One file per line is displayed. Most of the columns are self explanatory. We will explain the details about couple of cryptic columns (FD and TYPE).

FD – Represents the file descriptor. Some of the values of FDs are,

  • cwd – Current Working Directory
  • txt – Text file
  • mem – Memory mapped file
  • mmap – Memory mapped device
  • NUMBER – Represent the actual file descriptor. The character after the number i.e ‘1u’, represents the mode in which the file is opened. r for read, w for write, u for read and write.

TYPE – Specifies the type of the file. Some of the values of TYPEs are:
REG – Regular File
DIR – Directory
FIFO – First In First Out
CHR – Character special file

 

Finding Process using heavy disk io: 
http://sys.cuquejo.org/finding-process-heavy-disk-io/

ss – socket statistics – translated for netstat users

ss –  translated for netstat users

Since netstat version 1.4, man page says that netstat is obsolete, so We should use ‘ss’, but how?
I put here some tips here:

Show listening connections with process name and PID

sudo ss -ltp

Show all connections with process name and PID of each open connection

sudo ss -tp

Filter out tcp, udp

ss -A tcp
OR
ss -A udp

Do not resolve hostname

ss -nt

Show only listening sockets

ss -ltn # For TCP
or
ss -lun # For UDP

Filtering connections by tcp state

# ss [ OPTIONS ] [ STATE-FILTER ] [ ADDRESS-FILTER ] #
ss -t4 state established
ss -t4 state time-wait

The state can be either of the following

  1. established
  2. syn-sent
  3. syn-recv
  4. fin-wait-1
  5. fin-wait-2
  6. time-wait
  7. closed
  8. close-wait
  9. last-ack
  10. closing
  11. all : All of the above states
  12. connected : All the states except for listen and closed
  13. synchronized : All the connected states except for syn-sent
  14. bucket : Show states, which are maintained as minisockets, i.e. time-wait and syn-recv.
  15. big : Opposite to bucket state.