This is just for my own reference to look up commands.

There are too many Linux commands to memorize them all. It's enough to remember the common ones and be able to look up the rest quickly when needed.

Table of Contents

  1. Quick Reference Table
  2. Files and Directories
  3. Viewing Files
  4. Text Processing
  5. Finding Files
  6. Permissions and Users
  7. Processes and Services
  8. Networking
  9. Package Management
  10. Disks and Mounting
  11. Compression and Archiving
  12. Logs and Troubleshooting
  13. Shell Miscellany
  14. Common One-Liners

Quick Reference Table

Task Common Commands
View directories ls, tree
Change directories cd, pwd
Copy, move, delete cp, mv, rm
View files cat, less, head, tail
Search text grep, rg
Process text awk, sed, cut, sort, uniq
Find files find, locate, which
Change permissions chmod, chown
View processes ps, top, htop, kill
Networking ip, ss, curl, ping, ssh
Archive and compress tar, zip, gzip

Files and Directories

ls

View directories:

ls
ls -l
ls -la
ls -lh

Common parameters:

  • -l: Detailed list
  • -a: Show hidden files
  • -h: Human-readable file sizes, e.g., 1.2G

cd and pwd

pwd
cd /var/www
cd ~
cd -

cd - takes you back to the previous directory, which is quite useful.

cp

cp a.txt b.txt
cp -r src/ backup/
cp -a src/ backup/

-a attempts to preserve permissions, timestamps, and other information, making it more reliable than -r when backing up directories.

mv

mv old.txt new.txt
mv file.txt /tmp/

Can be used for both moving and renaming.

rm

rm file.txt
rm -r folder/
rm -rf folder/

Be careful not to slip up with rm -rf. Especially when using variables, echo the path first, otherwise you will regret it if you delete the wrong thing.

mkdir

mkdir logs
mkdir -p app/data/cache

-p automatically creates intermediate directories.

Viewing Files

cat

cat file.txt

View small files directly. Don't use it for large files as it will flood your screen.

less

less app.log

Common operations:

  • /keyword: Search
  • n: Next result
  • q: Quit

head and tail

head -n 20 app.log
tail -n 50 app.log
tail -f app.log

Generally use tail -f for viewing logs.

nl

nl file.txt

Displays files with line numbers. Useful when troubleshooting configurations.

Text Processing

grep

grep "ERROR" app.log
grep -n "ERROR" app.log
grep -R "listen" /etc/nginx
grep -i "error" app.log

Common parameters:

  • -n: Show line numbers
  • -R: Search directories recursively
  • -i: Ignore case

If rg (ripgrep) is available on the system, I usually prefer it as it's much faster:

rg "ERROR"
rg "listen" /etc/nginx

sed

sed -n '1,20p' file.txt
sed 's/old/new/g' file.txt

Often used for replacing text or just viewing specific lines.

awk

awk '{print $1}' access.log
awk '{print $1, $9}' access.log

Processes text by columns. Frequently used for log analysis.

cut

cut -d':' -f1 /etc/passwd
cut -d',' -f1,3 data.csv

Use this for simple column splitting; no need to write complex scripts right away.

sort and uniq

sort file.txt
sort -n numbers.txt
sort file.txt | uniq
sort file.txt | uniq -c

Very handy for counting duplicates.

wc

wc -l file.txt
wc -c file.txt

Counts lines and bytes.

Finding Files

find

find . -name "*.log"
find /var/log -type f -mtime -7
find . -type f -size +100M

A few common conditions:

  • -name: By name
  • -type f: Find files only
  • -type d: Find directories only
  • -mtime -7: Modified within 7 days
  • -size +100M: Larger than 100M

which

which nginx
which python

Shows exactly where a command is coming from.

locate

locate nginx.conf

Fast, but relies on an index. Newly created files might not be found.

Permissions and Users

chmod

chmod 644 file.txt
chmod 755 script.sh
chmod +x script.sh

Common permissions:

  • 644: Regular files
  • 755: Executable scripts or directories
  • 600: Sensitive files like private keys

chown

sudo chown user:group file.txt
sudo chown -R www-data:www-data /var/www/app

You often need to check this when website directory permissions are incorrect.

whoami
id
passwd
sudo useradd name
sudo usermod -aG sudo name

Processes and Services

Viewing Processes

ps aux
ps aux | grep nginx
top
htop

Use htop if you have it; it looks nicer.

Killing Processes

kill 1234
kill -9 1234
pkill -f nginx

kill -9 is a forceful kill, not a universal fix button. Try a normal kill first, and use it only if that fails.

systemd Services

sudo systemctl status nginx
sudo systemctl start nginx
sudo systemctl restart nginx
sudo systemctl enable nginx

View service logs:

journalctl -u nginx -f
journalctl -u nginx --since "1 hour ago"

Networking

IP and Ports

ip addr
ip route
ss -tuln
ss -tulnp

ss -tulnp shows which process is occupying which port.

Connectivity

ping 1.1.1.1
curl -I https://example.com
curl http://127.0.0.1:3000

Don't just ping to test HTTP services. A successful ping doesn't mean the website is alive.

SSH

ssh root@1.2.3.4
ssh -p 2222 user@1.2.3.4
scp file.txt user@1.2.3.4:/tmp/

rsync

rsync -avz ./dist/ user@server:/var/www/app/
rsync -avz --delete ./dist/ user@server:/var/www/app/

--delete removes extra files on the destination side, so think carefully before using it.

Package Management

Debian / Ubuntu:

sudo apt update
sudo apt install nginx
sudo apt upgrade

CentOS / RHEL:

sudo yum install nginx
sudo dnf install nginx

Arch:

sudo pacman -S nginx

openSUSE:

sudo zypper install nginx

Commands vary across distributions. Make sure not to copy the wrong one for your system.

Disks and Mounting

Viewing Disks

df -h
du -sh *
du -sh /var/log

Finding large directories:

du -h --max-depth=1 / | sort -h

Block Devices

lsblk
blkid
sudo fdisk -l

Mounting

sudo mount /dev/sdb1 /mnt
sudo umount /mnt

Compression and Archiving

tar

tar -czvf archive.tar.gz folder/
tar -xzvf archive.tar.gz
tar -tzf archive.tar.gz

zip

zip -r archive.zip folder/
unzip archive.zip

gzip

gzip file.txt
gunzip file.txt.gz

Logs and Troubleshooting

dmesg
journalctl -xe
journalctl -u service-name -f
tail -f /var/log/syslog
tail -f /var/log/nginx/error.log

Port is occupied:

sudo lsof -i :80
sudo ss -tulnp | grep :80

If configuration changes don't take effect, first check if the service was restarted, then check the logs. Many problems aren't due to missing configurations, but because the service simply didn't reload them.

Shell Miscellany

Environment Variables

env
printenv
export NODE_ENV=production
echo $PATH

alias

alias ll='ls -lah'
alias gs='git status'

Add them to ~/.bashrc or ~/.zshrc to make them permanent.

Executing Scripts

chmod +x script.sh
./script.sh

Cron Jobs

crontab -e
crontab -l

Common One-Liners

Find large files:

find / -type f -size +500M 2>/dev/null

Search for errors in logs:

grep -R --line-number "ERROR" /var/log 2>/dev/null

View the size of each folder in the current directory:

du -h --max-depth=1 . | sort -h

Sync website build artifacts:

rsync -avz --delete ./dist/ user@server:/var/www/site/

View recent logs for a service:

journalctl -u nginx --since "2 hours ago" -f

Find the process occupying a port:

sudo ss -tulnp | grep :3000

Batch delete .tmp files:

find . -type f -name "*.tmp" -delete

It's best to remove -delete and preview the results before executing this command:

find . -type f -name "*.tmp"

Don't delete right away; deleting the wrong files is quite troublesome.