Date: September 13, 2023

Abstract: Below I've listed 30 powerful shell commands along with real-world examples for each one. This should be a super handy reference as you work through different bash tasks on your Mac! Contact us if you’d like more clarification or help. 😊

Here’s an expanded list of Command-Line utilities with examples for each command:

Important: I forgot to add 'rclone' to this list.  The 'rclone' command is definitely note-worthy. So note-worthy in fact I wrote up a full page on how useful it is today with all the online backup services out there.  >Link here.

Advanced Bash Commands with Examples

  1. xargs
    • Purpose: Build and execute commands from standard input.
    • Example: find . -name "*.txt" | xargs rm
      (Find and remove all .txt files in the current directory.)
  2. grep -r
    • Purpose: Search recursively through directories for a pattern.
    • Example: grep -r "search_term" /path/to/directory
      (Search for “search_term” in all files within the directory.)
  3. chmod
    • Purpose: Change file permissions.
    • Example: chmod 755 script.sh
      (Make the script executable for the user and readable for others.)
  4. chown
    • Purpose: Change file owner and group.
    • Example: sudo chown user:group file.txt
      (Change the ownership of file.txt to user and group.)
  5. ln -s
    • Purpose: Create symbolic links.
    • Example: ln -s /path/to/original /path/to/symlink
      (Create a symbolic link pointing to the original file.)
  6. alias
    • Purpose: Create shortcuts for commands.
    • Example: alias ll='ls -la'
      (Create a shortcut ll for listing files with detailed info.)
  7. jobs
    • Purpose: Show background and stopped jobs.
    • Example: jobs
      (List currently running background jobs.)
  8. bg / fg
    • Purpose: Run jobs in the background (bg) or foreground (fg).
    • Example: bg %1 / fg %1
      (Move job 1 to the background/foreground.)
  9. nohup
    • Purpose: Run commands immune to hangups (keeps running after logout).
    • Example: nohup script.sh &
      (Run script.sh in the background and ignore hangups.)
  10. disown
    • Purpose: Remove jobs from shell without terminating them.
    • Example: disown %1
      (Disown job 1 so it keeps running after logout.)
  11. export
    • Purpose: Set environment variables for the current session.
    • Example: export PATH=$PATH:/new/path
      (Add /new/path to the system’s PATH variable.)
  12. source
    • Purpose: Execute commands from a file in the current shell.
    • Example: source ~/.bash_profile
      (Load and execute commands from .bash_profile.)
  13. df -h
    • Purpose: Display disk space usage in a human-readable format.
    • Example: df -h
      (Display disk space usage in GB/MB.)
  14. du -sh
    • Purpose: Display the size of a directory or file.
    • Example: du -sh /path/to/directory
      (Show the size of a directory.)
  15. rsync
    • Purpose: Sync files between directories or systems.
    • Example: rsync -avz /source/path/ /destination/path/
      (Sync directories with verbose and compression.)
  16. crontab
    • Purpose: Schedule tasks using cron.
    • Example: crontab -e
      (Edit and schedule tasks using cron.)
  17. tail -f
    • Purpose: Follow a file’s content in real-time.
    • Example: tail -f /var/log/system.log
      (Follow the latest entries in system.log.)
  18. head
    • Purpose: Show the first few lines of a file.
    • Example: head -n 20 file.txt
      (Display the first 20 lines of file.txt.)
  19. curl -O
    • Purpose: Download a file from the web.
    • Example: curl -O https://example.com/file.txt
      (Download file.txt from a URL.)
  20. wget
    • Purpose: Download files (if installed via Homebrew).
    • Example: wget https://example.com/file.txt
      (Download file.txt using wget.)
  21. zip / unzip
    • Purpose: Compress and extract zip files.
    • Example: zip archive.zip file.txt, unzip archive.zip
      (Create and extract zip files.)
  22. ssh-keygen
    • Purpose: Generate SSH keys.
    • Example: ssh-keygen -t rsa -b 4096
      (Generate a 4096-bit RSA SSH key.)
  23. history
    • Purpose: Show a list of recently executed commands.
    • Example: history
      (Display the history of executed commands.)
  24. !! (bang bang)
    • Purpose: Run the last command again.
    • Example: !!
      (Repeat the last command.)
  25. !n (history recall)
    • Purpose: Run the nth command from the history.
    • Example: !125
      (Run the 125th command in the history.)
  26. pkill
    • Purpose: Kill processes by name.
    • Example: pkill chrome
      (Kill all Chrome browser processes.)
  27. watch
    • Purpose: Repeatedly run a command at regular intervals.
    • Example: watch -n 5 df -h
      (Run df -h every 5 seconds.)
  28. tee
    • Purpose: Read from input and write to both output and a file.
    • Example: echo "Hello" | tee file.txt
      (Write “Hello” to both the console and file.txt.)
  29. curl -I
    • Purpose: Fetch headers from a URL.
    • Example: curl -I https://example.com
      (Retrieve HTTP headers from example.com.)
  30. basename
    • Purpose: Strip directory and suffix from filenames.
    • Example: basename /path/to/file.txt .txt
      (Return file, removing the directory and .txt extension.)

 

Primary Category
Sub-Domain
Confidentiality
No