Linux Cheat Sheet
Linux Cheat Sheet
Linux Cheat Sheet
ls -l /home
• Description: Lists all files and directories in /home with detailed information.
• Options:
cd /var/log
• Tips:
pwd
mkdir -p /tmp/test_dir/sub_dir
rmdir /tmp/test_dir/sub_dir
rm -rf /tmp/test_dir
• Options:
cp -r /etc/nginx /tmp/
• Options:
mv /tmp/nginx /tmp/nginx_backup
touch newfile.txt
stat newfile.txt
• Description: Provides detailed information about the file, such as size, permissions, and
timestamps.
cat /etc/hosts
• Tips:
less /var/log/syslog
head -n 5 /etc/passwd
tail -n 10 /var/log/syslog
• Tips:
nano /tmp/test.txt
• Tips:
o Ctrl + O: Save.
o Ctrl + X: Exit.
• Options:
umask 022
• Options:
5. Networking
ping -c 4 google.com
curl https://example.com
df -h
• Options:
du -sh /var/log
• Options:
o -s: Summary for the directory.
• Options:
umount /mnt
lsblk
blkid
fsck /dev/sdb1
mkfs.ext4 /dev/sdb1
7. Process Management
• Description: Lists all processes with details and filters for apache.
top
kill -9 1234
pkill -f apache
jobs
bg %1
fg %1
8. User Management
who
whoami
passwd john
9. Networking
ifconfig
ip a
netstat -tuln
• Options:
traceroute google.com
wget https://example.com/file.zip
uptime
free -h
vmstat 1 5
• Description: Provides CPU, memory, and I/O stats every second for 5 seconds.
iostat
sar -u 1 3
• Description: Updates the package list and upgrades all packages to the latest version.
• Options:
• Options:
• Options:
• Options:
gzip file.txt
gunzip file.txt.gz
• Description: Changes the owner and group of file.txt to user and group.
sort file.txt
uniq file.txt
wc -l file.txt
#!/bin/bash
• Usage:
chmod +x script.sh
./script.sh
#!/bin/bash
name="Linux"
#!/bin/bash
for i in 1 2 3 4 5; do
done
#!/bin/bash
if [ $1 -gt 10 ]; then
echo "Greater than 10"
else
echo "Less than or equal to 10"
fi
• Description: A script that checks if a number is greater than 10.
uname -a
hostname
uptime
vmstat 1 5
• Description: Displays system performance metrics (CPU, memory, I/O) every 1 second for 5
iterations.
iostat -x
sar -u 1 5
top
• Description: Shows a dynamic real-time view of processes and system resource usage.
htop
free -m
• Options:
dstat
journalctl -u nginx.service
lsof -i :80
strace -c ls
netstat -tuln
ss -tuln
#!/bin/bash
greet() {
echo "Hello, $1!"
}
greet "World"
• Description: Demonstrates a function that greets the given input.
#!/bin/bash
arr=("apple" "banana" "cherry")
for fruit in "${arr[@]}"; do
echo "Fruit: $fruit"
done
• Description: Iterates through an array and prints each element.
19.3 Reading User Input
#!/bin/bash
read -p "Enter your name: " name
echo "Welcome, $name!"
• Description: Prompts the user for input and prints a welcome message.
make
• Other Commands:
crontab -e
javascript
Copy code
0 2 * * * /path/to/script.sh
o Description: Runs script.sh every day at 2:00 AM.
• Options:
• Description: Runs an NGINX container and maps port 8080 to the container’s port 80.
24. Miscellaneous
time ls
uptime
aws s3 ls s3://my-bucket
25.2 Terraform
terraform init
• Other Commands:
25.4 Docker
• Description: Builds a Docker image named my-app from the current directory.
• Description: Splits largefile into 10MB chunks named part_aa, part_ab, etc.
xz file.txt
7z a archive.7z file.txt
• Description: Scans the host at 192.168.1.1 for open ports and services.
dig example.com
id
groups username
30. Security
• Encrypt a file:
gpg -c file.txt
• Decrypt a file:
gpg file.txt.gpg
30.2 fail2ban - Prevent brute-force attacks
nice -n 10 ./script.sh
renice -n -5 -p 1234
32.1 uptime
uptime -p
sudo iotop
• Description: Displays the current date and time in the specified format.
35. Miscellaneous
uptime
echo "10+5" | bc
watch -n 2 ls -lh
unalias ll
locate myfile.txt
sudo updatedb
lsblk -f
uptime
sar -r 1 5
sudo iotop
nice -n 10 ./script.sh
renice -n 5 -p 1234
ip a
nc -zv 192.168.1.1 22