Skip to content

Latest commit

 

History

History
164 lines (122 loc) · 4.31 KB

File metadata and controls

164 lines (122 loc) · 4.31 KB

Use Cases - Shell

Last Updated: 2026-02-06 by Keming He

Platform

Important

This guide is for macOS / Linux users with POSIX-compatible shell (sh, bash, zsh).

For Windows users: See WSL (Windows Subsystem for Linux) or PowerShell.

Essential shell commands for developer workflows.

Table of Contents

Search Files and Content

Find Files

find . -name "*.md"               # Find by name (current dir)
find . -iname "readme*"           # Case-insensitive
find . -type f -name "*.js"       # Files only
find . -type d -name "src"        # Directories only
find . -mtime -7                  # Modified in last 7 days

Search Content

grep "pattern" file.txt           # Search in file
grep -r "pattern" .               # Recursive search
grep -i "pattern" file.txt        # Case-insensitive
grep -n "pattern" file.txt        # Show line numbers
grep -l "pattern" *.md            # List matching files

Combined Search

find . -name "*.js" -exec grep -l "TODO" {} \;    # Find files containing TODO

↑ Back to Table of Contents


View and Navigate Files

View File Contents

cat file.txt                      # Entire file
head -20 file.txt                 # First 20 lines
tail -20 file.txt                 # Last 20 lines
tail -f log.txt                   # Follow updates (logs)
less file.txt                     # Scrollable view (q to quit)

File Info

wc -l file.txt                    # Count lines
file document.pdf                 # Identify file type

Navigate

pwd                               # Current directory
cd /path/to/dir                   # Change directory
cd ..                             # Up one level
cd -                              # Previous directory
cd ~                              # Home directory

List Contents

ls -la                            # Detailed with hidden files
ls -lh                            # Human-readable sizes
ls -lt                            # Sort by modification time
tree -L 2                         # Tree view, 2 levels (if installed)

↑ Back to Table of Contents


Output Control

Pipe to Cat

Prevents interactive/pager mode. Essential for scripts and AI agents.

git diff | cat                    # Avoid interactive diff
git log | cat                     # Disable pager
git status | cat                  # Full output, no interaction

Redirect Output

command > file.txt                # Overwrite file
command >> file.txt               # Append to file
command > /dev/null               # Discard stdout
command 2> /dev/null              # Discard stderr
command > /dev/null 2>&1          # Discard all output
command 2>&1 | tee output.txt     # Save and display

↑ Back to Table of Contents


File Management

Create

mkdir directory                   # Create directory
mkdir -p path/to/nested/dir       # Create nested directories
touch file.txt                    # Create empty file

Copy and Move

cp source.txt dest.txt            # Copy file
cp -r source/ dest/               # Copy directory
mv old.txt new.txt                # Rename/move

Delete

rm file.txt                       # Delete file
rm -r directory/                  # Delete directory
rmdir empty-dir/                  # Delete empty directory only

↑ Back to Table of Contents