Basics

ls - listing of files

# Usage:
# --color = enabling coloured output
# -l = long listing
# -h = human readable Listing of data
# -t = sort by time
# -R = Recursive listing of directories

# Must use aliases - `.bash_aliases`
alias ls='ls -A -F -B --human --color'
alias ll='ls -l'
alias la='ls -A'
alias lh='ls -h'

ls -lrt

less - terminal pager

# Usage:
# -N = Line number enabled
# +F = Real Time Monitoring

sudo dmesg | less +F
cat test.txt | less
# OS and packages
apt install less  # debian/ubuntu
yum install less # centos/fedora(dnf, microdnf, rpm, yum)
pacman -S less # arch(pacman)
apk add less # alpine
brew install less # OS X

head - starting content of the files

# Usage:
# -n = line numbers to be displayed in the head
# -c = bytes to be displayed from the file

head file.txt
head -n 10 file.txt
head -c 150 file.txt

tail - end of the content of files

# Usage:
# -f = real time updates to the files content appended to the end of file.
# -n = no of lines of tail to be displayed

tail file.txt
tail -f file.log

ln - soft/hard link

# Usage:
# -s = softlink/symlink

ln sourceFileForHardLink hardLinkFileNameWithPath
ln -s sourceFileForHardLink softLinkFileNameWithPath

chmod - change file permissions

  • User-Group-Others(— — —)
  • Read-Write-Execute(rwx)
# Usage:
# -R = Recursive
# -V = Verbose

chmod +x file.txt
chmod 600 file.txt
chmod u-w file.txt
chmod u+x file.txt

chown - change the ownership of files

# Usage:
# -R = Recursive
# -V = Verbose

chown user:group directory

du - directory usage

# Usage:
# -h = human readable

du -h

df - disk filesystem

# Usage:
# -h = human readable
# -i = inode listing
# --total = total summary
# --output = specify the output strategy

df -h /proc
df -h --output=source,avail,pcent,target

inode - index number

  • Data structures that keeps track of all the files in the filesystem
  • all unique inode in a given filesystem
  • stores metadata(type, size, group, user, permission, access, change & modification time)

find - find the files/directories etc

# Usage:
# -name = file the name
# -iname = file name search using ignorecase
# -type = type of search, d = directory, f = file
#         l = symbolic link, s = socket, b = block special
# -exec = command execute the command in searched files 
# -user = search the file with the specified user
# -size = size of files to be searched

find . -name somefile.txt
find /home -iname somefile.txt
find / -type f -perm 0777 -print -exec chmod 644 {} \;

ps - lists all the processes which are running.

# Usage:
# -A or -e = list all the process running for all users 
# -T = process running with the current terminal(tty)
# -f = full listing for the process
# -u = filter process for a specific user 
# -L = List threads for a process PID
# -G = List process associated with a group
# -C = search process by name as PID is not known
# -o = specifies the format for the columns

ps -ef
ps aux
ps -L <PID>
# OS and packages
apt install procps  # debian/ubuntu
yum install procps  # centos/fedora(dnf, microdnf, rpm, yum)
pacman -S procps  # arch(pacman)
apk add procps # alpine
brew install procps  # OS X

nohup - run process in the background, even when session is logged out, “no hangup”: prevents from receiving SIGHUP signals

# Usage:
nohup ./example.sh > output.log 2>&1 &
nohup python example.py &

disown - delete/remove jobs so that it doesn’t send SIGHUP signal

# Usage:
# -a = removes all the jobs
# -h = marks job so that it DOESN'T send SIGHUP when shell receives a SIGHUP signal
# -r = deletes running job

disown -r
disown -h %2
disown -a

kill - kills the running process

# Usage:
# -l = lists all the SIG for kill
#   1 = HUP - reload a process
#   9 = KILL - Force kill a process
#   15 = TERM - Graceful kill of process 

kill PID_NUMBER
kill -9 PID

fzf - CLI fuzzy finder

rg - ripgrep - recursive grep

pandoc - Convert documents

#Usage
pandoc README.md --from markdown --to docx -o tmp.docx

sort - alphabetical(lowercase < uppercase) / increasing sorting by default

#Usage
# -r = reverse order
# -n = recognise numerical results
# -k = sort based on specific column
# -u = remove duplicate entries
# -c = check if the file is in sorted format
# -M = arrange based on months
# -s = stable sort

sort -nr numeric.txt
ls -l | sort -nk 5
sort -c test.txt

uniq - remove duplicate entries, must be in sorted format

#Usage
# -c = count freq 
# -d = print only the lines which are duplicated 
# -u = print all unique lines
# -i = check the frequency after ignoring the case

sort file2 | uniq -c
sort file2 | uniq -u
sort file2 | uniq -cd

cut - cut a field out

# Usage:
# -f = field to cut
# -d = delimiter to use for cutting 

cut -d: -f1,6 /etc/passwd
ls -lrt | cut -d ' ' -f 1

wc - word count

# Usage:
# -l = no of lines
# -w = no of words
# -m = no of characters

wc -l

tr - translate character

# Usage:
# -C = Complement character
# -d = delete character
tr A-Z a-z
tr -d 'e'

sed - substitution

sed s/Amazing/super/gi textfile.txt
sed s/originalword/replaceword/g filename
sed -n '3,5p' textfile.txt

rsync - remote sync

# Usage
# -v = verbose
# -r = recursive
# -z = compress during transfer
# -h = human-redable
# -P or --progress = shows real-time progress

rsync -avzhe ssh --progress /root/pkgs root@127.0.0.1:/root/pkgs
rsync -zvh backup.tar.gz /tmp/backups/
# OS and packages
apt install rsync  # debian/ubuntu
yum install rsync  # centos/fedora(dnf, microdnf, rpm, yum)
pacman -S rsync  # arch(pacman)
apk add rsync # alpine
brew install rsync  # OS X