ls -l /path/to/directory
Shows file permissions, sizes, and timestamps.
mkdir -p dir1/dir2/dir3
Creates nested directories recursively.
rm -rf /path/to/directory
Forcefully removes directories and contents.
cp -a source/ destination/
Copies files/directories preserving permissions.
find /search/path -name "*.log"
Searches for all .log files in the directory.
tar -czf archive.tar.gz /source/directory
Compresses a directory into a tarball.
tar -xzf archive.tar.gz -C /destination/
Extracts files to a specific directory.
du -sh /path/to/directory
Shows the total size of a directory.
ln -s /original/file /link/location
Creates a symbolic link to a file.
diff file1.txt file2.txt
Highlights differences between two files.
grep "error" /var/log/syslog
Finds lines containing "error" in the log file.
sed -i 's/old/new/g' file.txt
Searches and replaces text in-place.
awk '{print $1}' file.txt
Prints the first column of data.
sort file.txt | uniq -c
Counts occurrences of sorted lines.
grep -oEw '\w+' file.txt | sort | uniq -c
Counts word frequencies in a file.
grep -E '[0-9]{3}-[0-9]{4}' file.txt
Matches patterns like 123-4567.
cat file.txt | grep "keyword" > output.txt
Saves filtered output to a new file.
cat file1.txt file2.txt > combined.txt
Concatenates two files into one.
sed -n '5,10p' file.txt
Prints lines 5 to 10 of the file.
tac file.txt
Prints lines in reverse order.
ps -aux
Shows all running processes with details.
killall firefox
Terminates all instances of Firefox.
sleep 100 &
Runs a command in the background.
top
Displays real-time process activity.
sudo apt update
Executes a command with elevated privileges.
cron -e
Edits cron jobs for automated tasks.
systemctl status apache2
Checks the status of the Apache service.
netstat -tuln
Displays active listening ports.
strace ls
Monitors system calls made by a command.
gdb -p <PID>
Debugs a running process interactively.
ifconfig
Displays network interface configurations.
wget http://example.com/file.iso
Downloads a file from a URL.
curl -I http://localhost:8080
Checks HTTP headers from a server.
ssh user@192.168.1.100
Establishes an SSH session with a remote server.
scp file.txt user@remote:/path/to/destination
Securely copies files over SSH.
dig example.com
Queries DNS records for a domain.
ping -c 4 8.8.8.8
Pings Google's DNS server 4 times.
tcpdump -i eth0 port 80
Captures HTTP traffic on the eth0 interface.
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
Configures firewall to allow SSH traffic.
curl -X POST -d "name=value" http://api.example.com/endpoint
Sends a POST request with data.
cat << EOF > file.txt
This is a multi-line text.
EOF
Creates a file with a block of text.
i=1; while [ $i -le 5 ]; do echo $i; i=$((i+1)); done
Counts from 1 to 5 using a loop.
myfunc() { echo "Hello, $1!"; }; myfunc World
Defines and calls a function with arguments.
[[ $(date +%H) -gt 12 ]] && echo "Afternoon" || echo "Morning"
Prints greeting based on time of day.
read -p "Enter name: " name; echo "Hello $name"
Prompts the user for input.
set -e; false; echo "This won't print"
Exits immediately on command failure.
parallel echo {} ::: arg1 arg2 arg3
Runs commands in parallel.
#!/usr/bin/expect
spawn ssh user@host
expect "password:" { send "secret\r" }
interact
Automates SSH login with password.
awk '/^[[:space:]]*$/ {next;1}' file.txt
Skips empty lines in a file.
ps aux | awk '{print $2}' | xargs -I {} kill {}
Kills all processes (use with caution).
0 2 * * * /backup/script.sh
Runs a script daily at 2 AM.
find /var/log/ -name "*.log" -exec gzip {} \;
Compresses log files older than 7 days.
tar -czf /backup/$(date +%Y%m%d).tar.gz /source
Creates a timestamped backup.
find /tmp -type f -mtime +7 -exec rm {} \;
Deletes files older than 7 days in /tmp.
df -h | awk '$NF=="/"{print $5}' | grep -q '^9' && echo "Critical!"
Alerts if root partition is over 90% full.
apt update && apt upgrade -y
Updates all packages automatically.
systemctl restart nginx
Restarts the Nginx service.
useradd -m -s /bin/bash newuser
Creates a new user with a home directory.
ufw allow 80/tcp
Opens port 80 for HTTP traffic.
iftop
Displays real-time network traffic.
This plan covers essential Bash commands for intermediate to advanced users. Practice these examples in a Linux environment to gain proficiency. Combine commands using pipes, redirects, and scripting to automate complex tasks.