Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Concepts

Useful Linux one-liners

A collection of practical Linux one-liners for files, text, numbers, loops and processes, each explained and safe to run, with live examples you can edit.

A one-liner is a short pipeline or loop that solves a real problem in a single line. They are the everyday vocabulary of the command line: count things, rename things, find duplicates, reshape a table. Each recipe below is small enough to read in one go, and each is built from tools covered elsewhere in this reference.

The recipes are grouped by task. Run them, then change them: swap a pattern, add a flag, or replace the sample data with your own. Quote variables, use -- before file names you do not control, and test destructive commands (rm, mv) with echo in front first.

Terminal
ls | wc -l                         # how many entries here?
sort file | uniq -d                # which lines occur more than once?

Try it

Bash Text and numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Files, loops and safe renaming

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Recipes

Files and directories

Task One-liner
Count entries in a directory ls -A | wc -l
Show the 5 largest files below here find . -type f -exec du -k {} + | sort -rn | head -n 5
Find files changed in the last day find . -type f -mtime -1
Delete empty files (preview with -print) find . -type f -empty -delete
Make a backup copy with a date cp -- file "file.$(date +%F)"
Create nested directories mkdir -p project/{src,docs,tests}
Disk use per top-level folder du -sh -- */ | sort -h

Text

Task One-liner
Remove blank lines grep -v '^$' file
Trim trailing spaces sed 's/[[:space:]]*$//' file
Number lines nl -ba file or awk '{ print NR": "$0 }' file
Print lines 10 to 20 sed -n '10,20p' file
Replace in many files grep -rl old . | xargs sed -i 's/old/new/g'
Join lines with commas paste -sd, file
Reverse the lines tac file
Show only the last field awk '{ print $NF }' file
Words in a file wc -w < file

Processes and system

Task One-liner
Top 5 memory users ps aux --sort=-%mem | head -n 6
Is a program running? pgrep -x sshd >/dev/null && echo yes
Run something with a time limit timeout 5 command
Repeat every 2 seconds watch -n 2 command
Free space as a plain number df -P . | awk 'NR==2 { print $4 }'
Show listening ports ss -ltn

Scripting helpers

Task One-liner
Loop over lines safely while IFS= read -r line; do ...; done < file
Default value name=${1:-world}
Check a command exists command -v git >/dev/null || echo missing
Run on every file, one at a time find . -name '*.log' -exec gzip -- {} \;
Parallel work, 4 at a time printf '%s\n' *.log | xargs -P4 -n1 gzip
Random password-like string head -c 24 /dev/urandom | base64

Usage notes

  • Preview, then act. Put echo before mv, rm or sed -i the first time, read the output, then remove the echo.
  • ls is for people, not scripts. Names with spaces or newlines break ls | ... pipelines. Use globs (for f in *.txt) or find -print0 | xargs -0.
  • Quote every variable: "$f", not $f. See Quoting pitfalls.
  • Pipes hide failures. Without set -o pipefail the status of a pipeline is that of its last stage.
  • sed -i and xargs differ across systems. GNU versions are assumed here; BSD and macOS need sed -i '' and xargs without -r.
  • Do not write to the file you read. sort file > file empties it first. Use sort -o file file or a temporary file.
  • Make useful ones permanent. Turn a recipe you use weekly into an alias, a function in ~/.bashrc or a small script. See alias.
  • bc is not on our runner, so the arithmetic example above falls back to awk. Check availability with command -v bc.