When you dump data from a database or a system log into a text file, it is rarely formatted perfectly for your needs. You often end up with a massive block of text containing usernames, IP addresses, dates, and server statuses all mashed together on a single line, separated only by colons or spaces (like a CSV file). If you only need to extract the IP addresses and ignore the rest of the garbage, manually deleting the other words line by line is impossible. This is where the Linux cut command shines. The cut command is a string manipulation tool designed specifically to slice through lines of text, isolate specific columns (fields) based on a delimiter you define, and print only the data you actually want.
Understanding Delimiters and Fields
To use cut effectively, you must understand two concepts:
- Delimiter (
-d): The specific character that separates the chunks of data on a single line. In a CSV file, the delimiter is a comma (,). In the Linux/etc/passwdfile, the delimiter is a colon (:). - Field (
-f): The specific “column” of data you want to extract, counting from left to right starting at 1.
Extracting a Single Column
The most common use of cut is to extract a single piece of data from a structured system file.
For example, every user account on a Linux machine is listed in the /etc/passwd file. Each line looks like this:
root:x:0:0:root:/root:/bin/bash
If you want to print a simple list of all the usernames on the server, you only want the very first word before the first colon. You don’t care about the UIDs or the bash paths.
- Open your terminal.
- Type the command:
cut -d ':' -f 1 /etc/passwd - Press Enter.
Explanation:
-d ':'tellscutto use the colon as the separating boundary.-f 1tellscutto grab the first chunk of text on the left side of the first colon.
The terminal will instantly output a clean, vertical list of every username on the system.
Extracting Multiple Columns
You are not limited to a single field. You can extract multiple columns simultaneously by separating the field numbers with a comma.
If you want to extract both the username (Field 1) and their default home directory (Field 6) from that same file, you would run:
cut -d ':' -f 1,6 /etc/passwd
The output will stitch those two specific columns back together, keeping the original colon delimiter between them (e.g., root:/root).
Cutting by Character Position
Sometimes, data isn’t separated by a neat delimiter. Instead, the data you want is always located at a specific character position on every line (for example, the first 10 characters of every line contain a date). In this scenario, you ignore delimiters and use the -c (character) flag.
cut -c 1-10 server_log.txt
This command acts like a pair of scissors, slicing off everything from the 11th character onwards and printing only the first 10 characters of every single line.
You can also use open-ended ranges. To cut off the first 5 characters and print everything from character 6 to the end of the line, you would use:
cut -c 6- server_log.txt