Linux, an open-source operating system, has become a cornerstone in the tech world, powering everything from servers and supercomputers to smartphones and home appliances. Its flexibility, security, and robustness make it a preferred choice for developers, system administrators, and tech enthusiasts alike. One of the most powerful aspects of Linux is its command-line interface (CLI), which allows users to interact directly with the system through text-based commands.
The CLI is not just a tool for the tech-savvy; it’s an essential skill for anyone looking to harness the full potential of Linux. From managing files and processes to configuring networks and automating tasks, the command line offers unparalleled control and efficiency.
This article aims to provide a comprehensive guide to essential Linux commands, catering to both beginners and advanced users. Whether you’re just starting your Linux journey or looking to deepen your command-line expertise, this guide will equip you with the knowledge you need to navigate and manage your Linux system effectively.
Section 1: Basic Commands
1.1. Navigating the Filesystem
Navigating the filesystem is fundamental to using Linux. Here are some basic commands to get you started:
pwd
- Print Working Directory
The pwd
command displays the current directory you’re working in. This is particularly useful when you need to confirm your location within the filesystem.
$ pwd
/home/username
ls
- List Directory Contents
The ls
command lists the contents of a directory. It has several options to enhance its functionality:
-l
: Long listing format, showing detailed information about each file.-a
: Include hidden files (those starting with a dot).-h
: Human-readable format, making file sizes easier to read.-R
: Recursively list subdirectories.
$ ls
Desktop Documents Downloads Music Pictures Videos
$ ls -l
total 0
drwxr-xr-x 2 username username 4096 Jan 1 12:34 Desktop
drwxr-xr-x 2 username username 4096 Jan 1 12:34 Documents
$ ls -a
. .. .bashrc .profile Desktop Documents
$ ls -h
total 0
drwxr-xr-x 2 username username 4.0K Jan 1 12:34 Desktop
drwxr-xr-x 2 username username 4.0K Jan 1 12:34 Documents
$ ls -R
.:
Desktop Documents
./Desktop:
file1.txt
./Documents:
file2.txt
cd
- Change Directory
The cd
command changes the current directory. Here are some common usages:
cd ..
: Move up one directory level.cd /
: Move to the root directory.cd ~/Documents
: Move to the Documents directory in your home folder (the tilde~
represents the home directory).
$ cd ..
$ pwd
/home
$ cd /var/log
$ pwd
/var/log
$ cd ~/Documents
$ pwd
/home/username/Documents
1.2. File Operations
Managing files is a core task in any operating system. Here are some essential commands for file operations:
touch
- Create an Empty File
The touch
command creates an empty file or updates the timestamp of an existing file.
$ touch newfile.txt
$ ls -l newfile.txt
-rw-r--r-- 1 username username 0 Jan 1 12:34 newfile.txt
cp
- Copy Files and Directories
The cp
command copies files or directories. Key options include:
-r
: Recursively copy directories.-i
: Prompt before overwriting files.-v
: Verbose mode, showing files being copied.
$ cp file1.txt file2.txt
$ cp -r dir1/ dir2/
$ cp -i file1.txt file2.txt
$ cp -v file1.txt file2.txt
mv
- Move or Rename Files and Directories
The mv
command moves or renames files and directories.
$ mv file1.txt file2.txt
$ mv file.txt /path/to/destination/
rm
- Remove Files and Directories
The rm
command removes files or directories. Important options include:
-r
: Recursively remove directories.-f
: Force removal without prompting.
$ rm file1.txt
$ rm -rf directory/
1.3. Viewing and Editing Files
Viewing and editing files is a frequent task. Here are some commands to help with that:
cat
- Concatenate and Display Files
The cat
command displays the contents of a file.
$ cat file.txt
Hello, World!
more
- View File Contents Page by Page
The more
command allows you to view file contents one page at a time.
$ more file.txt
less
- View File Contents with Backward Navigation
The less
command is similar to more
but allows backward navigation.
$ less file.txt
head
- Display the First Part of a File
The head
command shows the first 10 lines of a file by default.
$ head file.txt
tail
- Display the Last Part of a File
The tail
command shows the last 10 lines of a file by default. The -f
option allows real-time updates.
$ tail file.txt
$ tail -f logfile.txt
nano
- Simple Text Editor
nano
is a straightforward text editor that’s easy to use.
$ nano file.txt
vim
- Advanced Text Editor
vim
is a powerful text editor with extensive features for advanced users.
$ vim file.txt
With these basic commands, you can navigate the filesystem, manage files, and view or edit their contents efficiently. Mastering these commands is the first step towards becoming proficient in Linux.
Section 2: System Information and Management
Understanding and managing system information is crucial for maintaining a healthy Linux environment. This section covers commands that provide insights into system status, user management, and process control.
2.1. System Information
These commands allow you to gather detailed information about your Linux system.
uname
- Print System Information
The uname
command displays system information. Useful options include:
-a
: Print all system information.-r
: Print the kernel release.-s
: Print the kernel name.
$ uname -a
Linux hostname 5.4.0-42-generic #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
$ uname -r
5.4.0-42-generic
$ uname -s
Linux
top
- Task Manager for Linux
The top
command provides a dynamic, real-time view of running processes, including CPU and memory usage.
$ top
htop
- Interactive Process Viewer
htop
is an enhanced version of top
, offering a more user-friendly interface and additional features.
$ htop
df
- Report File System Disk Space Usage
The df
command reports the amount of disk space used and available on file systems. The -h
option displays the output in a human-readable format.
$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 1.9G 0 1.9G 0% /dev
tmpfs 393M 1.3M 392M 1% /run
/dev/sda1 20G 15G 4.5G 77% /
du
- Estimate File Space Usage
The du
command estimates the file space usage. Key options include:
-h
: Human-readable format.-s
: Summarize the total.
$ du -h
4.0K ./Desktop
8.0K ./Documents
12K .
$ du -sh
12K .
2.2. User and Group Management
Managing users and groups is essential for system security and organization.
whoami
- Display the Current User
The whoami
command shows the username of the current user.
$ whoami
username
id
- Display User and Group Information
The id
command displays user and group information for the current user or a specified user.
$ id
uid=1000(username) gid=1000(username) groups=1000(username),27(sudo)
$ id username
uid=1000(username) gid=1000(username) groups=1000(username),27(sudo)
useradd
- Add a New User
The useradd
command creates a new user. Important options include:
-m
: Create a home directory for the new user.-G
: Specify supplementary groups.
$ sudo useradd -m newuser
$ sudo useradd -m -G sudo newuser
usermod
- Modify a User Account
The usermod
command modifies an existing user account. Key options include:
-aG
: Add the user to supplementary groups.
$ sudo usermod -aG sudo newuser
passwd
- Change User Password
The passwd
command changes the password for a user.
$ sudo passwd newuser
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
groupadd
- Add a New Group
The groupadd
command creates a new group.
$ sudo groupadd newgroup
2.3. Process Management
Managing processes is vital for maintaining system performance and stability.
ps
- Report a Snapshot of Current Processes
The ps
command provides a snapshot of current processes. Useful options include:
-e
: Display all processes.-f
: Full-format listing.-u
: Display processes for a specific user.
$ ps -e
PID TTY TIME CMD
1 ? 00:00:01 systemd
2 ? 00:00:00 kthreadd
$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 12:34 ? 00:00:01 /sbin/init
root 2 0 0 12:34 ? 00:00:00 [kthreadd]
$ ps -u username
UID PID PPID C STIME TTY TIME CMD
username 1234 5678 0 12:34 ? 00:00:00 /usr/bin/bash
kill
- Terminate a Process
The kill
command sends a signal to terminate a process. The most common signal is -9
(SIGKILL), which forces termination.
$ kill -9 PID
pkill
- Terminate Processes by Name
The pkill
command terminates processes based on their name.
$ pkill processname
bg
- Resume a Suspended Job in the Background
The bg
command resumes a suspended job in the background.
$ bg %1
fg
- Bring a Job to the Foreground
The fg
command brings a background job to the foreground.
$ fg %1
These commands provide essential tools for monitoring and managing your Linux system, ensuring you can keep your environment running smoothly and efficiently.
Section 3: Networking Commands
Networking is a critical aspect of any operating system, and Linux provides a robust set of commands to manage and troubleshoot network connections. This section covers essential networking commands for both basic and advanced network management.
3.1. Basic Networking
These commands help you perform fundamental networking tasks such as checking connectivity and configuring network interfaces.
ping
- Send ICMP ECHO_REQUEST to Network Hosts
The ping
command checks the network connectivity between your system and another host. It sends ICMP ECHO_REQUEST packets and waits for a response. The output shows the time it takes for each packet to travel to the host and back.
$ ping google.com
PING google.com (172.217.164.110) 56(84) bytes of data.
64 bytes from 172.217.164.110: icmp_seq=1 ttl=54 time=10.3 ms
64 bytes from 172.217.164.110: icmp_seq=2 ttl=54 time=10.2 ms
ifconfig
- Configure Network Interfaces (deprecated, use ip
instead)
The ifconfig
command is used to configure network interfaces. However, it is deprecated in favor of the ip
command.
$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::a00:27ff:fe4e:66a1 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:4e:66:a1 txqueuelen 1000 (Ethernet)
RX packets 308 bytes 25624 (25.6 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 308 bytes 25624 (25.6 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ip
- Show/Manipulate Routing, Devices, Policy Routing, and Tunnels
The ip
command is the modern replacement for ifconfig
and provides more functionality. Here are some common usages:
ip a
: Show all IP addresses.ip link
: Show network interfaces.
$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:4e:66:a1 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 86397sec preferred_lft 86397sec
inet6 fe80::a00:27ff:fe4e:66a1/64 scope link
valid_lft forever preferred_lft forever
$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:4e:66:a1 brd ff:ff:ff:ff:ff:ff
netstat
- Network Statistics
The netstat
command provides various network-related statistics such as network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. Key options include:
-tuln
: Show TCP and UDP listening ports.
$ netstat -tuln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
udp 0 0 0.0.0.0:68 0.0.0.0:*
ss
- Another Utility to Investigate Socket Statistics
The ss
command is a modern replacement for netstat
and provides more detailed information about network connections. Key options include:
-tuln
: Show TCP and UDP listening ports.
$ ss -tuln
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 *:22 *:*
tcp LISTEN 0 128 :::22 :::*
udp UNCONN 0 0 *:68 *:*
3.2. Data Transfer
These commands are used for transferring data between hosts, whether for copying files or downloading content from the web.
scp
- Secure Copy (Remote File Copy)
The scp
command securely copies files between hosts over SSH. Here are some common usages:
$ scp file.txt user@remote:/path/to/destination/
$ scp user@remote:/path/to/source/file.txt /local/destination/
rsync
- Remote File and Directory Synchronization
The rsync
command synchronizes files and directories between two locations. Key options include:
-a
: Archive mode, which preserves permissions, timestamps, and other attributes.-v
: Verbose mode, showing detailed output.-z
: Compress file data during the transfer.
$ rsync -avz /local/directory/ user@remote:/path/to/destination/
$ rsync -avz user@remote:/path/to/source/ /local/destination/
wget
- Non-Interactive Network Downloader
The wget
command downloads files from the web non-interactively, making it ideal for scripts and automated tasks.
$ wget http://example.com/file.zip
curl
- Transfer Data from or to a Server
The curl
command transfers data to or from a server using various protocols, including HTTP, HTTPS, FTP, and more. Here are some common usages:
- Download a file:
$ curl -O http://example.com/file.zip
- Send a POST request with data:
$ curl -d "param1=value1¶m2=value2" -X POST http://example.com/resource
- Fetch headers from a URL:
$ curl -I http://example.com
These networking commands provide essential tools for managing and troubleshooting network connections, as well as transferring data between hosts. Mastering these commands will enable you to handle a wide range of networking tasks efficiently.
Section 4: Advanced Commands
Advanced commands in Linux allow users to perform complex tasks such as package management, disk management, and system monitoring. These commands are essential for system administrators and power users who need to maintain and optimize their Linux systems.
4.1. Package Management
Package management is crucial for installing, updating, and removing software on your Linux system. Different Linux distributions use different package management systems.
apt-get
- APT Package Handling Utility (Debian-based)
The apt-get
command is used for handling packages in Debian-based distributions like Ubuntu. Common commands include:
update
: Update the package list.upgrade
: Upgrade all installed packages.install
: Install a new package.remove
: Remove an installed package.
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install package_name
$ sudo apt-get remove package_name
yum
- Package Manager for RPM-based Distributions
The yum
command is used for managing packages in RPM-based distributions like CentOS and Red Hat. Common commands include:
install
: Install a new package.update
: Update all installed packages.remove
: Remove an installed package.
$ sudo yum install package_name
$ sudo yum update
$ sudo yum remove package_name
dnf
- Next Generation Package Management (Fedora)
The dnf
command is the modern replacement for yum
in Fedora and other RPM-based distributions. It offers improved performance and better dependency management.
$ sudo dnf install package_name
$ sudo dnf update
$ sudo dnf remove package_name
snap
- Package Management System for Snaps
The snap
command is used to manage snap packages, which are self-contained applications. Common commands include:
install
: Install a snap package.remove
: Remove a snap package.list
: List installed snap packages.
$ sudo snap install package_name
$ sudo snap remove package_name
$ snap list
4.2. Disk Management
Disk management involves creating, modifying, and managing disk partitions and filesystems.
fdisk
- Partition Table Manipulator
The fdisk
command is used to create and manipulate disk partitions. Here is an example of creating a new partition:
$ sudo fdisk /dev/sda
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048): 2048
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +1G
Command (m for help): w
The partition table has been altered!
mkfs
- Build a Linux File System
The mkfs
command is used to create a filesystem on a partition.
$ sudo mkfs.ext4 /dev/sda1
mount
- Mount a File System
The mount
command is used to mount a filesystem.
$ sudo mount /dev/sda1 /mnt
umount
- Unmount a File System
The umount
command is used to unmount a filesystem.
$ sudo umount /mnt
4.3. System Monitoring and Performance
Monitoring system performance is essential for maintaining a healthy Linux environment. These commands help you track system resources and performance metrics.
vmstat
- Report Virtual Memory Statistics
The vmstat
command reports virtual memory statistics, including processes, memory, paging, block IO, traps, and CPU activity.
$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 122104 13172 94456 0 0 12 10 35 45 1 0 98 1 0
iostat
- Report CPU and I/O Statistics
The iostat
command reports CPU and I/O statistics, helping you identify performance bottlenecks.
$ iostat
Linux 5.4.0-42-generic (hostname) 01/01/2021 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
1.00 0.00 0.50 0.10 0.00 98.40
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 1.00 5.00 3.00 500 300
sar
- Collect, Report, or Save System Activity Information
The sar
command collects and reports system activity information, including CPU usage, memory usage, and network activity. Here are some common usages:
- Report CPU usage every second for three iterations:
$ sar -u 1 3
Linux 5.4.0-42-generic (hostname) 01/01/2021 _x86_64_ (2 CPU)
01:00:01 AM CPU %user %nice %system %iowait %steal %idle
01:00:02 AM all 1.00 0.00 0.50 0.10 0.00 98.40
01:00:03 AM all 1.00 0.00 0.50 0.10 0.00 98.40
01:00:04 AM all 1.00 0.00 0.50 0.10 0.00 98.40
- Report memory usage:
$ sar -r 1 3
Linux 5.4.0-42-generic (hostname) 01/01/2021 _x86_64_ (2 CPU)
01:00:01 AM kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit
01:00:02 AM 122104 94456 43.63 13172 94456 122104 12.3
01:00:03 AM 122104 94456 43.63 13172 94456 122104 12.3
01:00:04 AM 122104 94456 43.63 13172 94456 122104 12.3
These advanced commands provide powerful tools for managing packages, disks, and system performance, enabling you to maintain and optimize your Linux environment effectively.
Section 5: Scripting and Automation
Scripting and automation are powerful aspects of Linux that allow users to automate repetitive tasks, manage system configurations, and perform complex operations with ease. This section covers the basics of shell scripting and task scheduling.
5.1. Shell Scripting Basics
Shell scripting involves writing scripts using a shell language like Bash to automate tasks. Here are some fundamental concepts and commands to get you started.
Creating and Running Shell Scripts
A shell script is a text file containing a series of commands. To create a shell script, use a text editor to write your commands and save the file with a .sh
extension. To run the script, you need to make it executable and then execute it.
$ nano myscript.sh
Add the following content to the script:
#!/bin/bash
echo "Hello, World!"
Make the script executable and run it:
$ chmod +x myscript.sh
$ ./myscript.sh
Hello, World!
echo
- Display a Line of Text
The echo
command prints text to the terminal.
$ echo "Hello, World!"
Hello, World!
read
- Read a Line of Input
The read
command reads a line of input from the user.
$ read -p "Enter your name: " name
$ echo "Hello, $name!"
Variables - Using Variables in Scripts
Variables store data that can be used and manipulated within a script.
$ name="John"
$ echo "Hello, $name!"
Hello, John!
Loops - For, While, and Until Loops
Loops allow you to execute a block of code multiple times.
- For Loop:
for i in 1 2 3 4 5
do
echo "Iteration $i"
done
- While Loop:
count=1
while [ $count -le 5 ]
do
echo "Count is $count"
count=$((count + 1))
done
- Until Loop:
count=1
until [ $count -gt 5 ]
do
echo "Count is $count"
count=$((count + 1))
done
Conditionals - If, Else, Elif Statements
Conditionals execute different blocks of code based on certain conditions.
if [ $count -eq 5 ]
then
echo "Count is 5"
elif [ $count -lt 5 ]
then
echo "Count is less than 5"
else
echo "Count is greater than 5"
fi
5.2. Task Scheduling
Task scheduling allows you to automate the execution of scripts and commands at specific times or intervals.
cron
- Schedule Commands to Run at Specific Times
The cron
daemon runs scheduled tasks at specified times. You can use the crontab
command to edit the cron table and schedule tasks.
- Crontab Syntax:
* * * * * command_to_execute
- - - - -
| | | | |
| | | | +----- Day of the week (0 - 7) (Sunday is both 0 and 7)
| | | +------- Month (1 - 12)
| | +--------- Day of the month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)
- Examples:
Run a script every day at 2 AM:
$ crontab -e
0 2 * * * /path/to/script.sh
Run a script every 5 minutes:
$ crontab -e
*/5 * * * * /path/to/script.sh
at
- Schedule Commands to Run Once
The at
command schedules a command to run once at a specified time.
$ at 2:00 PM
at> /path/to/script.sh
at> <EOT>
These scripting and automation commands provide powerful tools for managing and automating tasks on your Linux system, enabling you to increase efficiency and reduce manual effort.
Conclusion
Mastering Linux commands is essential for anyone looking to harness the full potential of the operating system. From basic file operations and system management to advanced networking and scripting, these commands provide the tools you need to navigate, manage, and optimize your Linux environment effectively.
Remember, practice is key to becoming proficient with these commands. Explore beyond the listed commands, experiment with different options, and refer to the man pages (man command
) for detailed information.
For further learning and practice, consider exploring online tutorials, Linux forums, and communities. The more you practice, the more confident and efficient you will become in using the Linux command line.