Linux cut, sort, uniq, tr, paste, join, and comm Cheat Sheet

Column extraction, sorting, deduplication, character translation, file combining, and list comparison.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

cut and paste

Extract and assemble delimited fields.

Extract comma-delimited fields

Print the first and third fields from a CSV-like file.

bashANYcutcsvfields
bash
cut -d, -f1,3 users.csv
Notes

`cut` is fast and simple for delimited text when quoting rules are uncomplicated.

Extract character positions

Print characters 1 through 8 from each line.

bashANYcutcharacters
bash
cut -c1-8 ids.txt
Notes

Character mode is handy for fixed-width text formats.

Exclude selected fields

Print all fields except one.

bashANYcutexclude
bash
cut -d, --complement -f2 users.csv
Notes

Useful when one noisy column needs to be removed.

Merge files side by side

Combine lines from two files into columns.

bashANYpastemergecolumns
bash
paste names.txt emails.txt
Notes

The nth line of each file is merged into a single output row.

Paste with a custom delimiter

Combine files using commas or tabs.

bashANYpastedelimiter
bash
paste -d, names.txt emails.txt
Notes

Useful for building ad hoc CSV-like output.

sort and uniq

Sort text and collapse duplicate lines.

Sort lines alphabetically

Default lexicographic sort.

bashANYsortalphabetical
bash
sort names.txt
Notes

`sort` is often the first step before `uniq` or `comm`.

Sort in reverse order

Invert the output order.

bashANYsortreverse
bash
sort -r names.txt
Notes

Common when listing highest values or newest versions first.

Sort numerically

Order lines by numeric value.

bashANYsortnumeric
bash
sort -n numbers.txt
Notes

Without `-n`, numbers are compared lexicographically as strings.

Sort human-readable sizes

Sort values like 1K, 5M, 2G.

bashANYsortsizes
bash
sort -h sizes.txt
Notes

Great for commands like `du -h` and capacity reports.

Sort by a specific field

Sort on the second whitespace-separated column.

bashANYsortfield
bash
sort -k2,2 file.txt
Notes

Field sorting is essential for tabular command output.

Sort and deduplicate

Return sorted unique lines.

bashANYsortuniqdedupe
bash
sort -u names.txt
Notes

Equivalent in effect to `sort | uniq` for many workflows.

Count duplicate lines

Count occurrences of adjacent duplicate lines.

bashANYuniqcountdedupe
bash
sort access.log | uniq -c
Notes

Remember that `uniq` only collapses adjacent duplicates, so sorting first is common.

Show duplicate lines only

Print only lines that appear more than once.

bashANYuniqduplicates
bash
sort file.txt | uniq -d
Notes

Great for spotting repeated entries or duplicates in lists.

tr Translation and Character Operations

Translate, delete, and normalize characters.

Convert lowercase to uppercase

Translate characters from lower to upper case.

bashANYtrcaseuppercase
bash
tr '[:lower:]' '[:upper:]' < input.txt
Notes

Character classes make case conversions portable.

Convert uppercase to lowercase

Translate text to lowercase.

bashANYtrcaselowercase
bash
tr '[:upper:]' '[:lower:]' < input.txt
Notes

Useful for normalization before comparison or counting.

Delete characters

Remove all digits from a stream.

bashANYtrdeletecharacters
bash
tr -d '0-9' < file.txt
Notes

Character deletion is useful for quick sanitization tasks.

Join lines into one line

Replace newlines with spaces.

bashANYtrnewlinesjoin-lines
bash
tr '
' ' ' < lines.txt
Notes

Useful when flattening simple lists into a single line of text.

Collapse repeated blank lines

Squeeze consecutive newline characters.

bashANYtrsqueezenewlines
bash
tr -s '
' < file.txt
Notes

Can help normalize output before diffing or exporting.

join and comm

Compare two sorted files and merge common keys.

Join two files on a common field

Merge records based on matching first fields.

bashANYjoinmergesorted-files
bash
join users.txt departments.txt
Notes

Both input files should usually be sorted on the join field first.

Join on a different field number

Specify alternate join fields.

bashANYjoinfields
bash
join -1 2 -2 1 left.txt right.txt
Notes

Use `-1` and `-2` to choose the join field in each file.

Show common lines between sorted files

Print only lines present in both files.

bashANYcommcomparecommon-lines
bash
comm -12 <(sort file1.txt) <(sort file2.txt)
Notes

`comm` compares two sorted inputs and can suppress selected columns.

Show lines only in the first file

List lines unique to the first sorted file.

bashANYcommdifference
bash
comm -23 <(sort file1.txt) <(sort file2.txt)
Notes

Useful for simple set-difference operations in shell scripts.

Show lines only in the second file

List lines unique to the second sorted file.

bashANYcommdifference
bash
comm -13 <(sort file1.txt) <(sort file2.txt)
Notes

Great for comparing inventories, exports, or generated lists.

Recommended next

No recommendations yet.