When you are parsing logs or system configuration files in the Linux terminal, you frequently encounter data formatted in strict, predictable columns. For example, the /etc/passwd file contains user data separated by colons, and CSV files separate data with commas. If you only need to extract one specific piece of information from each line (like pulling just the usernames out of the password file), using complex programming tools like awk or sed is overkill. The cut command is a dedicated, lightweight utility designed explicitly to slice vertical sections out of text files.
Extracting Data by Delimiter (Fields)
The most common and powerful way to use cut is to slice a file based on a specific character that separates the data (called a delimiter). In cut terminology, the extracted columns are called “fields.”
To extract data, you use the -d flag to define the delimiter, and the -f flag to specify which field number you want to print.
Let’s use the /etc/passwd file as an example. The data looks like this: root:x:0:0:root:/root:/bin/bash. The data is separated by colons (:). If we want to extract just the first column (the usernames):
cut -d ':' -f 1 /etc/passwd
The command will instantly output a clean, vertical list of every username on the system.
You can also extract multiple fields simultaneously. If you want the username (field 1) and their default shell (field 7), you separate the field numbers with a comma:
cut -d ':' -f 1,7 /etc/passwd
Extracting Data by Character Position
Sometimes, data is not separated by a clean delimiter like a comma. Instead, it is formatted with strict character limits (fixed-width formatting). In these cases, you can use the -c (character) flag to slice the file based on the exact character position on the line.
For example, if you have a file containing a list of serial numbers, and you know the manufacturer code is always the first 4 characters of the line, you can extract a range of characters:
cut -c 1-4 serials.txt
This tells cut to grab the 1st through the 4th characters of every line and discard the rest.
You can also extract from a starting point to the absolute end of the line by omitting the second number. To chop off the first 5 characters and print everything else:
cut -c 6- serials.txt
Piping Output to Cut
While cut can read files directly, it is most frequently used at the end of a command pipeline to clean up the output of other tools. For example, if you want to find all running processes owned by the root user, you might run ps aux | grep root. If you only want the Process IDs (which is the second column of the ps output, separated by spaces), you can pipe it directly into cut:
ps aux | grep root | tr -s ' ' | cut -d ' ' -f 2
(Note: Because ps uses multiple, variable spaces between columns, we first pipe it through tr -s ' ' to squeeze all repeated spaces into a single space, allowing cut to process it accurately).