cut command is a command-line utility that allows you to cut out sections of a specified file or piped data and print the result to standard output. The command cuts parts of a line by field, delimiter, byte position, and character.
In this tutorial, you will learn what the
cut command is and how to use it.
- A system running Linux.
- Access to a terminal.
cut Command Syntax
cut command takes the following syntax:
cut [option] [file]
[option] is necessary. Otherwise, the command outputs an error. Available options are described in the following section.
[file], enter the name of the file you want to process. Not specifying a filename instructs the
cut command to read from the standard input, in which case
cut filters the pipeline. If you specify multiple filenames, the
cut command concatenates the requested content.
Note: Another Linux command that produces formatted outputs is the awk command.
cut Command Options
cut command options provide instructions on using a delimiter, cutting by byte position, field, or character. Use a single option for each
cut command you run.
The available options are:
|Select using a specified field, a field set, or a field range.
|Select using a specified byte, a byte set, or a byte range.
|Select using a specified character, a character set, or a character range.
|Used to specify a delimiter to use instead of the default TAB delimiter.
|When specified, this option instructs
cut to display all the bytes, characters, or fields, except the selected.
|The default setting is to print the lines that don't contain delimiter characters. Specifying the
-s option instructs cut not to print the lines that don't contain delimiters.
cut uses the input delimiter as the output delimiter. Specifying the
--output-delimiter option allows you to specify a different output delimiter.
-c options take the
LIST argument, which is one of the following:
- An integer
Nrepresenting a byte, field or character, starting from 1.
- Multiple integers, comma-separated.
- A range of integers.
- Multiple integer ranges, comma-separated.
Each range can be one of the following:
N-- Starts from the integer
N(field, byte or character) up to the end of the line.
N-M- From the integer
Nup to integer
-M- From the first field, byte, or character, up to the specified
Mfield, byte, or character.
Linux cut Examples
Below are the most common
cut command use cases.
Important: Pay attention to the locale of the file/command output you are processing. Cutting characters or bytes in a language other than English may produce an incorrect output if the character in question is longer than one byte.
Cut by Bytes
-b option allows you to extract data using bytes. The syntax is:
cut -b [LIST] [file]
[LIST] argument are the bytes to extract from each line of
Depending on what you want to extract, you can cut a single byte, multiple bytes, or a byte range. To cut from a specific file, specify the filename at the end of the command. For example, we will use employees.txt for testing:
Note: Use the cat command to show a file's contents in the terminal.
To extract the first byte from each input line, run:
cut -b 1 employees.txt
The command prints only the first byte from each file input line.
The following example shows how to pipe into the who command and extract the first 5 bytes from the output:
who | cut -b -5
Cut by Characters
To cut by characters, specify the
-c option. Cutting by characters is similar to cutting by bytes, except you need to specify the character position rather than the byte position. The syntax is:
cut -c [LIST] [file]
[LIST] argument specifies the characters to be extracted from each line of
cut -c 10- employees.txt
The command extracts everything from character 10 until the end of the line from each line of employees.txt. The results are printed in standard output.
The following example shows the result when a file isn't specified and
cut reads its input from standard input. Take a look at the
who command output:
The output indicates that one user is currently logged in. Use the
cut command to extract the logged-in user's username from the
who command's output:
who | cut -c 1-8
In the example above, we instruct
cut to extract characters 1 through 8 from each line of input. In case of multiple results, sort the results by appending the command with
Note: To know the character position, run the
who command first and count out the appropriate character positions. The
who command's output has a fixed format, so the character positions don't change. However, data isn't always organized in a fixed manner in every command output, so make sure to check the output before piping it.
cut to extract multiple different characters from a line. For example, display the username and login time of all logged-in users:
who | cut -c 1-8,18-
Cut Based on a Delimiter
If the fields aren't separated by the default tab character, use the
-d option to specify a different delimiter. That means that the character specified after the
-d option is considered the separator in the lines. The syntax is:
cut -d [delimiter] [file]
In place of the
[delimiter] argument, specify the delimiter you want. You can use any character as a delimiter. Using the
cut command to extract fields from a file without specifying the
-d option means that the default delimiter is the tab character.
In the following example, we use whitespace as a delimiter and print the second field:
echo "phoenixNAP is a global IT services provider" | cut -d ' ' -f 2
Note: The echo command prints out a text string you provide as an output message.
Cut by Fields
When piping into commands whose output doesn't have a fixed format (e.g., the
who command), the
-c option isn't helpful. Using the
-f option to separate by fields is a better choice in that case.
cut -f 2 employees.txt
In the example above, we used the
-f option to extract the second field from the employees.txt file.
The data in the
/etc/passwd file isn't aligned in the same way as the data in the
who command output. Thus, you cannot extract all the users on the system by relying on the character number.
However, the fields in the
/etc/passwd file are delimited by a colon. Hence, count the number of colons to extract the same fields. For example:
cut -d: -f1,6 /etc/passwd
The output returns each user in the system and their home directory, corresponding to fields 1 and 6, respectively.
Complement a Selection
--complement option prints everything except for the character/byte/field at the specified position. For example, the following command prints all fields except the first and third:
cut employees.txt -f 1 --complement
Specify an Output Delimiter
When specifying multiple characters/bytes/fields, the
cut command concatenates the output without a delimiter. Specify a delimiter in the output using the
For example, to set the output delimiter to
_ (underscore), use:
cut employees.txt -f 1,3 --output-delimiter='_'
Note: If you are just starting with Linux commands, take a look at the comprehensive overview in our article Linux Commands All Users Should Know.
You now know what the Linux
cut command is and how to use it to process a file or command output. Feel free to test out the different options to get used to them and maximize your use of the command line for manipulating data and command outputs.