Linux Commands
Overview of common and advanced Linux commands.
Tip: Examples in this article default to using Bash. If your default shell is not Bash, you can run echo $SHELL to check and adjust the syntax accordingly.
1. Quick Reference for Basic Commands
1.1 Navigation and Viewing
# Location and navigation
pwd # Show current directory
ls -lah # List files (including hidden, human-readable sizes)
cd /path/to/dir # Enter directory
cd - # Switch between previous and current directory
# View file contents
cat file.txt # View small files directly
less -N file.txt # Paginated view, N shows line numbers (q to quit, / to search)
head -n 20 file.txt # View first 20 lines
tail -n 50 -f app.log # Continuously view log tail (Ctrl-C to exit)
1.2 Files and Directories
touch a.txt # Create empty file or update timestamp
mkdir -p data/raw # Recursively create directories
cp -r src/ dst/ # Copy directory
mv old.txt new.txt # Rename/move
rm -rf tmp/ # Careful! Recursively delete directory
# Safety tip: ls to confirm path before deleting; use rm -ri for interactive confirmation in production
1.3 Search and Text Processing
grep -nri "keyword" ./ # Recursive search (case-insensitive, show line numbers)
grep -R --include='*.log' "ERROR" logs/ # Limit to specific extension
wc -l access.log # Count lines
sort -u domains.txt # Sort and deduplicate
uniq -c ip.txt | sort -nr | head # Count duplicates and sort by frequency
cut -d, -f1,3 data.csv # Extract columns 1 and 3 (comma-separated)
tr -d '\r' < file.txt > file.unix.txt # Remove Windows carriage returns
1.4 Permissions and Users
ls -l # View permissions rwx (u/g/o)
chmod u+x run.sh # Add execute permission for user
chmod -R 640 conf/ # Recursively set permissions
sudo chown -R user:group /srv/app # Recursively change owner/group
whoami && id # Current user and group info
1.5 Compression and Archiving
tar -czvf app.tar.gz app/ # Archive and compress
tar -xzvf app.tar.gz # Extract
zip -r logs.zip logs/ # Create zip
unzip logs.zip -d ./logs_out # Extract zip
1.6 Network and Downloads
ping -c 4 example.com # Connectivity test (Windows uses ping -n 4)
curl -I https://example.com # View response headers
wget -c https://host/file.iso # Resume download
ssh user@host # Remote login
scp file user@host:/path/ # Upload file
scp -r dir user@host:/path/ # Upload directory
# rsync is more efficient:
rsync -avzP dir/ user@host:/path/ # Incremental sync + progress
1.7 System Status and Processes
date && uptime # Time and load
uname -a # Kernel/system info
df -h # Disk usage
du -sh * | sort -h # Size of items in current directory
free -h # Memory overview
top # Interactive processes (q to quit)
ps aux | grep python # Process query
kill 12345 # Send SIGTERM (graceful exit)
kill -9 12345 # Force exit (use with caution)
2. Advanced Techniques and Combinations
2.1 Redirection, Pipes, and Logging
cmd > out.txt # Overwrite
cmd >> out.txt # Append
cmd 2> err.txt # Error output only
cmd > all.txt 2>&1 # Merge stdout and stderr
long_cmd | tee -a run.log # Output to terminal and append to log
2.2 find and xargs
# Find .log files over 100MB and interactively delete
find . -type f -name "*.log" -size +100M -print0 | xargs -0 -I{} rm -i {}
# Batch replace text (GNU sed)
find . -type f -name "*.conf" -print0 | xargs -0 sed -i 's/DEBUG/INFO/g'
2.3 Bash Globbing and Brace Expansion
echo file{1..3}.txt # file1.txt file2.txt file3.txt
echo {dev,stg,prod}-app # dev-app stg-app prod-app
# Enable ** recursive globbing (current session)
shopt -s globstar
ls **/*.md
2.4 Common grep/awk/sed Snippets
# Count Nginx status code distribution (assuming column 9 is status code)
awk '{cnt[$9]++} END{for (c in cnt) printf "%s %d\n", c, cnt[c] | "sort -n"}' access.log
# Sum column 2 (comma-separated)
awk -F, '{s+=$2} END{print s}' data.csv
# Extract line range and in-place replacement (GNU sed)
sed -n '1,50p' config.yml
sed -i 's/timeout: .*/timeout: 30s/' config.yml
2.5 Job Control and Long-Running Tasks
# Ctrl-Z suspends foreground process -> enters background job
jobs # View jobs
bg %1 # Put job 1 in background to continue
fg %1 # Bring back to foreground
# Background run independent of terminal (ignore SIGHUP)
nohup long_cmd > run.out 2>&1 &
disown -h %1 # Detach from current shell management (optional)
2.6 Scheduled Tasks and Services
crontab -e # Edit current user's cron jobs
# ┌ minute (0-59)
# │ ┌ hour (0-23)
# │ │ ┌ day (1-31)
# │ │ │ ┌ month (1-12)
# │ │ │ │ ┌ weekday (0-7, Sunday is 0 or 7)
# │ │ │ │ │
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
# Systemd service management (root/with sudo)
sudo systemctl status nginx
sudo systemctl enable --now nginx
journalctl -u nginx -f
2.7 Package Management
Package managers by distribution:
# Debian/Ubuntu
sudo apt update && sudo apt install -y htop git curl
# CentOS/RHEL
sudo yum install -y htop git curl # or dnf
# Arch
sudo pacman -Syu htop git curl
3. Using screen to Manage Long-Running Tasks
Even after SSH disconnect, tasks will continue running. When running long tasks on remote servers (training, backup, scraping, compiling, etc.), it’s recommended to use screen to create “detachable sessions.” Even if SSH disconnects, tasks will continue running in the background.
3.1 Installation
# Debian/Ubuntu
sudo apt update && sudo apt install -y screen
# CentOS/RHEL
sudo yum install -y screen # or dnf install -y screen
3.2 Core Operations
Here’s a quick reference for common commands.
- Create a named session:
screen -S name
- List all sessions:
screen -ls
- Resume session (attach):
screen -r session_ID_or_name
-
Temporarily exit (detach, doesn’t interrupt task): Press Ctrl + a, then press d (shows [detached])
-
Completely exit session: Type
exitinside the session or press Ctrl + d; can also terminate from outside:
screen -S name -X quit
3.3 Common Shortcuts
Inside screen, press Ctrl+a first, then the following key.
- c: Create new window
- n / p: Next/previous window
- ”: List and switch windows
- A: Rename current window
- [: Enter copy/scroll mode (scroll up/down to view history, Esc to exit)
- H: Toggle current window logging (generates screenlog.N)
-
S: Horizontal split; : Vertical split; Tab switches between regions; X closes split region - ?: Show help
You can adjust default scroll buffer, log path, status bar, etc. in ~/.screenrc.
3.4 One-Time Start and Background Run Command
# Method 1: Create session and run command directly in background
screen -dmS job_name bash -c 'python train.py --epochs 50 >> train.log 2>&1; exec bash'
# Method 2: Enter session, manually run command, then detach
screen -S job_name
# ...(run your command)...
# Ctrl-a d to detach
Note: -d -m means start without attaching; -S specifies session name; use bash -c to chain commands and log redirection.
3.5 Logging and Troubleshooting
# Temporarily log current window inside screen (press H again to stop)
Ctrl-a H
# Start with logging enabled and specify file
screen -L -Logfile /var/log/myjob.log -S myjob
# Clean up invalid sessions (zombie/leftover):
screen -wipe
3.6 Practical Workflow Example
# 1) Connect to server and create named session
screen -S nightly_backup
# 2) Run long-term task
/usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
# 3) Detach (doesn't interrupt task)
Ctrl-a d
# 4) Disconnect or reconnect later
ssh user@server
screen -r nightly_backup
# 5) Exit after task completes
exit # or Ctrl-d
Tip: If you see “multiple matching sessions” prompt, first screen -ls to get specific ID, then screen -r 1234.pts-0.server.
Related tools: tmux has stronger split-window and status bar capabilities; if you already have a preference, either one works.
4. Common Pitfalls and Best Practices
- Confirm before deleting: Always
lsto verify path beforerm -rf, or userm -rifor interactive deletion. - sudo and redirection:
echo "a" >> /etc/fileredirection executes in current shell, need:sudo sh -c 'echo "a" >> /etc/file'. - Environment and PATH: Put common aliases in
~/.bashrc, likealias ll='ls -alF'; runsource ~/.bashrcafter modification. - Distinguish distribution command differences: Package managers (apt/yum/dnf/pacman), service names, and config paths may differ.
- Performance and localization: Add
LC_ALL=Cfor batch text processing to improve speed (use as needed).
5. References and Quick Lookup
- man pages:
man cmd,cmd --help - Bash manual and guide: GNU Bash Reference Manual
- Screen sessions:
man screen, or check~/.screenrcexamples
If you need additions for tmux, zsh, advanced regex, or more systematic logging/monitoring solutions, feel free to leave feedback in the comments.