Learn › Linux Foundations › Text Processing
cut - a hands-on Linux lab on a real virtual machine.
Slice a column out of every line with cut: fields by delimiter using -d and -f, characters by position with -c, the four list forms (N, N,M, N-M, N-), and --complement to invert a selection. Meet the missing-list error and the default-tab trap, all in the practice terminal on a tiny colon-separated fixture.
Most files you meet are not free-flowing prose. They are records, one per line, with fields separated by a marker: a colon, a comma, a tab. The file /etc/passwd uses colons. A spreadsheet exported to text uses commas. You rarely want the whole line. You want the third column, or the first, or everything after the first.
There is one command whose entire job is to slice a column out of every line and throw the rest away. It is called cut, and it is the fastest way to pull one field out of a structured file.
The black boxes in this lesson are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. At the end of the module one real Linux machine boots just for you, and you run the full mission there.
Everything in this lesson runs against one tiny file, people.txt. It holds exactly two lines, and each line has three fields separated by a colon: a name, an age, and a city.
alice:30:NYC
bob:25:LA
So the colon is the delimiter, the character that marks where one field ends and the next begins. Field 1 is the name, field 2 is the age, field 3 is the city. Keep that picture in your head, because every command below is just a different way of asking cut which of those three fields to keep.
To slice a field out, cut needs two things from you. First, what separates the fields: the -d flag, short for delimiter, followed by the character. Here that is a colon, so -d:. Second, which field to keep: the -f flag, short for field, followed by a number. Field 1 is the name.
Put them together and ask cut for the name column only:
cut -d: -f1 people.txt
prompt: student@linuxcamp:~$ answer: cut -d: -f1 people.txt output: alice bob hint: -d: sets the delimiter to a colon, -f1 keeps field one: cut -d: -f1 people.txt
Two names, one per line, and nothing else. cut walked each line, split it on the colon into three fields, kept field 1, and dropped the rest. The age and the city are gone. That is the core move: -d says how the line is divided, -f says which piece you want back.
-f does not stop at a single number. Give it a comma-separated list and it keeps each of those fields. Ask for fields 1 and 3, the name and the city, and skip the age in the middle:
cut -d: -f1,3 people.txt
prompt: student@linuxcamp:~$ answer: cut -d: -f1,3 people.txt output: alice:NYC bob:LA hint: List the fields you want with a comma, no spaces: cut -d: -f1,3 people.txt
Look closely at what came back: alice:NYC, with a colon still in it. When you keep more than one field, cut glues them back together using the same delimiter you gave it. The age (field 2) is gone, so field 1 and field 3 sit next to each other joined by a colon. That reassembly with the delimiter is worth remembering; it surprises people who expected a space.
The field list understands ranges too. -f1-3 means fields 1 through 3. And a number with a trailing dash and nothing after it, -f2-, means "field 2 to the end of the line, however many that is." That open-ended form is the one you reach for when you want everything except the first column.
Ask for field 2 onward, which here is the age and the city:
cut -d: -f2- people.txt
prompt: student@linuxcamp:~$ answer: cut -d: -f2- people.txt output: 30:NYC 25:LA hint: A trailing dash means to the end: -f2- is field two onward: cut -d: -f2- people.txt
Everything from field 2 rightward came back, 30:NYC and 25:LA, again rejoined by the colon. That is the four list forms in one place: 1 for a single field, 1,3 for a pick-list, 1-3 for a closed range, and 2- for an open range that runs to the end. The same forms work for -f fields and for -c characters, which is the next flag.
Sometimes a line has no delimiter at all, just fixed columns of characters, like a report where every name sits in the first ten spaces. For that, cut counts raw character positions with the -c flag, short for characters. -c1-5 means keep characters 1 through 5 of every line, counting from the left, delimiter or no delimiter.
Run it on the same file and watch it ignore the colons entirely:
cut -c1-5 people.txt
prompt: student@linuxcamp:~$ answer: cut -c1-5 people.txt output: alice bob:2 hint: -c counts character positions, not fields: cut -c1-5 people.txt
Read those two lines carefully, because they prove the point. Line one, alice:30:NYC, gives back alice: its first five characters happen to be the whole name. But line two, bob:25:LA, gives back bob:2: characters one through five are b, o, b, colon, two. -c does not know or care about fields or colons. It counts positions blindly. That is the difference from -f: -f respects the delimiter, -c respects only the ruler.
cut has two mistakes that nearly every beginner makes once. Meet them here, in the sandbox, instead of on a real file.
Trap one: you forgot to say what to keep. If you give cut a delimiter but never tell it which field or character to keep, it refuses to guess. Run cut -d: people.txt with no -f:
prompt: student@linuxcamp:~$ answer: cut -d: people.txt output: cut: you must specify a list of bytes, characters, or fields Try 'cut --help' for more information. hint: cut needs a -f or -c to know what to keep. Run it without one to read the error: cut -d: people.txt
The literal message is cut: you must specify a list of bytes, characters, or fields. It means exactly what it says: you set the delimiter but never named a field or a character range, so cut has no idea what to hand back. The fix is always to add a -f LIST (for fields) or a -c LIST (for characters). A -d on its own is never enough.
This is the one that wastes an afternoon. If you never pass -d, cut does not default to a colon or a space. Its default delimiter is a single TAB character. So on a colon-separated file, running cut -f1 with no -d finds no tab anywhere. It decides the entire line is one giant field 1 and hands the whole line straight back, looking like cut did nothing.
See it happen. Ask for field 1 but leave out the -d::
cut -f1 people.txt
prompt: student@linuxcamp:~$ answer: cut -f1 people.txt output: alice:30:NYC bob:25:LA hint: With no -d, cut splits on TAB, finds none, and returns the whole line: cut -f1 people.txt
Nothing got cut. The whole line came back because there is no tab in people.txt for the default delimiter to split on, so each line is treated as a single field. The moment cut -f1 seems to print the entire line unchanged, check for a missing -d. On a colon file you MUST pass -d:, on a comma file -d,. The default tab bites everyone once.
Scaffolding off. No command is shown this time. You have every piece you need.
You want the opposite of what you started with. Not field 1, but every field EXCEPT field 1: the age and the city, rejoined by the colon, exactly 30:NYC and 25:LA. One flag flips a selection into its inverse: --complement, which keeps everything you did NOT name. Combine it with the delimiter and the field you want to exclude.
prompt: student@linuxcamp:~$ answer: cut --complement -d: -f1 people.txt output: 30:NYC 25:LA hint: --complement inverts the selection. Name the field to drop, field 1, and add the colon delimiter: cut --complement -d: -f1 people.txt
That is the everyday move, typed from memory. --complement told cut to keep every field you did NOT list, so naming -f1 dropped the name and returned fields 2 and 3, 30:NYC and 25:LA. Notice you could have gotten the same result with -f2-, an open range from field 2. Two roads to one answer is normal in the shell, and picking the one you remember is the whole skill the real machine will ask of you.
You earned this cheat sheet. Every row is a form of cut you have already run:
The list forms you can now write for either -f or -c: 1 (one), 1,3 (a pick-list), 1-3 (a closed range), and 2- (open to the end). And the two rules that save you: cut needs a -f or -c or it errors, and with no -d it splits on a TAB, not a colon or a space.
cut is deliberately simple: one delimiter character, columns kept whole. When a file uses different separators on different lines, or you need to reorder or reformat columns, that is the job of awk, which has its own lesson later in this module. Reach for cut first; it is smaller and faster for the common case of pulling one clean column.
The practice terminal has shown you every form of cut: fields by delimiter with -d and -f, the four list forms, characters by position with -c, --complement to invert a selection, and the two traps of a missing list and the default tab. Every one of those you typed yourself.
The Text Processing module ends with one real Linux machine, the text-processing capstone mission, and that is where you slice real files. A VM boots just for you. Its objectives use exactly what you practiced across this module: cutting columns, plus the sibling commands grep, sort, and tr, each of which has its own lesson. One difference from here: the mission shows no commands. You read the objective, recall the flag, and type it. That recall is what makes it stick.
Finish the other Text Processing lessons, then go slice a real file for yourself.
Practice cut in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.