Skip to content

Linux Command Cheat Sheet

File Operations

ls -la                   # list files with details
ls -lh                   # human-readable sizes
ls -lt                   # sorted by modification time

cp file.txt /backup/     # copy file
cp -r dir/ /backup/      # copy directory recursively
cp -a dir/ /backup/      # copy preserving attributes

mv file.txt new.txt      # rename
mv file.txt /target/     # move

rm file.txt              # delete file
rm -rf dir/              # delete directory (careful!)
rm -i file.txt           # prompt before each delete

mkdir dir                # create directory
mkdir -p a/b/c           # create nested directories

find . -name "*.py"      # find files by name
find . -type f -size +10M  # files larger than 10MB

Viewing Files

cat file.txt               # print entire file
less file.txt              # scroll through (q to quit)
head -20 file.txt          # first 20 lines
tail -20 file.txt          # last 20 lines
tail -f log.txt            # follow new lines (live)
wc -l file.txt             # line count
nl file.txt                # numbered lines

Text Processing

grep "error" log.txt                # search for pattern
grep -r "todo" src/                  # recursive search
grep -i "warning" log.txt            # case-insensitive
grep -v "debug" log.txt              # exclude matches
grep -c "error" log.txt              # count matches

sed 's/old/new/g' file.txt           # replace all occurrences
sed -i '.bak' 's/old/new/g' file.txt  # in-place with backup
awk '{print $1, $3}' data.txt         # print columns
sort file.txt                         # sort lines
uniq file.txt                         # remove duplicates (sort first)

Process Management

ps aux                   # all running processes
ps aux | grep nginx      # find specific process
top                      # live process viewer
htop                     # better process viewer (if installed)
kill PID                 # terminate process
kill -9 PID              # force kill
killall nginx            # kill all processes by name

Networking

curl https://example.com          # HTTP request
curl -o file.txt https://example.com/file  # download
wget https://example.com/file     # download (alternative)

ping google.com                   # test connectivity
traceroute google.com             # trace network path
ss -tln                           # listening TCP ports
ss -uan                           # all UDP connections
netstat -tln                      # if ss is not available

ip a                              # network interfaces and IPs
ip r                              # routing table
nslookup example.com              # DNS lookup (or `dig example.com`)

Disk Usage

df -h                    # disk free (human-readable)
du -sh dir/              # directory size
du -sh * | sort -rh      # sizes of all items, sorted
lsblk                    # block devices
mount                    # mounted filesystems

Compression

tar -czf archive.tar.gz dir/        # compress directory
tar -xzf archive.tar.gz             # extract
tar -czf archive.tar.gz --exclude='node_modules' dir/

zip -r archive.zip dir/             # zip directory
unzip archive.zip                   # unzip

gzip file.txt                       # compress (replaces with .gz)
gunzip file.txt.gz                  # decompress

Permissions

chmod 644 file.txt     # rw-r--r--
chmod 755 script.sh    # rwxr-xr-x
chmod 600 secret.txt   # rw-------
chown user:group file  # change owner:group
umask 022              # default permissions for new files

System Information

uname -a                # kernel version
cat /etc/os-release     # OS distribution
free -h                 # memory usage
lscpu                   # CPU info
uptime                  # how long since boot
dmesg | tail            # kernel messages

Package Management

Debian/Ubuntu

apt update && apt upgrade     # update all packages
apt install package            # install
apt remove package             # uninstall
apt search keyword             # search packages

RHEL/Fedora

dnf update                    # update all packages
dnf install package            # install
dnf remove package             # uninstall
dnf search keyword             # search packages

SSH

ssh user@host                  # connect to remote
ssh -p 2222 user@host          # custom port
ssh-keygen -t ed25519          # generate SSH key
ssh-copy-id user@host          # copy public key to server
ssh -i key.pem user@host       # use specific key
scp file.txt user@host:/path/  # copy file over SSH

Shortcuts

ShortcutAction
Ctrl+CKill current command
Ctrl+ZSuspend current command
Ctrl+DEnd-of-file (exit shell)
Ctrl+WDelete word before cursor
Ctrl+UDelete entire line
Ctrl+RReverse search history
TabAuto-complete
!!Repeat last command
!$Last argument of last command

Related: See our grep command guide and file permissions guide.