Published on

Bash Snippets for Terminal Productivity

views·3 mins read

File & Directory Operations

Find and Replace in Files (sed)

Replace 'old_text' with 'new_text' in all files in the current directory.

sed -i 's/old_text/new_text/g' *

Search Text Recursively (grep)

Find a string in all files within a directory, showing line numbers.

grep -rnw './path' -e 'search_term'

List Files by Size

Sort files in the current directory by size in human-readable format.

ls -lhS

Create Directory and Enter (Function)

Add this to your .bashrc or .zshrc.

mkcd() {
  mkdir -p "$1" && cd "$1"
}

Find Large Files

Find files larger than 100MB in the current directory.

find . -type f -size +100M

Tree View of Directory

find . -maxdepth 2 -not -path '*/.*'

Process & System Monitoring

Check Disk Usage

Show disk usage of folders in the current directory.

du -sh * | sort -h

Kill Process by Name

pkill -f process_name

Find Process on Port

Find what is running on port 8080.

lsof -i :8080
# Or
netstat -nlp | grep :8080

Monitor Logs in Real-time

tail -f /var/log/syslog

Watch Command Output

Run a command every 2 seconds and highlight changes.

watch -d 'ls -l'

Networking

Check Public IP

curl ifconfig.me

Test Port Connectivity

nc -zv 127.0.0.1 8080

Download File with Resume

curl -C - -O http://example.com/largefile.zip

Archiving

Extract tar.gz

tar -xzvf archive.tar.gz

Create tar.gz

tar -czvf archive.tar.gz folder_name/

Extract Zip

unzip archive.zip -d destination_folder/

Shell Scripting Tricks

Loop Over Files

for file in *.log; do
    echo "Processing $file"
    cat "$file" | grep "ERROR" >> errors.txt
done

Check if File Exists

if [ -f "config.json" ]; then
    echo "Config found."
else
    echo "Config missing."
fi

Read Input with Default

read -p "Enter environment [dev]: " env
env=${env:-dev}
echo "Deploying to $env"

Useful Aliases

Add these to your shell profile for speed.

alias gs='git status'
alias gp='git pull'
alias gcm='git commit -m'
alias ll='ls -lah'
alias ..='cd ..'
alias ...='cd ../..'
alias ports='netstat -tulanp'