The Linux command line is an intricate web of powerful tools, and mastering even a fraction of its commands can dramatically boost your productivity.
Let’s dive into some essential commands, not just what they do, but how they fit together and why they’re so indispensable.
Navigating the File System
pwd: Print Working Directory. This tells you exactly where you are in the file system hierarchy.- Example:
$ pwd /home/user/documents
- Example:
ls: List Directory Contents. Shows you what files and directories are in your current location.- Example:
$ ls -lha total 20K drwxr-xr-x 2 user user 4.0K Jan 20 10:00 . drwxr-xr-x 3 user user 4.0K Jan 20 09:55 .. -rw-r--r-- 1 user user 512 Jan 20 10:00 my_document.txt -rw-r--r-- 1 user user 1.2K Jan 19 15:30 script.sh-l: Use a long listing format.-h: Print sizes in human-readable format (e.g., 1K, 2M).-a: Do not ignore entries starting with.(hidden files).
- Example:
cd: Change Directory. This is how you move around.- Example:
$ cd documents # Move into the 'documents' directory $ cd .. # Move up one directory $ cd ~ # Move to your home directory $ cd / # Move to the root directory
- Example:
mkdir: Make Directory. Creates a new directory.- Example:
$ mkdir new_project $ mkdir -p parent/child/grandchild # Creates parent, child, and grandchild directories if they don't exist
- Example:
rmdir: Remove Directory. Deletes an empty directory.- Example:
$ rmdir old_stuff - Note: For non-empty directories, use
rm -r.
- Example:
Working with Files
touch: Create Empty Files or Update Timestamps. If the file doesn’t exist, it’s created. If it does, its access and modification times are updated.- Example:
$ touch new_file.txt $ touch existing_file.log # Updates timestamp
- Example:
cp: Copy Files and Directories.- Example:
$ cp source.txt destination.txt # Copy file $ cp -r source_dir destination_dir # Copy directory recursively
- Example:
mv: Move or Rename Files and Directories.- Example:
$ mv old_name.txt new_name.txt # Rename file $ mv my_file.txt /path/to/new/location/ # Move file
- Example:
rm: Remove Files or Directories. Be careful with this one!- Example:
$ rm unwanted_file.txt $ rm -r old_project # Remove directory and its contents recursively $ rm -rf / # DANGER: Do not run this command! It will delete everything.-r: Recursive, for directories.-f: Force, ignore nonexistent files and never prompt.
- Example:
cat: Concatenate and Display Files. Primarily used to display file content.- Example:
$ cat my_document.txt $ cat file1.txt file2.txt > combined.txt # Concatenate two files into a new one
- Example:
less: View File Content Page by Page. More advanced thancatfor large files.- Example:
$ less /var/log/syslog- Navigate with arrow keys,
Page Up/Page Down. Pressqto quit.
- Navigate with arrow keys,
- Example:
head: Display the Beginning of a File.- Example:
$ head -n 10 my_log.txt # Show the first 10 lines
- Example:
tail: Display the End of a File.- Example:
$ tail -n 50 error.log # Show the last 50 lines $ tail -f access.log # Follow the file as it grows (real-time log monitoring)
- Example:
Searching and Filtering
grep: Search for Patterns in Text. Incredibly powerful for finding specific lines in files or command output.- Example:
$ grep "error" /var/log/messages $ ps aux | grep "nginx" # Find processes related to nginxps aux: Lists all running processes.|: The pipe symbol, which sends the output of the command on its left as input to the command on its right.
- Example:
find: Search for Files in a Directory Hierarchy.- Example:
$ find . -name "*.log" # Find all files ending with .log in the current directory and subdirectories $ find /home/user -type f -mtime -7 # Find files modified in the last 7 days in /home/user-type f: Only find files.-mtime -7: Modified less than 7 days ago.
- Example:
System Information and Monitoring
ps: Report a Snapshot of the Current Processes.- Example:
$ ps aux # Shows all processes for all users in a user-friendly format $ ps -ef # Shows all processes in a standard format
- Example:
top: Display Linux Processes. An interactive, real-time view of running processes, CPU usage, memory, etc.- Example:
$ top- Press
kto kill a process, enter PID, then signal (e.g., 9 for SIGKILL). Pressqto quit.
- Press
- Example:
htop: An interactive process viewer. A more user-friendly and feature-rich alternative totop.- Example:
$ htop- Install it if you don’t have it (
sudo apt install htoporsudo yum install htop).
- Install it if you don’t have it (
- Example:
df: Report File System Disk Space Usage.- Example:
$ df -h # Show disk space in human-readable format
- Example:
du: Estimate File Space Usage.- Example:
$ du -sh /var/log # Show total size of /var/log in human-readable format $ du -h --max-depth=1 /home/user # Show sizes of top-level directories in /home/user
- Example:
free: Display Amount of Free and Used Memory in the System.- Example:
$ free -h # Show memory in human-readable format
- Example:
Permissions and Ownership
chmod: Change File Mode Bits (Permissions).- Example:
$ chmod 755 my_script.sh # Owner: rwx, Group: r-x, Others: r-x $ chmod +x my_script.sh # Add execute permission for everyone $ chmod u+w file.txt # Add write permission for the owner- Numeric
755means: owner (4+2+1=7), group (4+0+1=5), others (4+0+1=5). u(user/owner),g(group),o(others),a(all).+(add),-(remove),=(set exactly).
- Numeric
- Example:
chown: Change File Owner and Group.- Example:
$ chown user:group my_file.txt # Change owner to 'user' and group to 'group' $ chown user my_file.txt # Change owner to 'user', group remains unchanged $ chown -R user:group my_directory # Recursively change owner and group
- Example:
Networking
ping: Send ICMP ECHO_REQUEST to Network Hosts. Checks network connectivity.- Example:
$ ping google.com- Press
Ctrl+Cto stop.
- Press
- Example:
ssh: OpenSSH SSH Client (Remote Login Program).- Example:
$ ssh user@remote_host_ip
- Example:
scp: Secure Copy. Copies files between hosts over SSH.- Example:
$ scp local_file.txt user@remote_host:/path/on/remote/ $ scp user@remote_host:/path/on/remote/remote_file.txt . # Copy to current directory
- Example:
Archiving and Compression
tar: Tape Archiver. Used to create and extract compressed archives (often.tar.gzor.tar.bz2).- Example (Create a compressed archive):
$ tar -czvf archive.tar.gz /path/to/directory-c: Create an archive.-z: Compress with gzip.-v: Verbose (show files being processed).-f: Use archive file.
- Example (Extract an archive):
$ tar -xzvf archive.tar.gz # Extract a .tar.gz file $ tar -xjvf archive.tar.bz2 # Extract a .tar.bz2 file-x: Extract files from an archive.-j: Compress with bzip2.
- Example (Create a compressed archive):
Text Manipulation
sed: Stream Editor. Powerful for performing text transformations on an input stream (a file or pipeline).- Example (Replace all occurrences of 'old' with 'new'):
$ sed 's/old/new/g' my_file.txt $ sed -i 's/old/new/g' my_file.txt # Edit file in-places: substitute command.g: global, replace all occurrences on a line.
- Example (Replace all occurrences of 'old' with 'new'):
awk: Pattern Scanning and Processing Language. Excellent for structured text data.- Example (Print the first and third fields of each line):
$ awk '{print $1, $3}' my_data.csv$1refers to the first field,$2to the second, etc. Fields are space-delimited by default.
- Example (Print the first and third fields of each line):
Other Useful Commands
man: Manual Pages. Get help on any command.- Example:
$ man ls- Press
qto quit.
- Press
- Example:
echo: Display a Line of Text. Often used in scripts or to display variables.- Example:
$ echo "Hello, World!" $ echo $PATH # Display the value of the PATH environment variable
- Example:
alias: Create Command Aliases. Shorthands for longer commands.- Example:
$ alias ll='ls -lha' $ ll # Now runs 'ls -lha'
- Example:
history: Display Command History.- Example:
$ history 10 # Show the last 10 commands $ !500 # Re-run command number 500 from history
- Example:
This list is just the beginning, but these commands form the bedrock of effective command-line usage in Linux. The true power comes from combining them using pipes (|), redirection (>, >>, <), and understanding their options.