Linux Bash Cheatsheet - how to works with files with examples

This cheatsheet covers everyday file operations and text processing in a Linux Bash shell. Each example includes the command and its output, using a small service inventory to demonstrate filtering, counting, sorting and replacing text.

Examples use GNU/Linux tools. The $ symbol represents the shell prompt and is not part of the command. Commands without output complete silently.

Create a working directory and move into it:

$ mkdir -p bash-lab
$ cd bash-lab
$ pwd
/home/admin/bash-lab

Create an empty file, copy it and rename the copy. Use quotes when a filename contains spaces:

$ touch notes.txt
$ cp notes.txt "notes backup.txt"
$ mv "notes backup.txt" notes-copy.txt
$ ls -1
notes-copy.txt
notes.txt

Use ls -lah for permissions, ownership, hidden files and readable sizes. Remove an individual file with confirmation:

$ rm -i notes-copy.txt
rm: remove regular empty file 'notes-copy.txt'? y

Read Files with cat, head and tail

Create the example inventory. The four columns are hostname, service, status and port:

$ cat > services.txt <<'EOF'
web01 nginx running 443
db01 mysql running 3306
web02 nginx stopped 443
cache01 redis running 6379
EOF

Display the entire file with cat, or limit the output to its first or last lines:

$ cat services.txt
web01 nginx running 443
db01 mysql running 3306
web02 nginx stopped 443
cache01 redis running 6379
$ head -n 2 services.txt
web01 nginx running 443
db01 mysql running 3306
$ tail -n 1 services.txt
cache01 redis running 6379

For large files, use less services.txt and press q to exit. Use tail -f application.log to follow new log entries; stop with Ctrl+C.

Filter Lines with grep

Find lines containing a literal string. The -n option adds line numbers; -F treats the pattern as plain text:

$ grep -nF 'nginx' services.txt
1:web01 nginx running 443
3:web02 nginx stopped 443

Exclude matching lines with -v:

$ grep -vF 'stopped' services.txt
web01 nginx running 443
db01 mysql running 3306
cache01 redis running 6379

Extract Fields with awk

By default, awk splits each line into whitespace-separated fields. Print the hostname and port using $1 and $4:

$ awk '{print $1, $4}' services.txt
web01 443
db01 3306
web02 443
cache01 6379

Filter by an exact field value and print only running hosts:

$ awk '$3 == "running" {print $1}' services.txt
web01
db01
cache01

For simple comma-delimited data, use awk -F, to change the separator. Quoted CSV fields containing commas require a CSV-aware parser.

Count and Sort Results

Count lines with wc -l. Input redirection returns the count without the filename:

$ wc -l < services.txt
4

Sort numerically by the fourth field:

$ sort -k4,4n services.txt
web01 nginx running 443
web02 nginx stopped 443
db01 mysql running 3306
cache01 redis running 6379

Extract service names, sort them and count repeated values:

$ awk '{print $2}' services.txt | sort | uniq -c
1 mysql
2 nginx
1 redis

sort -u produces a unique sorted list. Sorting before uniq -c groups identical lines so every occurrence is counted.

Check Disk Usage with du

Display allocated disk space for a file or directory using human-readable units:

$ du -h services.txt
4.0K services.txt

Summarize each immediate entry in a directory and show the largest first:

$ du -sh -- * | sort -hr
4.0K services.txt
0 notes.txt

Values depend on the filesystem. du measures disk usage; df -h reports filesystem capacity and available space.

Locate Files with find

Find regular files by name, searching recursively from the current directory:

$ find . -type f -name 'services.txt'
./services.txt

Select files modified within the last 24 hours:

$ find . -type f -name '*.txt' -mtime -1 | sort
./notes.txt
./services.txt

Combine file discovery with a content search. The {} placeholder is replaced by matching paths:

$ find . -type f -name '*.txt' -exec grep -HnF 'stopped' {} +
./services.txt:3:web02 nginx stopped 443

Keep filename patterns quoted so Bash does not expand them before find receives them. The -exec … {} + form passes filenames as separate arguments, including names containing spaces.

Find and Replace Text with sed

Preview a replacement without modifying the file. The g flag replaces every match on each line:

$ sed 's/nginx/apache/g' services.txt
web01 apache running 443
db01 mysql running 3306
web02 apache stopped 443
cache01 redis running 6379

Print only selected lines, such as records containing a stopped service:

$ sed -n '/stopped/p' services.txt
web02 nginx stopped 443

Apply a replacement in place and retain the original as services.txt.bak:

$ sed -i.bak 's/nginx/apache/g' services.txt
$ grep -F 'apache' services.txt
web01 apache running 443
web02 apache stopped 443

The search pattern is a regular expression: use \. to match a literal dot. In replacement text, & inserts the matched text; use \& for a literal ampersand. Preview the result before using -i.

Translate Characters with tr

Unlike sed, tr translates or deletes individual characters. Convert lowercase text to uppercase:

$ printf '%s\n' 'linux bash' | tr '[:lower:]' '[:upper:]'
LINUX BASH

Replace commas with line breaks:

$ printf '%s\n' 'web01,db01,cache01' | tr ',' '\n'
web01
db01
cache01

Remove carriage returns from a CRLF text file:

$ tr -d '\r' < windows.txt > unix.txt

Write transformed output to a different file. Redirecting back to the input file truncates it before the command can read it.

Save Results with Pipes and Redirection

Combine filtering, field extraction and sorting, then save the result:

$ awk '$3 == "running" {print $1}' services.txt | sort > running-hosts.txt
$ cat running-hosts.txt
cache01
db01
web01

| sends standard output to the next command. > creates or overwrites a file; >> appends. Use tee output.txt when you need to display and save the same output.

Essential Command Reference

The core workflow is to locate files, inspect their content, extract the required data and save the result. Keep this reference for routine shell work:

# Files
ls -lah
cp source.txt destination.txt
mv old.txt new.txt
rm -i file.txt

# Read and filter
cat file.txt
head -n 10 file.txt
tail -n 10 file.txt
grep -nF 'text' file.txt
awk '{print $1}' file.txt
wc -l < file.txt

# Sort and inspect disk usage
sort -u file.txt
sort -k4,4n file.txt
du -sh -- directory

# Find and transform
find . -type f -name '*.txt'
sed 's/old/new/g' file.txt
sed -i.bak 's/old/new/g' file.txt
tr '[:lower:]' '[:upper:]' < file.txt

# Combine and save
awk '{print $2}' file.txt | sort | uniq -c > summary.txt