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
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.)
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.)
chmod
- Purpose: Change file permissions.
- Example:
chmod 755 script.sh
(Make the script executable for the user and readable for others.)
chown
- Purpose: Change file owner and group.
- Example:
sudo chown user:group file.txt
(Change the ownership offile.txt
touser
andgroup
.)
ln -s
- Purpose: Create symbolic links.
- Example:
ln -s /path/to/original /path/to/symlink
(Create a symbolic link pointing to the original file.)
alias
- Purpose: Create shortcuts for commands.
- Example:
alias ll='ls -la'
(Create a shortcutll
for listing files with detailed info.)
jobs
- Purpose: Show background and stopped jobs.
- Example:
jobs
(List currently running background jobs.)
bg
/fg
- Purpose: Run jobs in the background (
bg
) or foreground (fg
). - Example:
bg %1
/fg %1
(Move job 1 to the background/foreground.)
- Purpose: Run jobs in the background (
nohup
- Purpose: Run commands immune to hangups (keeps running after logout).
- Example:
nohup script.sh &
(Runscript.sh
in the background and ignore hangups.)
disown
- Purpose: Remove jobs from shell without terminating them.
- Example:
disown %1
(Disown job 1 so it keeps running after logout.)
export
- Purpose: Set environment variables for the current session.
- Example:
export PATH=$PATH:/new/path
(Add/new/path
to the system’sPATH
variable.)
source
- Purpose: Execute commands from a file in the current shell.
- Example:
source ~/.bash_profile
(Load and execute commands from.bash_profile
.)
df -h
- Purpose: Display disk space usage in a human-readable format.
- Example:
df -h
(Display disk space usage in GB/MB.)
du -sh
- Purpose: Display the size of a directory or file.
- Example:
du -sh /path/to/directory
(Show the size of a directory.)
rsync
- Purpose: Sync files between directories or systems.
- Example:
rsync -avz /source/path/ /destination/path/
(Sync directories with verbose and compression.)
crontab
- Purpose: Schedule tasks using cron.
- Example:
crontab -e
(Edit and schedule tasks using cron.)
tail -f
- Purpose: Follow a file’s content in real-time.
- Example:
tail -f /var/log/system.log
(Follow the latest entries insystem.log
.)
head
- Purpose: Show the first few lines of a file.
- Example:
head -n 20 file.txt
(Display the first 20 lines offile.txt
.)
curl -O
- Purpose: Download a file from the web.
- Example:
curl -O https://example.com/file.txt
(Downloadfile.txt
from a URL.)
wget
- Purpose: Download files (if installed via Homebrew).
- Example:
wget https://example.com/file.txt
(Downloadfile.txt
using wget.)
zip / unzip
- Purpose: Compress and extract zip files.
- Example:
zip archive.zip file.txt
,unzip archive.zip
(Create and extract zip files.)
ssh-keygen
- Purpose: Generate SSH keys.
- Example:
ssh-keygen -t rsa -b 4096
(Generate a 4096-bit RSA SSH key.)
history
- Purpose: Show a list of recently executed commands.
- Example:
history
(Display the history of executed commands.)
!!
(bang bang)- Purpose: Run the last command again.
- Example:
!!
(Repeat the last command.)
!n
(history recall)- Purpose: Run the nth command from the history.
- Example:
!125
(Run the 125th command in the history.)
pkill
- Purpose: Kill processes by name.
- Example:
pkill chrome
(Kill all Chrome browser processes.)
watch
- Purpose: Repeatedly run a command at regular intervals.
- Example:
watch -n 5 df -h
(Rundf -h
every 5 seconds.)
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 andfile.txt
.)
curl -I
- Purpose: Fetch headers from a URL.
- Example:
curl -I https://example.com
(Retrieve HTTP headers fromexample.com
.)
basename
- Purpose: Strip directory and suffix from filenames.
- Example:
basename /path/to/file.txt .txt
(Returnfile
, removing the directory and.txt
extension.)
Primary Category
Sub-Domain
Confidentiality
No