Tags: "bash", "cli", "terminal", access_time 4-min read

Edit this post on Github

Super Useful Bash Commands

Created: August 12, 2019 by [lek-tin]

Last updated: September 16, 2019

  1. List all services
sudo service --status-all
  1. Create a nested directory if it doesn’t exist already.
mkdir -p /nested/directory
  1. To list detailed information about all items (visible and invisible) in the current directory (the -a option is to show all files, the -l option is to show details)
ls -al
  1. To list the contents of your Documents folder and all sub folders (this may take a while if you have a large Documents folder)
ls -R ~/Documents
  1. Find process which is listening to port 3000, and kill that process or process group
sudo lsof -i :3000
kill -9 <PID>
kill -9 -<PID>
  1. [Dangerous] Recursively delete EVERYTHING files
rm -rf /tmp/*   // everything in '/tmp/' folder
rm -rf ./*      // everything in current folder
  1. Delete curtain files
// Your exact case would be handled by brace expansion, like so:
rm -rf abc.log.2012-03-{14,27,28}
rm -rf abc.log.2012-03-14 abc.log.2012-03-27 abc.log.2012-03-28
  1. Search
# search text or searches the given file for lines containing a match to the given strings or words.
docker logs | grep xxx
grep xxx
# Search for files that contains a specific text
find PATH -type f | xargs grep 'youtube.com'
# Recursively search for a text in files
grep -Ril "text-to-find" /
# i stands for ignore case (optional in your case).
# R stands for recursive.
# l stands for "show the file name, not the result itself".
# / stands for starting at the root of your machine.
  1. Limit output
# Display only the first 10 lines or the last 20 lines
ll | head -n 10 | tail -n 20
  1. Check memory usage
free
# total used free shared buffers cached
# Mem: 8027952 4377300 3650652 0 103648 1630364
# -/+ buffers/cache: 2643288 5384664
# Swap: 15624188 608948 15015240
  1. Count total number of lines of code of all the php files in a directory recursively
find . -name '*.php' | xargs wc -l
  1. List ports in use
netstat -a
  1. Find a file by name
find . -name 'filename*'
  1. Append content to a file.
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
  1. Change permission
# owner can read and write
chmod 600 file
# owner can read, write and execute
chmod 700 file
# all can read and write
chmod 666 file
# all can read, write and execute
chmod 777 file
  1. Locate executable
    which command is very small and simple command to locate executables in the system. It allows user to pass several command names as arguments to get their paths in the system.
$ which [-option]
$ which ls gdb open grep
/bin/ls
/usr/bin/gdb
/bin/open
/bin/grep
  1. Alias
alias ll='ls -al'
  1. Upload files to the server
# Secure copy
scp FILE USER@SERVER_IP:.
  1. Extract bz2 files
# flag -k means keeping the source file
bzip2 -dk AREAWATER.csv.bz2
  1. show CPU architecture-related information
lscpu
  1. install a package
apt-get install <package-name>
  1. list the installed packages
apt list --installed
  1. List kernel attributes
sysctl -a
  1. Passing parameters to awk
echo 'Hello world' | awk '{ print $0 }'

Output:

Hello world
echo 3 4 | awk '{ print $1 + $2 }'

Output:

7
  1. Kill processes listening at PORT_NUMBER
lsof -i :<PORT_NUMBER> | awk -v i=2 -v j=2 'FNR==i {system("kill -9 " $2)}'
sudo kill -9 `sudo lsof -t -i:9001`
# If that doesn't work you could also use $() for command interpolation:
sudo kill -9 $(sudo lsof -t -i:9001)
  1. awk Print the jth field of the ith line
awk -v i=5 -v j=3 'FNR == i {print $j}'
  1. Embed sub scripts in main script. Sub script will be evaluated/executed before the main script.
# List current user directory
ls /Users/`echo $USER`
  1. To check whether a program is running
ps -ef | grep tomcat
  1. check user manual for a command. man command in Linux is used to display the user manual of any command that we can run on the terminal. It provides a detailed view of the command which includes

  2. Search for occurrences of a target value against all files in a directory

find . -name '*.js' -exec grep -i 'string to search for' {} \; -print