Linux Command Line Guide
This document provides a comprehensive overview of essential Linux command-line
operations, categorized by common tasks. Each command includes a brief description of its
function and the syntax for its use in the terminal.
File & Directory Management
These commands are used for navigating, creating, deleting, copying, and moving files and
directories.
● List directory contents: Displays files and directories in the current location.
ls
○ To list all files, including hidden ones:
ls -a
○ To list files with detailed information (permissions, owner, size, date):
ls -l
● Change directory: Moves you into a different directory.
cd [directory_name]
○ To go to the parent directory:
cd ..
○ To go to your home directory:
cd ~
○ To go to the previous directory:
cd -
● Print working directory: Shows the full path of your current directory:
pwd
● Create a new directory: Makes a new folder:
mkdir [directory_name]
○ To create parent directories if they don't exist:
mkdir -p [path/to/new/directory]
● Remove a directory: Deletes an empty folder:
rmdir [directory_name]
● Remove files or directories: Deletes files or directories. Use with caution!
rm [file_name]
○ To remove a non-empty directory and its contents recursively:
rm -r [directory_name]
○ To remove files/directories forcefully (without prompt):
rm -f [file_name]
○ To combine recursive and forceful deletion:
rm -rf [directory_name]
● Copy files or directories: Duplicates files or directories from one location to another.
cp [source_file] [destination_file]
○ To copy a directory and its contents:
cp -r [source_directory] [destination_directory]
● Move or rename files/directories: Moves files/directories to a new location or renames
them.
mv [source] [destination]
○ To rename a file:
mv old_name.txt new_name.txt
○ To move a file to a different directory:
mv file.txt /path/to/new/directory/
● Create an empty file or update timestamp: Creates a new empty file or updates the
access/modification times of an existing file.
touch [file_name]
File Permissions
These commands are used to manage who can read, write, and execute files and directories.
● Change file permissions: Modifies the read, write, and execute permissions for owner,
group, and others.
chmod [permissions] [file_name]
○ Symbolic mode:
■ u: user (owner), g: group, o: others, a: all
■ +: add permission, -: remove permission, =: set permission
■ r: read, w: write, x: execute
■ Example: Give owner read and write, group read, others no permissions:
chmod u=rw,g=r,o=- file.txt
■ Example: Add execute permission for everyone:
chmod a+x script.sh
○ Numeric (Octal) mode: Each permission (rwx) has a numeric value:
■ r (read) = 4
■ w (write) = 2
■ x (execute) = 1
■ Sum these values for each category (owner, group, others).
■ Example: Owner (rwx=7), Group (rx=5), Others (r=4)
chmod 754 file.txt
■ Example: Make a script executable only by the owner:
chmod 700 script.sh
● Change file owner and group: Changes the user and/or group ownership of a file or
directory.
chown [new_owner]:[new_group] [file_name]
○ To change only the owner:
chown new_owner file.txt
○ To change only the group:
chown :new_group file.txt
○ To change owner and group recursively for a directory:
chown -R new_owner:new_group directory/
Process Management
These commands help you view, control, and terminate running processes.
● List running processes: Shows currently running processes.
ps
○ To show all processes:
ps aux
○ To show processes in a tree format:
pstree
● Display real-time process information: Provides a dynamic, real-time view of running
processes, CPU usage, memory, etc.
top
○ An enhanced version of top with better user interface:
htop
(May need to be installed: sudo apt install htop or sudo yum install htop)
● Terminate a process: Sends a signal to a process to stop it.
kill [PID]
○ To forcefully terminate a process (useful for unresponsive processes):
kill -9 [PID]
○ To kill processes by name:
killall [process_name]
● Run a command in the background: Executes a command in the background, allowing
you to continue using the terminal.
[command] &
○ Example:
sleep 60 &
● List background jobs: Shows jobs currently running in the background.
jobs
● Bring a background job to the foreground: Brings a suspended or background job to
the foreground.
fg [%job_number]
● Suspend a foreground process: Stops a running foreground process (you can then
use bg to put it in the background or fg to resume it).
Ctrl + Z
Text Processing and Data Filtering
These commands are powerful tools for manipulating and filtering text data from files or
command output.
● Display file content: Shows the entire content of a file.
cat [file_name]
○ To concatenate multiple files and display their content:
cat file1.txt file2.txt
● Display file content page by page: Allows you to view large files one screen at a time,
with scrolling capabilities.
less [file_name]
○ To view the file and search for text, type / followed by the search term and press
Enter.
● Display the beginning of a file: Shows the first 10 lines of a file by default.
head [file_name]
○ To display the first N lines:
head -n [N] [file_name]
● Display the end of a file: Shows the last 10 lines of a file by default.
tail [file_name]
○ To display the last N lines:
tail -n [N] [file_name]
○ To continuously display new lines as they are added to a file (useful for logs):
tail -f [file_name]
● Search for patterns in files: Filters text based on a specified pattern or string.
grep [pattern] [file_name]
○ To search case-insensitively:
grep -i [pattern] [file_name]
○ To search recursively in directories:
grep -r [pattern] [directory_name]
○ To count the number of matches:
grep -c [pattern] [file_name]
● Sort lines of text files: Sorts lines alphabetically or numerically.
sort [file_name]
○ To sort in reverse order:
sort -r [file_name]
○ To sort numerically:
sort -n [file_name]
● Report or omit duplicate lines: Filters out duplicate lines from a sorted file.
uniq [file_name]
○ Often used with sort and a pipe:
sort file.txt | uniq
○ To count occurrences of each unique line:
sort file.txt | uniq -c
● Count lines, words, and characters: Displays the number of lines, words, and
characters in a file.
wc [file_name]
○ To count only lines:
wc -l [file_name]
○ To count only words:
wc -w [file_name]
Networking
These commands are used for checking network connectivity, viewing network configurations,
and transferring data.
● Test network connectivity: Sends ICMP echo requests to a host to check reachability.
ping [hostname_or_IP]
○ Example:
ping google.com
(Press Ctrl + C to stop)
● Display network configuration: Shows network interfaces, IP addresses, routing
tables, and network statistics.
ip a
○ To show routing table:
ip r
○ (Older command, still widely used but ip is preferred):
ifconfig
(May need to be installed: sudo apt install net-tools or sudo yum install net-tools)
● Display network connections, routing tables, interface statistics: Shows active
network connections (incoming and outgoing).
netstat
○ To show all listening ports and established connections numerically:
netstat -tulnp
(Requires sudo for process names)
● Resolve hostnames to IP addresses: Performs a DNS lookup.
nslookup [hostname]
○ Example:
nslookup example.com
● Download files from the internet: A versatile tool for transferring data with URL
syntax.
wget [URL]
○ Example:
wget https://example.com/file.zip
● Transfer data with URL syntax: A command-line tool and library for transferring data
with various protocols.
curl [URL]
○ Example:
curl https://api.github.com/users/octocat
○ To save the output to a file:
curl -o output.html https://example.com
Disk Usage & Monitoring
These commands help you monitor disk space, file system usage, and system resources.
● Display disk space usage: Shows the amount of free and used disk space on mounted
file systems.
df
○ To display sizes in human-readable format (e.g., KB, MB, GB):
df -h
● Estimate file space usage: Summarizes disk usage of files and directories.
du [directory_name]
○ To summarize disk usage for a directory in human-readable format:
du -sh [directory_name]
○ To show disk usage for all files and subdirectories, then sort by size:
du -h | sort -rh
● Display free and used memory: Shows information about physical and swap memory
usage.
free
○ To display in human-readable format:
free -h
● Display system uptime and load average: Shows how long the system has been
running and its average load over 1, 5, and 15 minutes.
uptime
● Display currently logged-in users: Shows who is logged into the system.
who
● Display current user and group IDs: Shows the effective user ID, group ID, and
supplementary groups for the current user.
id