This guide covers the most commonly used Linux commands along with their usage, tips, and examples. Whether you're a beginner or an experienced user, these commands are essential for navigating and managing a Linux system.
1. Navigation and File Management Commands
1.1 ls - List Directory Contents
The ls command lists the contents of a directory.
Usage:
ls [options] [directory]
Common Options:
-l: Long listing format.-a: Include hidden files (those starting with.).-h: Human-readable file sizes.
Examples:
# List files in the current directory
ls
# Long format list including hidden files
ls -alh
# List files in a specific directory
ls /var/log
1.2 cd - Change Directory
The cd command changes the current working directory.
Usage:
cd [directory]
Examples:
# Go to the home directory
cd ~
# Go up one directory level
cd ..
# Change to the /etc directory
cd /etc
1.3 pwd - Print Working Directory
The pwd command displays the current working directory.
Usage:
pwd
Example:
# Display the current directory path
pwd
1.4 mkdir - Make Directory
The mkdir command creates a new directory.
Usage:
mkdir [options] directory_name
Common Options:
-p: Create parent directories as needed.
Examples:
# Create a directory named 'projects'
mkdir projects
# Create nested directories 'projects/java'
mkdir -p projects/java
1.5 rm - Remove Files or Directories
The rm command removes files or directories.
Usage:
rm [options] file_or_directory
Common Options:
-r: Recursively remove directories and their contents.-f: Force removal without prompt.
Examples:
# Remove a file
rm example.txt
# Remove a directory and its contents
rm -r old_projects
# Force remove without confirmation
rm -rf temp_files
1.6 cp - Copy Files or Directories
The cp command copies files or directories.
Usage:
cp [options] source destination
Common Options:
-r: Recursively copy directories.-i: Prompt before overwrite.
Examples:
# Copy a file
cp file1.txt file2.txt
# Copy a directory recursively
cp -r dir1 dir2
# Copy with confirmation before overwrite
cp -i file1.txt file2.txt
1.7 mv - Move or Rename Files and Directories
The mv command moves or renames files and directories.
Usage:
mv [options] source destination
Examples:
# Rename a file
mv oldname.txt newname.txt
# Move a file to another directory
mv file.txt /home/user/documents/
# Move multiple files to a directory
mv file1.txt file2.txt /home/user/documents/
2. File Viewing and Editing Commands
2.1 cat - Concatenate and Display Files
The cat command displays the contents of a file.
Usage:
cat [options] filename
Examples:
# Display the contents of a file
cat file.txt
# Display line numbers
cat -n file.txt
2.2 less - View File Contents One Page at a Time
The less command allows you to view file contents one page at a time.
Usage:
less filename
Example:
# View a large file
less bigfile.log
Tip: Use the arrow keys to navigate. Press q to quit.
2.3 head and tail - View Beginning or End of Files
The head command displays the first few lines of a file, while tail displays the last few lines.
Usage:
head [options] filename
tail [options] filename
Examples:
# Display the first 10 lines (default)
head file.txt
# Display the last 20 lines
tail -n 20 file.txt
# Monitor a file in real-time
tail -f /var/log/syslog
2.4 nano - Simple Text Editor
nano is a simple command-line text editor.
Usage:
nano filename
Example:
# Edit or create a file
nano notes.txt
Tip: Use Ctrl + X to exit, Ctrl + O to save.
3. System Information and Management Commands
3.1 sudo - Execute Commands as Root User
The sudo command allows permitted users to execute a command as the superuser.
Usage:
sudo command
Example:
# Update package lists (requires root privileges)
sudo apt update
3.2 apt / yum / dnf - Package Managers
These commands manage software packages on your system.
Usage:
# Debian/Ubuntu systems
sudo apt update
sudo apt install package_name
# CentOS/RHEL systems
sudo yum update
sudo yum install package_name
# Fedora/CentOS 8+
sudo dnf update
sudo dnf install package_name
3.3 top - Display Active Processes
The top command provides a dynamic real-time view of running processes.
Usage:
top
Tip: Press q to quit, h for help.
3.4 ps - Report Process Status
The ps command displays information about active processes.
Usage:
ps [options]
Examples:
# List processes for the current user
ps
# List all processes
ps aux
# Search for a specific process
ps aux | grep apache2
3.5 kill - Terminate Processes
The kill command sends a signal to a process.
Usage:
kill [signal] PID
Examples:
# List processes
ps aux
# Terminate a process with PID 1234
kill 1234
# Force kill a process
kill -9 1234
3.6 df - Report Disk Space Usage
The df command displays disk space usage of file systems.
Usage:
df [options]
Examples:
# Display disk usage in human-readable format
df -h
# Display disk usage of a specific file system
df -h /dev/sda1
3.7 du - Estimate File Space Usage
The du command estimates file space usage.
Usage:
du [options] [file_or_directory]
Examples:
# Display sizes of directories and files
du -h
# Display total size of a directory
du -sh /var/log
4. Permissions and Ownership Commands
4.1 chmod - Change File Permissions
The chmod command changes file access permissions.
Usage:
chmod [options] mode file
Examples:
# Give the owner read, write, and execute permissions
chmod u+rwx script.sh
# Set permissions using numeric notation
chmod 755 script.sh
# Recursively change permissions
chmod -R 644 /var/www/html
4.2 chown - Change File Owner and Group
The chown command changes the owner and group of a file or directory.
Usage:
chown [options] owner[:group] file
Examples:
# Change the owner of a file to 'user'
sudo chown user file.txt
# Change the owner and group
sudo chown user:group file.txt
# Recursively change ownership
sudo chown -R user:group /var/www/html
5. Networking Commands
5.1 ping - Test Network Connectivity
The ping command checks network connectivity between your system and another host.
Usage:
ping [options] destination
Examples:
# Ping a host
ping example.com
# Ping with a specific count
ping -c 4 google.com
5.2 ifconfig / ip - Configure Network Interfaces
The ifconfig command configures network interfaces (deprecated in favor of ip command).
Usage:
# Display network interfaces (ifconfig)
ifconfig
# Display network interfaces (ip)
ip addr show
5.3 ssh - Secure Shell
The ssh command allows you to connect to a remote machine securely.
Usage:
ssh [user@]hostname [command]
Examples:
# Connect to a remote server
ssh user@example.com
# Run a command on a remote server
ssh user@example.com 'ls -la'
5.4 scp - Secure Copy
The scp command copies files between hosts over a secure connection.
Usage:
scp [options] source destination
Examples:
# Copy a file to a remote server
scp file.txt user@example.com:/home/user/
# Copy a file from a remote server
scp user@example.com:/home/user/file.txt /local/directory/
5.5 wget - Download Files from the Web
The wget command downloads files from the web.
Usage:
wget [options] url
Examples:
# Download a file
wget http://example.com/file.zip
# Download in the background
wget -b http://example.com/file.zip
6. Search and Processing Commands
6.1 grep - Search Text Using Patterns
The grep command searches for patterns in files.
Usage:
grep [options] pattern [file]
Examples:
# Search for a string in a file
grep "error" logfile.log
# Search recursively in all files in a directory
grep -r "TODO" /path/to/code
# Ignore case sensitivity
grep -i "warning" logfile.log
6.2 find - Search for Files and Directories
The find command searches for files and directories based on criteria.
Usage:
find [path] [options] [expression]
Examples:
# Find files named 'notes.txt'
find / -name 'notes.txt'
# Find files larger than 100MB
find / -size +100M
# Execute a command on found files
find . -type f -name "*.tmp" -exec rm {} \;
6.3 awk - Pattern Scanning and Processing Language
The awk command processes text files and data streams.
Usage:
awk 'pattern { action }' file
Example:
# Print the second column of a file
awk '{ print $2 }' file.txt
# Sum numbers in the first column
awk '{ total += $1 } END { print total }' numbers.txt
6.4 sed - Stream Editor for Filtering and Transforming Text
The sed command is used for text manipulation.
Usage:
sed [options] 'command' file
Examples:
# Replace 'old' with 'new' in a file
sed 's/old/new/g' file.txt
# Delete lines containing 'error'
sed '/error/d' file.txt
7. Compression and Archiving Commands
7.1 tar - Archive Files
The tar command creates and extracts archive files.
Usage:
tar [options] archive_name.tar file_or_directory
Examples:
# Create a tar archive
tar -cvf archive.tar /path/to/directory
# Extract a tar archive
tar -xvf archive.tar
# Create a compressed tar.gz archive
tar -czvf archive.tar.gz /path/to/directory
# Extract a tar.gz archive
tar -xzvf archive.tar.gz
7.2 zip and unzip - Compress and Decompress ZIP Files
The zip command compresses files into a ZIP archive, and unzip extracts them.
Examples:
# Compress files into a ZIP archive
zip archive.zip file1.txt file2.txt
# Extract a ZIP archive
unzip archive.zip
8. User Management Commands
8.1 adduser / useradd - Create a New User
The adduser and useradd commands create new user accounts.
Examples:
# Create a new user with home directory
sudo adduser username
# Add a user without creating home directory
sudo useradd username
8.2 passwd - Change User Password
The passwd command changes a user's password.
Usage:
passwd [username]
Examples:
# Change your own password
passwd
# Change another user's password
sudo passwd username
8.3 who and w - Show Who Is Logged In
The who command displays users currently logged in, while w provides more details.
Examples:
# List logged-in users
who
# Detailed information about users
w
9. Miscellaneous Commands
9.1 history - Show Command History
The history command displays the list of commands you've previously entered.
Usage:
history
Example:
# Show command history
history
# Run a command from history
!55 # Executes command number 55
9.2 alias - Create Aliases for Commands
The alias command creates shortcuts for commands.
Usage:
alias name='command'
Examples:
# Create an alias for 'ls -al'
alias ll='ls -al'
# Make alias permanent (add to ~/.bashrc)
echo "alias ll='ls -al'" >> ~/.bashrc
9.3 clear - Clear the Terminal Screen
The clear command clears the terminal screen.
Usage:
clear
Example:
# Clear the screen
clear
Conclusion
This guide covered the most commonly used Linux commands essential for daily tasks. Mastering these commands will enhance your efficiency and proficiency in managing Linux systems.
