25 Advanced Bash Commands Every Linux User Should Know
For Linux users aiming to maximize productivity, mastering advanced Bash commands is essential. These commands provide powerful ways to search, manipulate, and automate tasks, unlocking a world of efficiency for both system administration and development workflows. Here’s a deep dive into 25 of the most advanced Bash commands, essential for any serious Linux user.
1. grep
- Advanced Text Searching
- Usage:
grep -r "pattern" /directory
- Explanation:
grep
searches for text patterns within files and directories. With the-r
(recursive) option, it looks through subdirectories as well. It supports regex patterns, allowing for complex search capabilities. - Example:
grep -r "error" /var/log/
searches all logs for "error."
2. awk
- Text Processing and Data Extraction
- Usage:
awk '{print $1, $3}' file.txt
- Explanation:
awk
is a powerful tool for text manipulation, allowing you to filter and reformat data. You can use it to extract columns and apply conditions on text files. - Example:
awk '{print $1, $4, $5}' /var/log/syslog
extracts the first, fourth and fifth columns fromsyslog
.
3. sed
- Stream Editing for Efficient Text Manipulation
- Usage:
sed 's/old/new/g' file.txt
- Explanation:
sed
allows you to find and replace text patterns within files. It’s ideal for batch substitutions across large files or groups of files. - Example:
sed 's/apache/nginx/g' config.txt
replaces "apache" with "nginx" inconfig.txt
.
4. xargs
- Command Chaining for Pipelining Tasks
- Usage:
cat files.txt | xargs rm
- Explanation:
xargs
constructs command lines from standard input. It’s useful in combination withfind
orgrep
to perform actions on lists of files. - Example:
find . -name "*.log" | xargs rm
deletes all.log
files in the current directory and its subdirectories.
Note: This command delete actual files, use it cautiously
5. find
- Locating Files and Directories
- Usage:
find /path -type f -name "*.txt"
- Explanation:
find
is invaluable for locating files based on criteria like name, type, size, or modification date. - Example:
find /etc -type f -name "*.conf"
finds all configuration files in/etc
.
6. chmod
and chown
- Managing Permissions and Ownership
- Usage:
chmod 755 file
,chown user:group file
- Explanation:
chmod
andchown
modify permissions and file ownership. Proper permissions are essential for security and functionality. - Example:
chmod 644 document.txt
sets read and write permissions for the owner and read-only for others.
7. curl
and wget
- Retrieving Web Content
- Usage:
curl -O url
,wget url
- Explanation: Both
curl
andwget
fetch files from the internet, butcurl
is more versatile, supporting API requests. - Example: curl -O https://www.python.org/ftp/python/3.11.10/Python-3.11.10.tgz downloads a python installer to the current directory.
8. tar
- File Archiving and Compression
- Usage:
tar -czvf archive.tar.gz /etc/*.conf
- Explanation:
tar
is used for creating archives, combining multiple files into one. It’s often used withgzip
for compression. - Example:
tar -xzvf archive.tar.gz
extracts a.tar.gz
archive.
9. rsync
- Syncing Files Across Directories and Systems
- Usage:
rsync -avz /source /destination
- Explanation:
rsync
copies and synchronizes files, offering options for compression and partial transfers, making it ideal for backups. - Example:
rsync -avz /home/user /backup/
backs up a user’s home directory to/backup
.
10. netstat
and ss
- Network Monitoring
- Usage:
netstat -tuln
,ss -tuln
- Explanation:
netstat
andss
display network connections and listening ports. Usess
for more recent and faster network inspection. - Example:
ss -tuln
shows active TCP/UDP listening sockets.
11. df
and du
- Disk Usage Analysis
- Usage:
df -h
,du -sh /path
- Explanation:
df
provides an overview of disk space usage, whiledu
gives a breakdown by directory. - Example:
du -sh /home
shows the size of the/home
directory.
12. ps
and top
- Monitoring Running Processes
- Usage:
ps aux
,top
- Explanation:
ps
lists processes, whiletop
provides real-time monitoring of CPU and memory usage. - Example:
top
helps identify resource-intensive processes.
13. kill
and pkill
- Terminating Processes
- Usage:
kill PID
,pkill name
- Explanation:
kill
sends signals to processes. Usekill -9
for forceful termination, andpkill
to terminate by name. - Example:
pkill firefox
stops all Firefox processes.
14. tail
and head
- Viewing File Sections
- Usage:
tail -n 20 file.txt
,head -n 20 file.txt
- Explanation:
tail
displays the last lines, andhead
shows the first lines of a file. Useful for log analysis. - Example:
tail -f /var/log/syslog
continuously shows new log entries.
15. man
- Displaying Command Manuals
- Usage:
man command
- Explanation:
man
displays manuals for commands, helping you understand usage and options. - Example:
man rsync
provides details on usingrsync
.
16. diff
- File Comparison
- Usage:
diff file1 file2
- Explanation:
diff
shows line-by-line differences between files, useful for version control. - Example:
diff config.old config.new
compares two versions of a configuration file.
17. history
- Command Recall and Re-execution
- Usage:
history
- Explanation: Lists past commands, allowing for easy re-execution.
- Example:
!100
re-executes command number 100.
18. alias
- Creating Command Shortcuts
- Usage:
alias ll='ls -al'
- Explanation:
alias
allows you to create shortcuts for commonly used commands. - Example:
alias rmf='rm -f'
creates a shortcut for force deletion.
19. crontab
- Scheduling Jobs
- Usage:
crontab -e
- Explanation: Schedules recurring tasks using cron. Useful for automation.
- Example:
0 2 * * * /path/to/script.sh
runs a script daily at 2 AM.
20. zip
and unzip
- File Compression and Extraction
- Usage:
zip -r archive.zip folder
,unzip archive.zip
- Explanation: Creates and extracts ZIP archives, offering a quick way to manage compressed files.
- Example:
zip -r project.zip /project
compresses/project
intoproject.zip
.
21. ln
- Creating Links
- Usage:
ln -s /source /destination
- Explanation:
ln
creates hard and symbolic links, linking files or directories. - Example:
ln -s /opt/project /home/user/project_link
creates a symbolic link.
22. mount
and umount
- Mounting File Systems
- Usage:
mount /dev/sdb1 /mnt
,umount /mnt
- Explanation: Mounts and unmounts storage devices, allowing access to external storage.
- Example:
mount -o loop disk.iso /mnt
mounts an ISO file.
23. echo
and printf
- Displaying Text
- Usage:
echo "Hello"
,printf "User: %s\n" "$USER"
- Explanation: Outputs text, useful in scripts for showing variable values or message formatting.
- Example:
printf "Current date: %(%Y-%m-%d)T\n" -1
shows the current date.
24. uptime
- System Uptime and Load
- Usage:
uptime
- Explanation: Shows system uptime and average load, helpful for monitoring performance.
- Example:
uptime
displays the system’s active time since the last reboot.
25. iptables
- Configuring Firewall Rules
- Usage:
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
- Explanation:
iptables
manages firewall settings, controlling access to ports and services. - Example:
iptables -L
lists current firewall rules.
These 25 commands are indispensable tools for any Linux user, helping manage, monitor, and automate tasks efficiently. Familiarizing yourself with them will deepen your command-line expertise and significantly enhance your productivity on Linux systems.
Want to master the Bash CLI, check out the below 3 hours video which takes you to the complete Bash CLI Commands list.
Master Bash Command Line: Learn Bash CLI in 3 Hours [2024] — YouTube
🔗 Support my Work
▶️ Support by Subscribing my YouTube