Skip to content
Commitpedia
Log in Join & edit

Shell cheat sheet: everyday commands

A quick reference for moving around, managing files, viewing and searching text, checking processes and disk space in bash and zsh, with notes on the commands that can hurt you.

Edit history of Shell cheat sheet: everyday commands: 1 recorded edit
Edit history · 1 edit
Type
Article
Edits
1
Editors
1
First issued
15 Oct 2026
Last edit
15 Oct 2026
Reading
3 min

Keep this page open while you learn the terminal. Every command works in bash and zsh on Linux and macOS unless noted. Add --help to most commands, or run man command, for the full list of options.

Moving around

pwd                 # where am I?
ls                  # list files
ls -la              # long format, including hidden dotfiles
ls -lh              # human-readable sizes
cd projects         # go into a folder
cd ..               # up one level
cd ~   (or just cd) # home folder
cd -                # back to the previous folder

Press Tab to complete file and command names, and the Up arrow or Ctrl+R to search your history. Quote paths with spaces: cd "My Files".

Creating, copying and moving

mkdir notes                 # new folder
mkdir -p a/b/c              # create parent folders too
touch todo.txt              # empty file, or update its timestamp
cp todo.txt backup.txt      # copy a file
cp -r src/ src-copy/        # copy a folder
mv old.txt new.txt          # rename
mv report.pdf ~/Documents/  # move

cp and mv silently overwrite an existing destination. Add -i to be asked first, or -n to never overwrite.

Deleting, carefully

rm file.txt          # delete a file: there is no trash can
rm -i *.log          # ask before each file
rmdir empty-folder   # only removes empty folders
rm -r old-folder     # delete a folder and everything in it
  • rm does not move files to the trash. They are gone.
  • Before a risky delete, run the same pattern with ls first: ls *.log, then rm *.log.
  • Be very careful with rm -rf and variables. If $DIR is empty, rm -rf "$DIR"/* targets /*. In scripts, use "${DIR:?}", which stops with an error when the variable is unset or empty.
  • Never run commands you do not understand that combine sudo and rm.

Viewing files

cat file.txt          # print the whole file
less big.log          # scroll; q quits, /word searches
head -n 20 file.txt   # first 20 lines
tail -n 50 app.log    # last 50 lines
tail -f app.log       # follow a growing log, Ctrl+C to stop
wc -l file.txt        # count lines

Searching

find . -name "*.md"                 # files by name, below the current folder
find . -type d -name node_modules    # folders only
find . -type f -size +100M           # files over 100 MB
find . -mtime -1                     # changed in the last 24 hours
grep -rn "TODO" src/                 # text inside files, with line numbers
grep -rl "apiKey" .                  # only list matching file names
grep -rn --include='*.py' "import" . # limit to certain files

Permissions and ownership

Check before you run this

  • Runs with administrator rights. Make sure you understand every line.
ls -l script.sh            # show permissions
chmod +x script.sh         # make executable
chmod 644 notes.txt        # rw-r--r--
sudo chown ana:ana file    # change owner and group

Processes and ports

ps aux | grep node         # find a process
top                        # live view; q quits (htop is friendlier if installed)
kill 12345                 # ask process 12345 to stop (SIGTERM)
kill -9 12345              # force stop (SIGKILL), last resort
lsof -i :3000              # what uses port 3000?
jobs; fg; bg               # manage jobs started from this shell
some-command &             # run in the background

Ctrl+C interrupts the running command, Ctrl+Z suspends it (resume with fg), and Ctrl+D ends input or closes the shell.

Disk space

df -h              # free space per disk
du -sh *           # size of each item here
du -sh ~/Downloads # size of one folder

Archives and downloads

tar -czf site.tar.gz site/   # create a gzip archive
tar -xzf site.tar.gz         # extract it
zip -r site.zip site/        # zip a folder
unzip site.zip
curl -O https://example.com/file.tar.gz   # download keeping the name
curl -s https://api.github.com | head    # print a response

History and shortcuts

Check before you run this

  • Runs with administrator rights. Make sure you understand every line.
history | tail -n 20   # recent commands
!!                     # repeat the last command
sudo !!                # repeat it with sudo

macOS vs Linux notes

  • macOS ships BSD versions of many tools, so some flags differ from Linux’s GNU versions. Notable cases are sed -i, date and stat.
  • To get GNU tools on macOS, install them with Homebrew (for example coreutils, gnu-sed). They are installed with a g prefix, such as gsed.
  • The default shell on current macOS is zsh; most Linux distributions default to bash.

See also

  • 0049 Pipes and redirection: connecting commands A pipe sends one command's output into another command, and redirection sends output to a file. Together they let small tools build… Beginner · Concept · Linux, macOS Article Basics · Text processing 15 Oct 2026
  • 0050 File permissions: chmod, chown and what 755 means Every file has an owner, a group and read/write/execute permissions for three kinds of users. chmod changes the permissions and chown changes… Beginner · Concept · Linux, macOS Article Basics 15 Oct 2026
  • 0051 Environment variables and PATH Environment variables are named settings that every program inherits from the shell. PATH is the one that tells the shell where to… Beginner · Concept · Linux, macOS Article Basics 15 Oct 2026
  • 0076 jq recipes: 23 one-liners for JSON on the command line A runnable collection of jq filters for everyday JSON work: pick fields, filter with select, reshape, sort, count, group, pass shell variables,… Intermediate · Reference · Linux, macOS, Windows Script Command-line tools · jq & JSON on the command line 15 Oct 2026

Repos in the wild

History

Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.

  1. commitpedia-admin +4,391 chars

Report a problem

Something wrong, unsafe or copied? Tell the editors.
What is wrong?