This cheatsheet provides quick examples and explanations for essential Linux commands and bash scripting constructs with command output simulations for better understanding.
📁 File & Directory Commands
ls
– List files
$ ls
file1.txt folder1 script.sh
ls -ltr
– Long format, sorted by modification time (reverse)
$ ls -ltr
-rw-r--r-- 1 user user 0 Apr 14 10:01 file1.txt
drwxr-xr-x 2 user user 4096 Apr 14 10:02 folder1
mkdir folder_name
– Create a new directory
$ mkdir myfolder
touch file_name
– Create a new empty file
$ touch myfile.txt
cat file_name
– Read and display contents of a file
$ cat myfile.txt
Hello, World!
man command
– Show manual for any command
$ man ls
# Opens the manual for 'ls'
vim filename
/ vi filename
– Open file in Vim editor
$ vim myfile.txt
Vim Modes
-
ESC + i
– Enter insert mode -
ESC + :wq!
– Save and quit forcefully
pwd
– Print current working directory
$ pwd
/home/user/projects
cd folder_name
– Change directory
$ cd myfolder
🔒 File Permissions
chmod
– Change file access permissions
Permissions are represented as:
-
4
– Read -
2
– Write -
1
– Execute
To give all access to everyone:
$ chmod 777 myfile.txt
This gives read, write, and execute permissions to:
- Owner
- Group
- Others
🗑 Delete
rm -rf folder_name
– Force delete a folder recursively
$ rm -rf temp_folder
🧠 System Info
nproc
– Show number of CPU cores
$ nproc
8
free
– Show memory usage
$ free -h
total used free
Mem: 15Gi 4Gi 11Gi
top
– Live system monitoring
$ top
# Shows CPU, memory usage and running processes in real time
df -h
– Show disk usage in human-readable format
$ df -h
Filesystem Size Used Avail Use%
/dev/sda1 100G 50G 50G 50%
⚙️ Process Management
ps -ef
– Show all running processes
$ ps -ef
UID PID PPID CMD
root 1 0 /sbin/init
ps -ef | grep "amazon"
– Filter processes with 'amazon'
$ ps -ef | grep "amazon"
user 2345 1234 /usr/bin/amazon
|
– Pipe output from one command to another
Used to filter or chain commands
grep
– Filter lines from output
$ cat log.txt | grep "error"
[ERROR] Something went wrong!
awk
– Extract fields
$ ps -ef | grep amazon | awk -F" " '{print $2}'
2345
🐛 Debugging & Safe Scripting
set -x
– Enable debug mode (prints each command before executing)
set -e
– Exit script on any command failure (except in a pipeline)
set -o pipefail
– Also fail if any part of a pipeline fails
🌐 Network Commands
curl {log_file_url} | grep error
– Get a log file and search for 'error'
$ curl http://example.com/log.txt | grep error
[ERROR] Out of memory
curl -X GET {url}
– Send GET request
$ curl -X GET https://api.example.com/data
wget {url}
– Download a file
$ wget https://example.com/file.zip
🔍 Search
sudo find / -name filename
– Search for a file in entire system
$ sudo find / -name myfile.txt
/home/user/docs/myfile.txt
🔁 Bash Scripting Basics
if
statement
if [ condition ]
then
echo "Condition is true"
else
echo "Condition is false"
fi
for
loop
for i in {1..100}; do
echo $i
done
kill -9 PID
– Force kill a process
$ kill -9 1234
trap
– Catch signals like CTRL+C
trap "echo 'Don't use CTRL+C'" SIGINT
(Dangerous!)
trap "rm -rf *" SIGINT
⚠️ Be extremely careful. This will delete everything on CTRL+C!
✅ Notes
- Always test
chmod
,rm
, andtrap
with caution. - Prefer
vim
overvi
as it's more feature-rich. - Use
set -euxo pipefail
for safe and debuggable scripts.
📘 This README is designed to be a beginner-friendly cheatsheet. Contributions and suggestions welcome!
Made with ❤️ by Kishgi