Super Useful Bash Commands
Created: August 12, 2019 by [lek-tin]
Last updated: September 16, 2019
- List all services
sudo service --status-all
- Create a nested directory if it doesn’t exist already.
mkdir -p /nested/directory
- 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
- 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
- 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>
- [Dangerous] Recursively delete EVERYTHING files
rm -rf /tmp/* // everything in '/tmp/' folder
rm -rf ./* // everything in current folder
- 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
- 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.
- Limit output
# Display only the first 10 lines or the last 20 lines
ll | head -n 10 | tail -n 20
- 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
- Count total number of lines of code of all the php files in a directory recursively
find . -name '*.php' | xargs wc -l
- List ports in use
netstat -a
- Find a file by name
find . -name 'filename*'
- Append content to a file.
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
- 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
- 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
- Alias
alias ll='ls -al'
- Upload files to the server
# Secure copy
scp FILE USER@SERVER_IP:.
- Extract
bz2
files
# flag -k means keeping the source file
bzip2 -dk AREAWATER.csv.bz2
- show CPU architecture-related information
lscpu
- install a package
apt-get install <package-name>
- list the installed packages
apt list --installed
- List kernel attributes
sysctl -a
- Passing parameters to
awk
echo 'Hello world' | awk '{ print $0 }'
Output:
Hello world
echo 3 4 | awk '{ print $1 + $2 }'
Output:
7
- 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)
awk
Print thej
th field of thei
th line
awk -v i=5 -v j=3 'FNR == i {print $j}'
- Embed sub scripts in main script. Sub script will be evaluated/executed before the main script.
# List current user directory
ls /Users/`echo $USER`
- To check whether a program is running
ps -ef | grep tomcat
-
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 -
Search for occurrences of a target value against all files in a directory
find . -name '*.js' -exec grep -i 'string to search for' {} \; -print