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.
Navigate and Manage Files
Create a working directory and move into it:
Create an empty file, copy it and rename the copy. Use quotes when a filename contains spaces:
Use ls -lah for permissions, ownership, hidden files and readable sizes. Remove an individual file with confirmation:
Read Files with cat, head and tail
Create the example inventory. The four columns are hostname, service, status and port:
Display the entire file with cat, or limit the output to its first or last lines:
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:
Exclude matching lines with -v:
Extract Fields with awk
By default, awk splits each line into whitespace-separated fields. Print the hostname and port using $1 and $4:
Filter by an exact field value and print only running hosts:
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:
Sort numerically by the fourth field:
Extract service names, sort them and count repeated values:
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:
Summarize each immediate entry in a directory and show the largest first:
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:
Select files modified within the last 24 hours:
Combine file discovery with a content search. The {} placeholder is replaced by matching paths:
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:
Print only selected lines, such as records containing a stopped service:
Apply a replacement in place and retain the original as services.txt.bak:
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:
Replace commas with line breaks:
Remove carriage returns from a CRLF text file:
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:
| 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: