cut Command in Linux: Practical Examples & Cheat Sheet

cut Command in Linux: Practical Examples & Cheat Sheet

Tested on Ubuntu 22.04 / 24.04 and Debian 12

The cut command is a standard Linux utility used to extract specific sections from each line of a file or command output. It works well for simple, structured text where columns are separated by fixed positions or delimiters.

Typical use cases include:

  • Extracting columns from text files
  • Parsing delimited data such as CSV-like output
  • Filtering command output in shell pipelines

cut Command - Quick Cheat Sheet

Taskcut Command
Extract specific characters from each linecut -c 1-5 filename
Extract specific byte positionscut -b 1-10 filename
Extract a single column from filecut -f 1 filename
Extract multiple columnscut -f 1,3 filename
Extract range of columnscut -f 2-4 filename
Use custom delimitercut -d ':' -f 1 /etc/passwd
Extract username from passwd filecut -d ':' -f 1 /etc/passwd
Extract column from CSV filecut -d ',' -f 2 data.csv
Extract characters from command outputcommand | cut -c 1-10
Exclude selected field (complement)cut --complement -f 2 file.txt
Suppress lines without delimitercut -s -d ':' -f 1 file
Combine cut with pipesps aux | cut -c 1-20
Extract fields and change delimitercut -d ':' -f 1,3 --output-delimiter=' '
Use cut in pipeline`ps aux
Display cut versioncut --version
Display help messagecut --help

This quick reference summarizes the most commonly used cut command examples in Linux for extracting specific columns, fields, or character ranges from files and command output.


How the cut command works

The cut command operates on bytes, characters, or fields. One of these must be specified for the command to work.

Supported selection formats:

  • N → select the Nth byte, character, or field
  • N-M → select from position N to M
  • N- → select from position N to the end
  • -M → select from the beginning up to position M

General syntax:

text
cut OPTION... [FILE]...

Selecting bytes with cut command

Extract specific bytes

text
cut -b 1 file.txt

Extracts only the first byte from each line of the file.
Byte positions start from 1, not zero.

This is useful for fixed-format files where each column occupies a known byte offset.

Extract a range of bytes

text
cut -b 4-11 file.txt

Extracts bytes starting from position 4 through 11 on each line.

You can also extract non-contiguous byte positions by separating them with commas.

text
cut -b 1,3,5 file.txt

This prints only the selected byte positions, in the order specified.


Selecting characters from text

Extract specific characters

text
cut -c 2-12 file.txt

Extracts characters from position 2 through 12 on each line.

Spaces and TAB characters are counted as single characters, and character positions are counted from 1.

This is commonly used for trimming prefixes or extracting portions of text lines.


Selecting fields using delimiters

Extract fields using default delimiter (TAB)

By default, cut treats the TAB character as the field delimiter.

text
cut -f 1 file.txt

Extracts the first field from each line.

If a line does not contain a TAB, the entire line is printed as-is. This behavior is important to understand when working with mixed input.

Use a custom field delimiter

Use the -d option to specify a delimiter other than TAB. The delimiter must be a single character.

text
cut -d ':' -f 1 /etc/passwd

Extracts the first field from /etc/passwd, where fields are separated by colons.

This is commonly used for parsing:

  • user databases
  • log files
  • colon- or comma-separated text

Extract multiple fields

You can extract more than one field by separating field numbers with commas.

text
cut -d ':' -f 1,3 /etc/passwd

This extracts the first and third fields from each line.

Fields are printed in the order specified.

Extract a range of fields

You can extract a continuous range of fields using a hyphen.

text
cut -d ':' -f 1-3 /etc/passwd

Extracts fields 1 through 3 from each line.

To select from a field position to the end:

text
cut -d ':' -f 3- /etc/passwd

By default, cut prints lines even if the delimiter is missing. Use the -s option to suppress such lines.

text
cut -d ':' -f 1 -s file.txt

Only lines containing the delimiter are printed. This is useful when filtering noisy or inconsistent input.

Limitation when using spaces as delimiters

The cut command treats each character literally. This means it cannot handle multiple consecutive spaces as a single delimiter.

text
cut -d ' ' -f 1 file.txt

If your input uses variable-width spaces, prefer tools like awk instead.


Working with ranges and complements

Range and complement options allow you to fine-tune exactly which parts of each line are included or excluded in the output.

These options work with bytes (-b), characters (-c), and fields (-f).

Select from a position to the end

Use this form when you want everything from a specific position onward.

text
cut -c 5- file.txt

Prints characters starting from position 5 to the end of each line.

Select from the beginning to a position

Use this form when you want everything up to a specific position.

text
cut -c -10 file.txt

Prints characters from the beginning of the line up to position 10.

Combine multiple ranges

You can combine multiple ranges and positions using commas.

text
cut -c 1-5,10-15 file.txt

This extracts characters 1–5 and 10–15 from each line.

Ranges are processed in the order specified.

Exclude selected output using complement

Use --complement to print everything except the selected portion.

text
cut --complement -f 2 file.txt

Prints all fields except the second field. The --complement option works with -b, -c, and -f.


Using cut in pipelines

Extract columns from command output

Use cut to extract fixed-width columns from commands that produce aligned output.

text
ps aux | cut -c 1-20

This extracts the first 20 characters from each line of process output.

Process delimited output from other commands

Use field-based selection when command output is delimiter-separated.

text
df -h | cut -d ' ' -f 1

This extracts the filesystem column from disk usage output.

Note: If the delimiter appears multiple times (such as variable spaces), tools like awk may be more reliable.

Combine cut with grep for filtering

cut works well after filtering input with grep.

text
ps aux | grep root | cut -c 1-30

This filters processes owned by root and then extracts relevant columns.

Combine cut with sort or uniq

Use cut to extract a column before sorting or deduplicating data.

text
cut -d ':' -f 1 /etc/passwd | sort | uniq

This extracts usernames and removes duplicates.


Summary

The cut command in Linux is a fast and simple tool for extracting specific parts of text. It is ideal for byte, character, and field-based selection in scripts and command pipelines. Understanding delimiters, ranges, and complements allows you to handle most everyday text-processing tasks efficiently.


Also Read


Further Reading

Rohan Timalsina

Rohan Timalsina

is a technical writer and Linux enthusiast who writes practical guides on Linux commands and system administration. He focuses on simplifying complex topics through clear explanations.