Linux, the powerhouse of operating systems, is renowned for its security and flexibility, making it a favorite among developers, system administrators, and tech enthusiasts. Here, we explore 50 common Linux commands that are indispensable for daily operations, providing a detailed explanation and three practical examples for each to enhance your command-line proficiency.
1. ls, List Directory Contents
Explanation: The ls command is used to view the contents of a directory.
-
Example 1:
ls, Lists all files and directories in the current directory. -
Example 2:
ls -l, Lists files with detailed information like permissions, number of links, owner, group, size, and time of last modification. -
Example 3:
ls -a, Lists all entries including hidden files starting with ‘.’.
2. cd, Change Directory
Explanation: cd is used to change the current directory.
-
Example 1:
cd /home, Changes the directory to/home. -
Example 2:
cd .., Moves one directory up. -
Example 3:
cd, Takes you to the home directory.
3. pwd, Print Working Directory
Explanation: Displays the path of the current directory.
-
Example 1:
pwd, Outputs the full path of the current directory. -
Example 2:
pwd, Same as above, aspwddoes not vary much in usage. -
Example 3:
pwd, Again, outputs the current directory path.
4. mkdir, Make Directory
Explanation: Creates a new directory.
-
Example 1:
mkdir new_folder, Creates a new directory namednew_folder. -
Example 2:
mkdir -p path/to/new_folder, Creates all non-existent parent directories leading up to the specified directory. -
Example 3:
mkdir dir1 dir2 dir3, Creates multiple directories at once.
5. rmdir, Remove Directory
Explanation: Deletes empty directories.
-
Example 1:
rmdir old_folder, Removes an empty directory namedold_folder. -
Example 2:
rmdir dir1 dir2 dir3, Removes multiple empty directories. -
Example 3:
rmdir -p path/to/empty_folder, Removes both the directory and its empty parents.
6. rm, Remove Files or Directories
Explanation: Deletes files or directories.
-
Example 1:
rm file.txt, Deletesfile.txt. -
Example 2:
rm -r folder, Recursively deletes a directory and its contents. -
Example 3:
rm -f file.txt, Forcefully removesfile.txt, ignoring non-existent files and suppressing prompts.
7. cp, Copy Files or Directories
Explanation: Copies files or directories.
-
Example 1:
cp file1.txt file2.txt, Copiesfile1.txttofile2.txt. -
Example 2:
cp -r dir1 dir2, Recursively copiesdir1and its contents todir2. -
Example 3:
cp file.txt /path/to/destination/, Copiesfile.txtto the specified directory.
8. mv, Move or Rename Files or Directories
Explanation: Moves or renames files or directories.
-
Example 1:
mv file1.txt new_file.txt, Renamesfile1.txttonew_file.txt. -
Example 2:
mv file.txt /path/to/destination/, Movesfile.txtto a different directory. -
Example 3:
mv dir1 dir2, Movesdir1intodir2or renames it ifdir2does not exist.
9. chmod, Change File Modes or Access Permissions
Explanation: Modifies the access permissions of files or directories.
-
Example 1:
chmod 755 file.txt, Sets the permissions offile.txtto 755. -
Example 2:
chmod +x script.sh, Makesscript.shexecutable. -
Example 3:
chmod -R 644 folder/, Recursively sets the permissions of all files infolderto 644.
10. chown, Change File Owner and Group
Explanation: Changes the owner and/or group of files or directories.
-
Example 1:
chown user file.txt, Changes the owner offile.txttouser. -
Example 2:
chown user:group file.txt, Changes both the owner and group offile.txt. -
Example 3:
chown -R user:group folder, Recursively changes the owner and group offolderand its contents.
11. touch, Create Empty Files or Modify Timestamps
Explanation: Used to create a new empty file or update the timestamps of existing files.
-
Example 1:
touch newfile.txt, Creates an empty file namednewfile.txt. -
Example 2:
touch -a file.txt, Updates the access time offile.txt. -
Example 3:
touch -m file.txt, Updates the modification time offile.txt.
12. cat, Concatenate and Display Files
Explanation: Displays the contents of files and can concatenate multiple files.
-
Example 1:
cat file.txt, Displays the content offile.txt. -
Example 2:
cat file1.txt file2.txt > combined.txt, Concatenatesfile1.txtandfile2.txtintocombined.txt. -
Example 3:
cat -n file.txt, Displays the content offile.txtwith line numbers.
13. grep, Search Text Using Patterns
Explanation: Searches for patterns in text using regular expressions.
-
Example 1:
grep "pattern" file.txt, Searches for “pattern” infile.txt. -
Example 2:
grep -i "pattern" file.txt, Case-insensitive search for “pattern”. -
Example 3:
grep -r "pattern" ., Recursively search for “pattern” in all files in the current directory.
14. find, Search for Files in a Directory Hierarchy
Explanation: Locates files in a directory hierarchy based on conditions specified.
-
Example 1:
find /path -name "file.txt", Finds all files namedfile.txtunder/path. -
Example 2:
find /path -type d, Finds all directories under/path. -
Example 3:
find /path -size +100M, Finds files larger than 100MB in/path.
15. df, Report File System Disk Space Usage
Explanation: Displays the amount of disk space used and available on file systems.
-
Example 1:
df, Displays disk space usage of all mounted file systems. -
Example 2:
df -h, Displays disk space in human-readable format. -
Example 3:
df -i, Displays inode information.
16. du, Estimate File Space Usage
Explanation: Estimates and displays the disk space used by files and directories.
-
Example 1:
du, Shows the disk usage of the current directory. -
Example 2:
du -h, Shows disk usage in human-readable format. -
Example 3:
du -sh *, Shows the size of all files and directories in the current directory in a summarized format.
17. ps, Report a Snapshot of Current Processes
Explanation: Provides information about currently running processes.
-
Example 1:
ps, Displays your processes. -
Example 2:
ps -aux, Displays all running processes. -
Example 3:
ps -ef, Displays a full format listing.
18. kill, Send a Signal to a Process
Explanation: Used to send signals to processes, typically for stopping the process.
-
Example 1:
kill 1234, Sends the SIGTERM signal to process with PID 1234. -
Example 2:
kill -9 1234, Sends the SIGKILL signal to forcefully stop the process with PID 1234. -
Example 3:
kill -SIGINT 1234, Sends the SIGINT signal to process with PID 1234.
19. top, Task Manager
Explanation: Displays real-time view of running system processes.
-
Example 1:
top, Starts top with the default settings. -
Example 2:
top -u username, Displays processes owned byusername. -
Example 3:
top -i, Starts top with idle processes omitted.
20. man, Interface to the System Reference Manuals
Explanation: Provides detailed documentation about commands and other aspects of the Linux system.
-
Example 1:
man ls, Displays the manual page for thelscommand. -
Example 2:
man grep, Displays the manual page for thegrepcommand. -
Example 3:
man man, Displays the manual page for themancommand itself.
21. sudo, Execute a Command as Another User
Explanation: Allows a permitted user to execute a command as the superuser or another user.
-
Example 1:
sudo apt update, Runs the update command as superuser. -
Example 2:
sudo -u user command, Runscommandasuser. -
Example 3:
sudo visudo, Edits the sudoers file to modify permissions.
22. apt-get, APT Package Handling Utility
Explanation: Used on Debian-based systems to handle packages (install, update, remove).
-
Example 1:
sudo apt-get update, Updates package lists. -
Example 2:
sudo apt-get install nginx, Installs the nginx package. -
Example 3:
sudo apt-get remove nginx, Removes the nginx package.
23. wget, Non-interactive Network Downloader
Explanation: Downloads files from the web.
-
Example 1:
wget https://example.com/file.zip, Downloadsfile.zipfrom the specified URL. -
Example 2:
wget -O filename.zip https://example.com/file.zip, Downloads and renames the file tofilename.zip. -
Example 3:
wget -c https://example.com/file.zip, Continues a partially downloaded file.
24. curl, Transfer Data from or to a Server
Explanation: Used to transfer data from or to a server using various protocols.
-
Example 1:
curl https://example.com, Fetches the content of a webpage. -
Example 2:
curl -o local.html https://example.com, Saves the webpage tolocal.html. -
Example 3:
curl -d "param1=value1¶m2=value2" -X POST https://example.com, Sends POST data to the server.
25. echo, Display a Line of Text
Explanation: Outputs the strings it is being passed as arguments.
-
Example 1:
echo "Hello, world!", Prints “Hello, world!” to the terminal. -
Example 2:
echo $HOME, Displays the path of the home directory. -
Example 3:
echo -n "Hello, world!", Prints “Hello, world!” without a trailing newline.
26. tail, Output the Last Part of Files
Explanation: Displays the last part of a file to the terminal.
-
Example 1:
tail file.txt, Displays the last 10 lines offile.txt. -
Example 2:
tail -f file.txt, Continuously monitorsfile.txtfor new lines and displays them. -
Example 3:
tail -n 20 file.txt, Displays the last 20 lines offile.txt.
27. head, Output the First Part of Files
Explanation: Displays the first part of a file to the terminal.
-
Example 1:
head file.txt, Displays the first 10 lines offile.txt. -
Example 2:
head -n 5 file.txt, Displays the first 5 lines offile.txt. -
Example 3:
head -c 100 file.txt, Displays the first 100 characters offile.txt.
28. tar, Archive Utility
Explanation: Used to store multiple files in a single archive file and to extract files from the archive.
-
Example 1:
tar -cvf archive.tar files/, Creates an archive of thefiles/directory. -
Example 2:
tar -xvf archive.tar, Extracts the contents ofarchive.tar. -
Example 3:
tar -czvf archive.tar.gz files/, Creates a compressed archive using gzip.
29. zip, Package and Compress Files
Explanation: Compresses files and directories into a zip file.
-
Example 1:
zip archive.zip file1 file2, Compressesfile1andfile2intoarchive.zip. -
Example 2:
zip -r archive.zip folder/, Recursively compressesfolder/and its contents intoarchive.zip. -
Example 3:
zip -e secure.zip file.txt, Creates a password-protected zip file.
30. unzip, Extract Files from ZIP Archives
Explanation: Extracts files from ZIP archives.
-
Example 1:
unzip archive.zip, Extracts files fromarchive.zip. -
Example 2:
unzip archive.zip -d destination/, Extracts files into thedestination/directory. -
Example 3:
unzip -l archive.zip, Lists the contents ofarchive.zipwithout extracting.
31. alias, Create Aliases for Commands
Explanation: Allows you to create shortcuts for long commands or frequently used command sequences.
-
Example 1:
alias ll='ls -l', Creates an aliasllforls -l. -
Example 2:
alias rm='rm -i', Makesrmcommand interactive by default. -
Example 3:
unalias ll, Removes the aliasll.
32. diff, Compare Files Line by Line
Explanation: Compares the contents of two files line by line.
-
Example 1:
diff file1.txt file2.txt, Shows differences betweenfile1.txtandfile2.txt. -
Example 2:
diff -u file1.txt file2.txt, Displays differences in a unified format. -
Example 3:
diff -r dir1 dir2, Recursively compares files in two directories.
33. chmod, Change File Permissions
Explanation: Modifies the file access permissions.
-
Example 1:
chmod 755 script.sh, Sets read, write, and execute permissions for the owner, and read and execute permissions for others. -
Example 2:
chmod +x script.sh, Adds execute permission toscript.sh. -
Example 3:
chmod -R 644 folder/, Sets read and write permissions for the owner, and read for others on all files in the folder.
34. chgrp, Change Group Ownership
Explanation: Changes the group ownership of a file or directory.
-
Example 1:
chgrp admin file.txt, Changes the group ownership offile.txtto admin. -
Example 2:
chgrp -R admin folder, Recursively changes the group ownership offolderand its contents to admin. -
Example 3:
chgrp users file.txt, Changes the group ownership offile.txtto users.
35. hostname, Show or Set the System’s Host Name
Explanation: Displays or sets the hostname of the system.
-
Example 1:
hostname, Displays the current hostname. -
Example 2:
hostname new-hostname, Sets the system’s hostname tonew-hostname. -
Example 3:
hostname -f, Displays the fully qualified domain name (FQDN).
36. ping, Check Network Connectivity
Explanation: Sends ICMP ECHO_REQUEST packets to network hosts.
-
Example 1:
ping google.com, Checks connectivity to google.com. -
Example 2:
ping -c 4 google.com, Sends 4 packets to google.com and then stops. -
Example 3:
ping -i 2 google.com, Sends a packet every 2 seconds to google.com.
37. netstat, Network Statistics
Explanation: Displays network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.
-
Example 1:
netstat -a, Shows all active connections and listening ports. -
Example 2:
netstat -r, Displays the kernel routing table. -
Example 3:
netstat -tulpen, Shows a detailed table of all current connections.
38. scp, Secure Copy
Explanation: Copies files between hosts on a network using SSH for data transfer.
-
Example 1:
scp file.txt user@remote:/path, Copiesfile.txtto a remote host. -
Example 2:
scp user@remote:/path/file.txt ./, Copies a file from a remote host to the local machine. -
Example 3:
scp -r folder user@remote:/path, Recursively copies a folder to a remote host.
39. ssh, Secure Shell
Explanation: Connects to a remote machine securely.
-
Example 1:
ssh user@host, Logs intohostasuser. -
Example 2:
ssh -p 2222 user@host, Connects tohoston port 2222. -
Example 3:
ssh -i keyfile user@host, Uses a private key file for the connection.
40. rsync, Remote Sync
Explanation: Efficiently transfers and synchronizes files across computer systems.
-
Example 1:
rsync -a /local/dir remote:/remote/dir, Synchronizes directories between local and remote systems. -
Example 2:
rsync -avz --delete /local/dir remote:/remote/dir, Synchronizes while deleting extraneous files from the destination. -
Example 3:
rsync -a --progress /local/dir remote:/remote/dir, Synchronizes with progress output.
41. uname, Print System Information
Explanation: Displays important system information.
-
Example 1:
uname -a, Displays all system information. -
Example 2:
uname -r, Displays the kernel release. -
Example 3:
uname -m, Displays the machine hardware name.
42. who, Show Who is Logged On
Explanation: Displays who is currently logged on to the system.
-
Example 1:
who, Lists logged-in users. -
Example 2:
who -H, Lists logged-in users with column headers. -
Example 3:
who am i, Displays your current login information.
43. uptime, Tell How Long the System Has Been Running
Explanation: Shows how long the system has been running along with the number of users and the system load averages.
-
Example 1:
uptime, Displays the current uptime. -
Example 2:
uptime -p, Shows uptime in a pretty format. -
Example 3:
uptime --since, Shows the time since the system was last booted.
44. free, Display Memory Usage
Explanation: Shows the total amount of free and used physical and swap memory in the system.
-
Example 1:
free, Displays memory usage. -
Example 2:
free -m, Displays memory usage in megabytes. -
Example 3:
free -h, Displays memory usage in human-readable form.
45. watch, Execute a Program Periodically
Explanation: Executes a program periodically, showing output fullscreen.
-
Example 1:
watch -n 1 date, Displays the current time every second. -
Example 2:
watch -d ls -l, Highlights changes between successive updates ofls -l. -
Example 3:
watch -n 5 'df -h', Checks disk space usage every 5 seconds.
46. crontab, Schedule Periodic Background Jobs
Explanation: Manages cron jobs for time-based job scheduling.
-
Example 1:
crontab -l, Lists cron jobs for the current user. -
Example 2:
crontab -e, Edits the current user’s crontab. -
Example 3:
crontab -r, Removes the current user’s crontab.
47. date, Display or Set the System Date and Time
Explanation: Displays or sets the system’s date and time.
-
Example 1:
date, Displays the current date and time. -
Example 2:
date +%Y-%m-%d, Displays the date in YYYY-MM-DD format. -
Example 3:
sudo date --set="2024-01-01 12:00:00", Sets the system date and time.
48. env, Display or Modify the Environment
Explanation: Displays, sets, or removes environment variables.
-
Example 1:
env, Lists all environment variables. -
Example 2:
env PATH, Displays the value of the PATH variable. -
Example 3:
env -i bash, Starts a new instance of bash with a clean environment.
49. mount, Mount a File System
Explanation: Attaches a file system into the file system hierarchy.
-
Example 1:
mount /dev/sda1 /mnt, Mounts the device/dev/sda1at/mnt. -
Example 2:
mount -o remount,rw /, Remounts the root file system as read-write. -
Example 3:
mount -t vfat /dev/sdb1 /mnt, Mounts a FAT32 device by explicitly specifying the filesystem type.
50. mtr, My Traceroute
Explanation: Combines the functionality of traceroute and ping to provide one network diagnostic tool that covers both jobs.
-
Example 1:
mtr google.com, Initiates a real-time traceroute togoogle.com, showing both the route packets take to the destination and the response times. -
Example 2:
mtr -r -c 10 example.com, Performs a traceroute toexample.com, sending 10 packets to each hop and then providing a report. The-roption stopsmtrfrom running in real-time. -
Example 3:
mtr -n -i 2 192.168.1.1, Traces the route to192.168.1.1without resolving IP addresses to hostnames (-n) and sets the ping interval to 2 seconds (-i 2).
Some extras!
-
ncdu: NCurses Disk Usage Explanation: A disk usage analyzer with an ncurses interface, useful for tracking down space hogs. -
Example 1:
ncdu /home, Analyzes disk usage in the/homedirectory, providing a navigable ncurses interface to explore the directories and files. -
Example 2:
ncdu -x /, Runsncduon the root directory, but does not cross filesystem boundaries due to the-xoption. -
Example 3:
ncdu -o report.txt /var, Analyzes the/vardirectory and outputs the results toreport.txtfor later review. -
iftop: Display Bandwidth Usage Explanation: Displays bandwidth usage on an interface by host. -
Example 1:
sudo iftop, Displays the bandwidth usage on the default network interface. -
Example 2:
sudo iftop -i eth0, Monitors bandwidth usage on theeth0interface. -
Example 3:
sudo iftop -P, Shows ports in addition to hosts, which can be useful for identifying specific applications. -
iotop: Monitor Disk I/O Explanation: Monitors disk I/O usage information output by the Linux kernel. -
Example 1:
sudo iotop, Displays a table of current I/O usage by processes or threads on the system. -
Example 2:
sudo iotop -o, Shows only processes or threads which are doing I/O. -
Example 3:
sudo iotop -P, Displays processes and threads separately. -
nmap: Network Mapper Explanation: A security scanner used to discover hosts and services on a computer network. -
Example 1:
nmap -sP 192.168.1.0/24, Scans the local network for hosts that are up without performing port scanning. -
Example 2:
nmap -p 22,80,443 google.com, Scansgoogle.comfor open ports 22 (SSH), 80 (HTTP), and 443 (HTTPS). -
Example 3:
nmap -A -T4 scanme.nmap.org, Performs an aggressive scan with faster execution, including OS detection, version detection, script scanning, and traceroute. -
htop: Interactive Process Viewer Explanation: An interactive process viewer for Unix systems. -
Example 1:
htop, Opens an interactive process viewer that displays running processes and system resource usage. -
Example 2:
htop -u username, Displays processes owned byusername. -
Example 3:
htop -t, Displays processes in a tree view, which can help visualize parent-child relationships. -
lsof: List Open Files Explanation: Lists open files belonging to all active processes. -
Example 1:
sudo lsof -i, Lists all network connections. -
Example 2:
lsof /usr/lib, Lists all processes that have files open in/usr/lib. -
Example 3:
lsof -u username, Lists files opened by the userusername. -
tmux: Terminal Multiplexer Explanation: Allows multiple terminal sessions to be accessed simultaneously in a single window. -
Example 1:
tmux, Starts a new tmux session. -
Example 2:
tmux new -s mysession, Starts a new tmux session namedmysession. -
Example 3:
tmux attach -t mysession, Attaches to an existing tmux session namedmysession. -
dig: DNS Lookup Tool Explanation: A command-line tool for querying Domain Name System (DNS) servers. -
Example 1:
dig example.com, Queries the DNS for records associated withexample.com. -
Example 2:
dig +trace example.com, Traces the path of the DNS query forexample.comfrom the root DNS servers down to the authoritative servers. -
Example 3:
dig example.com MX, Queries for the Mail Exchange (MX) records ofexample.com.