10 must-know bash commands, essential for any developer looking to harness the full potential of their software environment.
1. grep
- Text Pattern Search
grep
is used for searching text patterns within files. It's extremely useful for locating specific strings of text.
-
Basic: Search for "error" in
filename.log
:
grep 'error' filename.log
- Advanced: Use regular expressions to find complex patterns. For instance, searching for words starting with 'error' followed by a digit:
grep -E 'error[0-9]' filename.log
2. find
- Find Files
find
helps locate files and directories based on criteria like name, size, or modification date.
-
Basic: Locate
.txt
files modified in the last 7 days:
find . -name "*.txt" -mtime -7
-
Advanced: Combine with
exec
to perform actions, like deleting all.tmp
files:
find . -name "*.tmp" -exec rm {} \;
3. tar
- File Compression and Extraction
tar
is essential for creating and extracting compressed archive files, commonly used for backups and file transfers.
-
Basic: Create a
tar.gz
archive of a directory:
tar -czvf archive.tar.gz /path/to/directory
- Advanced: Extract specific files from an archive:
tar -xzvf archive.tar.gz --include="specific_file"
4. awk
- Text Processing
awk
is a powerful text processing tool, ideal for manipulating data and generating reports.
-
Basic: Print the first column from
filename.txt
:
awk '{print $1}' filename.txt
- Advanced: Sum the values of a column:
awk '{sum += $1} END {print sum}' filename.txt
5. sed
- Stream Editor
sed
allows developers to edit text in a scriptable way, useful for modifying files programmatically.
-
Basic: Replace "oldtext" with "newtext" in
filename.txt
:
sed 's/oldtext/newtext/' filename.txt
- Advanced: Delete lines matching a pattern:
sed '/pattern_to_delete/d' filename.txt
6. wget
- Download Files
wget
enables file downloads from the internet, supporting various protocols like HTTP, HTTPS, and FTP.
- Basic: Download a file from the web:
wget http://example.com/file.zip
- Advanced: Download a website for offline viewing:
wget --mirror --convert-links --adjust-extension --page-requisites --no-parent http://example.com
7. chmod
- Change File Permissions
chmod
changes the file access permissions, crucial for managing the security of files.
- Basic: Change a file's permissions to read and execute only:
chmod 500 script.sh
- Advanced: Set sticky bit on a directory to maintain user ownership:
chmod +t /path/to/directory
Change a file's permissions to read and execute only.
chmod 500 script.sh
8. crontab
- Schedule Tasks
crontab
is used for scheduling tasks to run at specific times, automating routine background jobs.
- Basic: Edit the crontab to add a new scheduled task:
crontab -e
- Advanced: Schedule a script to run at 5 PM every Friday:
0 17 * * 5 /path/to/script.sh
9. ssh
- Secure Shell Access
ssh
provides secure remote access to other computers or servers over an unsecured network.
- Basic: Connect to a remote server:
ssh username@remote-server.com
- Advanced: Use SSH tunneling for secure browsing:
ssh -D 8080 -C -N username@remote-server.com
10. tail -f
- Monitor File Changes
tail -f
allows users to view the end of a file in real-time, helpful for monitoring log files.
- Basic: Follow the updates to a log file in real time:
tail -f /var/log/syslog
-
Advanced: Use with
grep
to filter logs:
tail -f /var/log/syslog | grep error