Top 10 Essential Bash Commands for Devs

Zane - Jan 9 - - Dev Community

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Advanced: Combine with exec to perform actions, like deleting all .tmp files:
  find . -name "*.tmp" -exec rm {} \;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Advanced: Extract specific files from an archive:
  tar -xzvf archive.tar.gz --include="specific_file"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Advanced: Sum the values of a column:
  awk '{sum += $1} END {print sum}' filename.txt
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Advanced: Delete lines matching a pattern:
  sed '/pattern_to_delete/d' filename.txt
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Advanced: Download a website for offline viewing:
  wget --mirror --convert-links --adjust-extension --page-requisites --no-parent http://example.com
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Advanced: Set sticky bit on a directory to maintain user ownership:
  chmod +t /path/to/directory
Enter fullscreen mode Exit fullscreen mode

Change a file's permissions to read and execute only.

chmod 500 script.sh
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Advanced: Schedule a script to run at 5 PM every Friday:
  0 17 * * 5 /path/to/script.sh
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Advanced: Use SSH tunneling for secure browsing:
  ssh -D 8080 -C -N username@remote-server.com
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Advanced: Use with grep to filter logs:
  tail -f /var/log/syslog | grep error
Enter fullscreen mode Exit fullscreen mode
. . . . . . . .
Terabox Video Player