A Comprehensive Guide to Linux Commands: From Basic to Advanced
Linux is a powerful and versatile operating system that provides users with a wide range of commands for managing files, processes, and system resources. Whether you’re a beginner or an advanced user, mastering Linux commands can significantly enhance your productivity and efficiency. This blog will take you through some of the most essential Linux commands, starting from the basics and gradually moving towards more advanced concepts.
1. Basic Commands
1.1 pwd
(Print Working Directory):
The pwd
command displays the current directory you’re working in. It’s helpful to know your location in the file system hierarchy.
pwd
1.2 ls
(List Directory Contents):
The ls
command lists the files and directories in the current directory. It has several options for viewing hidden files, listing in long format, and sorting.
ls -l
ls -a
1.3 cd
(Change Directory):
The cd
command is used to change the current directory to another directory.
cd /path/to/directory
cd ..
1.4 mkdir
(Make Directory):
The mkdir
command creates a new directory.
mkdir new_directory
1.5 touch
(Create a New File):
The touch
command creates an empty file.
touch newfile.txt
1.6 rm
(Remove Files and Directories):
The rm
command deletes files and directories. Be cautious with this command, as it permanently deletes files.
rm file.txt
rm -r directory_name
1.7 cp
(Copy Files and Directories):
The cp
command copies files and directories from one location to another.
cp source_file.txt destination_file.txt
cp -r source_directory destination_directory
1.8 mv
(Move/Rename Files and Directories):
The mv
command moves or renames files and directories.
mv old_name.txt new_name.txt
mv file.txt /new/location/
2. Intermediate Commands
2.1 chmod
(Change File Permissions):
The chmod
command changes the permissions of a file or directory. Permissions are represented by a combination of read (r), write (w), and execute (x) permissions for the user, group, and others.
chmod 755 script.sh
2.2 chown
(Change Ownership):
The chown
command changes the ownership of a file or directory to a different user and/or group.
chown user:group filename
2.3 find
(Search for Files and Directories):
The find
command searches for files and directories based on criteria such as name, size, or modification date.
find /path -name "filename"
2.4 grep
(Search Text Using Patterns):
The grep
command searches for patterns within files. It’s useful for finding specific strings in files.
grep "search_term" filename
grep -r "search_term" /path
2.5 tar
(Archive Files):
The tar
command is used to create, extract, and manipulate archive files (e.g., .tar, .tar.gz).
tar -cvf archive_name.tar directory_name
tar -xvf archive_name.tar
2.6 ps
(Process Status):
The ps
command displays information about running processes.
ps aux
2.7 kill
(Terminate Processes):
The kill
command sends a signal to a process to terminate it. You can use it with the process ID (PID) obtained from ps
.
kill PID
kill -9 PID # Forcefully terminate
2.8 df
(Disk Space Usage):
The df
command reports the amount of disk space used and available on file systems.
df -h
3. Advanced Commands
3.1 awk
(Pattern Scanning and Processing Language):
The awk
command is a powerful text-processing language used for manipulating data and generating reports.
awk '{print $1, $3}' file.txt
3.2 sed
(Stream Editor):
The sed
command is used for parsing and transforming text in a stream or file.
sed 's/old/new/g' file.txt
3.3 cron
(Job Scheduler):cron
is a time-based job scheduler in Unix-like operating systems. Use crontab
to schedule jobs.
crontab -e # Edit crontab
* * * * * /path/to/script.sh # Example cron job
3.4 rsync
(Remote Sync):
The rsync
command is used for synchronizing files and directories between two locations over a network.
rsync -avz /source/ /destination/
3.5 iptables
(Configure Network Packet Filtering Rules):iptables
is a command-line utility for configuring the Linux kernel’s built-in firewall.
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
3.6 systemctl
(Control the System and Service Manager):
The systemctl
command is used to control the systemd system and service manager.
systemctl start/stop/restart service_name
systemctl enable/disable service_name
3.7 ssh
(Secure Shell):
The ssh
command is used to securely log into a remote machine and execute commands.
ssh user@remote_host
3.8 top
(Task Manager):
The top
command provides a real-time view of running processes, including system resource usage.
top
3.9 iptables
(Packet Filtering and NAT):
The iptables
command is used to set up, maintain, and inspect the tables of IP packet filter rules in the Linux kernel.
iptables -L
Expanding Your Linux Command Arsenal: More Commands to Master
As you continue to explore Linux, you’ll encounter a vast array of commands that can streamline your workflow and enhance your productivity. In this section, we’ll dive into more advanced commands and utilities that are commonly used in Linux environments. These commands can help you manage files, control processes, interact with networks, and more.
4. File Management and Text Processing Commands
4.1 cat
(Concatenate and Display Files):
The cat
command is used to display the contents of a file, combine multiple files into one, and create new files.
cat file.txt
cat file1.txt file2.txt > combined.txt
4.2 less
(View File Contents One Screen at a Time):
The less
command allows you to view the contents of a file one screen at a time, which is useful for large files.
less largefile.txt
4.3 head
and tail
(View the Beginning or End of Files):
The head
command displays the first few lines of a file, while tail
shows the last few lines.
head -n 10 file.txt # First 10 lines
tail -n 10 file.txt # Last 10 lines
4.4 diff
(Compare Files Line by Line):
The diff
command compares two files line by line and highlights the differences between them.
diff file1.txt file2.txt
4.5 sort
(Sort Lines of Text Files):
The sort
command arranges the lines in a text file in alphabetical or numerical order.
sort file.txt
sort -n numbers.txt # Numerical sort
4.6 uniq
(Report or Omit Repeated Lines):
The uniq
command removes or reports duplicate lines in a sorted file.
sort file.txt | uniq
4.7 wc
(Word, Line, Character Count):
The wc
command counts the number of lines, words, and characters in a file.
wc file.txt
wc -l file.txt # Count lines
4.8 tr
(Translate or Delete Characters):
The tr
command is used for translating or deleting characters in a file.
tr 'a-z' 'A-Z' < file.txt # Convert lowercase to uppercase
4.9 cut
(Remove Sections from Each Line of Files):
The cut
command extracts specific sections from each line of a file.
cut -d ':' -f 1 /etc/passwd # Extract first field using ':' as delimiter
4.10 paste
(Merge Lines of Files):
The paste
command merges lines from multiple files into a single line.
paste file1.txt file2.txt
5. Networking and System Monitoring Commands
5.1 ping
(Check Network Connectivity):
The ping
command tests the connectivity between your machine and another network host.
ping google.com
5.2 wget
(Download Files from the Web):
The wget
command downloads files from the web via HTTP, HTTPS, or FTP.
wget https://example.com/file.zip
5.3 curl
(Transfer Data to or from a Server):
The curl
command is a versatile tool for transferring data to or from a server using various protocols.
curl -O https://example.com/file.zip
5.4 netstat
(Network Statistics):
The netstat
command displays network connections, routing tables, interface statistics, and more.
netstat -tuln
5.5 ifconfig
(Configure Network Interfaces):
The ifconfig
command is used to configure, manage, and query network interface parameters.
ifconfig eth0
5.6 nslookup
(Query DNS Records):
The nslookup
command queries the Domain Name System (DNS) to obtain domain name or IP address mapping information.
nslookup example.com
5.7 traceroute
(Trace Network Path to a Host):
The traceroute
command shows the route packets take to reach a network host.
traceroute google.com
5.8 htop
(Interactive Process Viewer):
The htop
command is an interactive and visually appealing process viewer. It provides a real-time view of system processes and resource usage.
htop
5.9 uptime
(System Uptime and Load):
The uptime
command displays how long the system has been running, along with the load averages.
uptime
5.10 whois
(Domain Lookup Information):
The whois
command provides information about a domain, including registration details and contact information.
whois example.com
6. File Compression and Archiving Commands
6.1 gzip
and gunzip
(Compress and Decompress Files):
The gzip
command compresses files, and gunzip
decompresses them.
gzip file.txt
gunzip file.txt.gz
6.2 zip
and unzip
(Create and Extract Zip Archives):
The zip
command creates compressed zip archives, and unzip
extracts them.
zip archive.zip file1.txt file2.txt
unzip archive.zip
6.3 bzip2
and bunzip2
(Compress and Decompress Files):
The bzip2
command compresses files more effectively than gzip
, and bunzip2
decompresses them.
bzip2 file.txt
bunzip2 file.txt.bz2
6.4 xz
and unxz
(Compress and Decompress Files):
The xz
command provides high compression ratios, and unxz
decompresses files.
xz file.txt
unxz file.txt.xz
6.5 tar
with Compression:
The tar
command can be combined with compression tools like gzip
and bzip2
to create compressed archives.
tar -cvzf archive.tar.gz directory/
tar -xvzf archive.tar.gz
7. System Administration Commands
7.1 sudo
(Superuser Do):
The sudo
command allows a permitted user to execute a command as the superuser (root) or another user.
sudo command
7.2 passwd
(Change User Password):
The passwd
command is used to change the password of a user.
passwd username
7.3 adduser
and useradd
(Add a New User):
The adduser
and useradd
commands create new user accounts on the system.
adduser newuser
useradd newuser
7.4 deluser
and userdel
(Remove a User):
The deluser
and userdel
commands remove user accounts from the system.
deluser username
userdel username
7.5 groupadd
(Create a New Group):
The groupadd
command creates a new group on the system.
groupadd newgroup
7.6 usermod
(Modify a User Account):
The usermod
command modifies user account settings, such as changing the user’s group or home directory.
usermod -aG groupname username
7.7 crontab
(Schedule Tasks):
The crontab
command allows you to schedule tasks (cron jobs) to run automatically at specified intervals.
crontab -e
7.8 alias
(Create Shortcuts for Commands):
The alias
command creates shortcuts for frequently used commands.
alias ll='ls -la'
7.9 umount
(Unmount Filesystems):
The umount
command unmounts filesystems that have been mounted with the mount
command.
umount /dev/sdb1
7.10 shutdown
and reboot
(Power Off and Restart the System):
The shutdown
command powers off the system, while reboot
restarts it.
shutdown now
reboot
Conclusion
With these additional commands, you are now equipped with a comprehensive toolkit for managing files, processes, networks, and system administration tasks in Linux. As you practice and apply these commands in real-world scenarios, you’ll gain a deeper understanding of how to leverage the power of the Linux command line to its fullest potential. Happy Linuxing!