Linux Commands Reference
Comprehensive reference for common Linux and Unix commands with examples.
Back to all tools on ToolForge
About Linux Commands Reference
This reference provides a comprehensive list of essential Linux and Unix commands for file operations, text processing, system administration, and scripting. Commands work across most distributions including Ubuntu, Debian, CentOS, RHEL, Fedora, and macOS.
Command Syntax Conventions
Syntax Notation: command <arg> - Required argument command [arg] - Optional argument command [options] - Optional flags file1|file2 - Either file1 or file2 ... - Repeatable argument Examples: ls -la /home - List with details in /home cat file.txt - Display file contents grep "pattern" * - Search pattern in all files
Essential Commands by Category
File & Directory Operations
| Command | Description | Example |
|---|---|---|
ls | List directory contents | ls -la |
ls -la | List all files with details | ls -la /etc |
cd <dir> | Change directory | cd /var/log |
pwd | Print working directory | pwd |
mkdir <name> | Create directory | mkdir -p project/src |
rm <file> | Remove file | rm -rf temp/ |
cp <src> <dest> | Copy files | cp -r dir1 dir2 |
mv <src> <dest> | Move/rename files | mv old.txt new.txt |
touch <file> | Create empty file/update timestamp | touch file.txt |
ln -s <src> <link> | Create symbolic link | ln -s /path/to/target link |
cat <file> | Display file contents | cat file.txt |
head -n N <file> | First N lines | head -20 logfile.log |
tail -n N <file> | Last N lines | tail -f logfile.log |
less <file> | View file (scrollable) | less largefile.txt |
wc -l <file> | Count lines | wc -l file.txt |
Search & Find
| Command | Description | Example |
|---|---|---|
find <path> -name <pattern> | Find files by name | find . -name "*.log" |
find . -type f | Find all files | find /var -type f |
find . -type d | Find all directories | find . -type d -name "src" |
find . -mtime -N | Files modified N days ago | find . -mtime -7 |
find . -size +N[M|G] | Files larger than N | find . -size +100M |
grep <pattern> <file> | Search text in file | grep "error" logfile |
grep -r <pattern> . | Recursive search | grep -r "TODO" . |
grep -i <pattern> | Case-insensitive search | grep -i "error" |
grep -v <pattern> | Inverse match (exclude) | grep -v "^#" config |
grep -n <pattern> | Show line numbers | grep -n "main" *.c |
which <command> | Show command path | which python3 |
whereis <command> | Find binary/source/man | whereis gcc |
locate <name> | Find by name (indexed) | locate httpd.conf |
Permissions & Ownership
| Command | Description | Example |
|---|---|---|
chmod <mode> <file> | Change permissions | chmod 755 script.sh |
chmod +x <file> | Add execute permission | chmod +x install.sh |
chown <user>:<group> <file> | Change ownership | chown www-data:www-data site/ |
chgrp <group> <file> | Change group | chgrp developers project/ |
umask <value> | Set default permissions | umask 022 |
ls -la | View permissions | ls -la file.txt |
Process Management
| Command | Description | Example |
|---|---|---|
ps aux | List all processes | ps aux | grep nginx |
top | Live process view | top |
htop | Interactive process viewer | htop |
kill <PID> | Terminate process | kill 1234 |
kill -9 <PID> | Force kill | kill -9 1234 |
killall <name> | Kill by name | killall chrome |
pkill <pattern> | Kill by pattern | pkill -f python3 |
pgrep <name> | Find PID by name | pgrep nginx |
nohup <cmd> | Run immune to hangups | nohup server.sh & |
bg | Resume job in background | bg %1 |
fg | Bring job to foreground | fg %1 |
jobs | List background jobs | jobs -l |
System Information
| Command | Description | Example |
|---|---|---|
uname -a | System information | uname -a |
hostname | Show/set hostname | hostname -I |
uptime | System uptime | uptime |
whoami | Current user | whoami |
id | User/group IDs | id |
w | Logged in users | w |
last | Login history | last -10 |
df -h | Disk space (human-readable) | df -h |
du -sh <dir> | Directory size | du -sh /var/log |
free -h | Memory usage | free -h |
vmstat | Virtual memory stats | vmstat 1 5 |
iostat | I/O statistics | iostat -x 1 |
Network Commands
| Command | Description | Example |
|---|---|---|
ping <host> | Test connectivity | ping -c 4 google.com |
curl <url> | Transfer data | curl -O https://example.com/file |
wget <url> | Download file | wget -O file.zip URL |
ssh <user>@<host> | SSH connection | ssh [email protected] |
scp <src> <dest> | Secure copy | scp file.txt user@host:/path |
rsync -av <src> <dest> | Sync files | rsync -av src/ dest/ |
netstat -tulpn | Network connections | netstat -tulpn | grep :80 |
ss -tulpn | Socket statistics | ss -tulpn |
ip addr | IP addresses | ip addr show |
ip route | Routing table | ip route show |
dig <domain> | DNS lookup | dig example.com |
nslookup <domain> | DNS query | nslookup google.com |
traceroute <host> | Trace route | traceroute google.com |
Text Processing
| Command | Description | Example |
|---|---|---|
sort <file> | Sort lines | sort -n numbers.txt |
uniq <file> | Remove duplicates | sort file | uniq |
cut -d : -f 1 | Extract fields | cut -d: -f1 /etc/passwd |
paste <file1> <file2> | Merge lines | paste file1 file2 |
tr <set1> <set2> | Translate characters | tr 'a-z' 'A-Z' |
sed 's/old/new/' | Stream editor | sed -i 's/foo/bar/g' file |
awk '{print $1}' | Pattern scanning | awk '{sum+=$1} END {print sum}' |
diff <file1> <file2> | Compare files | diff -u old.txt new.txt |
comm <file1> <file2> | Compare sorted files | comm -12 file1 file2 |
Archive & Compression
| Command | Description | Example |
|---|---|---|
tar -czf <out.tar.gz> <files> | Create gzip archive | tar -czf backup.tar.gz /data |
tar -xzf <file.tar.gz> | Extract gzip archive | tar -xzf archive.tar.gz |
tar -cjf <out.tar.bz2> | Create bzip2 archive | tar -cjf backup.tar.bz2 /data |
gzip <file> | Compress file | gzip -9 file.txt |
gunzip <file.gz> | Decompress file | gunzip file.gz |
zip -r <out.zip> <files> | Create ZIP archive | zip -r archive.zip folder/ |
unzip <file.zip> | Extract ZIP | unzip archive.zip |
Package Management
| Command | Description | Example |
|---|---|---|
apt update | Update package list (Debian/Ubuntu) | apt update && apt upgrade |
apt install <pkg> | Install package | apt install nginx |
apt remove <pkg> | Remove package | apt remove nginx |
yum install <pkg> | Install package (RHEL/CentOS) | yum install httpd |
dnf install <pkg> | Install package (Fedora) | dnf install nginx |
brew install <pkg> | Install package (macOS/Homebrew) | brew install node |
pip install <pkg> | Install Python package | pip install requests |
npm install <pkg> | Install Node package | npm install express |
User Management
| Command | Description | Example |
|---|---|---|
useradd <name> | Create user | useradd -m john |
userdel <name> | Delete user | userdel -r john |
passwd <name> | Change password | passwd john |
usermod -aG <grp> <usr> | Add user to group | usermod -aG sudo john |
groupadd <name> | Create group | groupadd developers |
groups <name> | Show user groups | groups john |
su - <user> | Switch user | su - postgres |
sudo <cmd> | Execute as root | sudo systemctl restart nginx |
Frequently Asked Questions
- What is the purpose of the ls command?
- The ls command lists directory contents. Common options: -l (long format with permissions), -a (show hidden files starting with .), -la (combine both), -lh (human-readable sizes), -R (recursive), -t (sort by time), -S (sort by size).
- How do I find files in Linux?
- Use the find command: 'find /path -name filename' searches by name, 'find . -type f' finds all files, 'find . -type d' finds directories, 'find . -mtime -7' finds files modified in last 7 days, 'find . -size +100M' finds files larger than 100MB.
- What is the difference between chmod and chown?
- chmod changes file permissions (read/write/execute), chown changes file ownership (user/group). Example: 'chmod 755 file' sets rwxr-xr-x permissions, 'chown user:group file' transfers ownership to specified user and group.
- How do I view and search file contents?
- cat displays entire file, head shows first N lines, tail shows last N lines, less allows scrolling, grep searches for patterns. Example: 'grep -r pattern .' searches recursively, 'tail -f logfile' follows log updates.
- What are pipes and redirection?
- Pipes (|) pass output from one command to another: 'ls | grep txt'. Redirection: > overwrites output to file, >> appends, 2> redirects errors, < reads from file. Example: 'command > file 2>&1' redirects both stdout and stderr.
- How do I manage processes?
- ps aux lists processes, top/htop shows live process view, kill PID terminates process, kill -9 PID force kills, pkill name kills by name, bg/fg manage background/foreground jobs, nohup runs commands immune to hangups.