The Ultimate Guide to Unix/Linux Commands with Examples

 

Unix/Linux is a powerful operating system, widely used by developers and system administrators. Mastering its command-line tools enhances productivity and helps you manage files, processes, permissions, and systems efficiently. This guide categorizes the most essential Unix/Linux commands by function — with practical examples included.

📁 File and Directory Commands

Listing and Navigating Directories

ls # List files and directories
ls -al # Long list format with hidden files ls -lt # Sort by last modified time cd dir # Change to directory 'dir' cd # Go to home directory pwd # Print working directory

File Viewing and Creation

cat > file.txt # Create file and input content
cat file.txt # Display content more file.txt # Page-by-page output head file.txt # First 10 lines tail file.txt # Last 10 lines tail -f file.txt # Continuously output file updates

File and Directory Operations


touch file.txt # Create or update a file rm file.txt # Delete a file rm -r dir # Recursively delete directory rm -f file.txt # Force delete rm -rf dir # Forcefully delete directory cp file1 file2 # Copy file cp -r dir1 dir2 # Copy directory mv file1 file2 # Move or rename ln -s file link # Create symbolic link mkdir dir # Create new directory

⚙️ Process Management

View Running Processes

ps # Current user's processes
top # Real-time process viewer

Terminate Processes

kill PID # Kill specific process
killall proc # Kill all named processes pkill pattern # Kill matching processes

Job Control

bg # Resume stopped job in background
fg # Resume job in foreground fg %1 # Bring job 1 to foreground

🔒 File Permissions

Permission Basics

Each permission level is represented numerically:

  • r (read) = 4

  • w (write) = 2

  • x (execute) = 1

Change File Permissions

chmod 755 filename # Owner: rwx, Group: r-x, Others: r-x
chmod 777 filename # Full permissions to all ls -ltr # View permissions in long list

🔍 Search and Find

Using grep to Search Patterns


grep "this" demo.txt # Search 'this' in file grep -r "ramesh" * # Recursive search grep -l "this" demo_* # Only filenames with match

Count Lines


wc -l file # Count lines grep -w "pattern" -c file # Count lines with exact match

Locate Files and Processes


locate filename # Fast find (uses updatedb) find . -name "file*" # Search in current dir pgrep pattern # Find process IDs by name

🖥️ System Information

General System Details


date # Current date and time cal # Current month calendar uptime # System uptime w # Who is logged in whoami # Current user finger user # User info uname -a # Kernel and system info

CPU & Memory


cat /proc/cpuinfo # CPU details cat /proc/meminfo # Memory details

Disk and Resource Usage


df -h # Disk space usage du -sh * # Directory sizes free -h # Memory usage

Locate Executables

whereis app # Locate binaries/source/docs
which app # Show default command path

📦 Compression and Archives

Tar Commands

tar cf file.tar file # Create tar
tar xf file.tar # Extract tar tar czf file.tar.gz files # Tar with gzip tar xzf file.tar.gz # Extract gzipped tar tar cjf file.tar.bz2 files # Tar with bzip2 tar xjf file.tar.bz2 # Extract bzipped tar

Gzip Compression


gzip file # Compress file gzip -d file.gz # Decompress file

🌐 Networking Commands

Basic Network Utilities

ping example.com # Check connectivity
whois openai.com # Domain ownership info dig openai.com # DNS lookup dig -x 8.8.8.8 # Reverse DNS lookup wget http://file.com/file.txt # Download file wget -c file # Resume stopped download

⌨️ Keyboard Shortcuts & Command History

Handy Shortcuts

  • Ctrl + C: Terminate current command

  • Ctrl + Z: Pause current command

  • Ctrl + D: Logout/EOF

  • Ctrl + W: Delete word

  • Ctrl + U: Delete entire line

  • Ctrl + R: Reverse search command history

  • !!: Repeat last command

  • exit: Logout

Command History

history # Show command history

✍️ Practical File Editing and Examples

Create a File

touch fruits.txt

View and Edit a File

cat fruits.txt
vi devops.txt

Add this content:

Apple
Mango Banana Cherry Kiwi Orange Guava

Save and Exit in vi:

  • Press i to insert

  • Press Esc, then type :wq and press Enter


📄 Specific Line Display

View Top/Bottom Lines

head -3 filename # First 3 lines
tail -3 filename # Last 3 lines

Compare Two Files

diff file1 file2 # Show differences

✅ Summary of Categories

CategoryCommands
📁 File Managementls, cd, pwd, cp, mv, rm, touch
🔄 Process Managementps, top, kill, fg, bg, pkill
🔒 Permissionschmod, ls -ltr
🔍 Searchinggrep, find, locate, pgrep
🖥️ System Infodate, uptime, uname, df, free
📦 Compressiontar, gzip
🌐 Networkingping, wget, dig, whois
⌨️ ShortcutsCtrl+C, !!, Ctrl+R, exit

Comments