Mastering the Linux Cut Command: A Beginner’s Guide with Examples

In Linux, the first command that might come to mind for displaying a file’s content on standard output is cat. However, there are instances when you need to exclude certain parts and print only specific sections of a file. Fortunately, there’s a utility named cut that’s designed for this purpose.

This article explains how to use the cut command with clear examples. Please note that the instructions provided have been tested on an Ubuntu 24.04 LTS machine.

Linux cut Command

The cut command in Linux enables users to remove sections from each line of files. The syntax for the cut command is as follows:

cut OPTION... [FILE]...

According to the man page, the utility serves this purpose:

Print selected parts of lines from each FILE to standard output.
    With no FILE, or when FILE is -, read standard input.

Below are some examples to help you understand how this utility functions.

Q1. How to Use the Cut Command?

The cut command requires you to specify a list of bytes, characters, or fields. To specify bytes, use the -b option.

For instance, consider a file named file1.txt containing the following line:

abcdefghijklmnopqrstuvwxyz

To display only the first three bytes, use the -b option like this:

cut -b1,2,3 file1.txt

The output will be:

abc

You can also specify a range:

cut -b1-10 file1.txt

This will produce the following output:

abcdefghij

The cut command allows you to use a hyphen (-) with a number to indicate whether you want to display all bytes after a certain point or before it.

For example, the following command displays all bytes from and including the fifth position:

cut -b5- file1.txt

The command below will show the first five bytes:

cut -b-5 file1.txt

How to use the cut command

Q2. How to Work with Characters?

In some cases, the file passed to the cut command may contain multi-byte characters. It’s better to use the -c option in such scenarios to accurately specify which characters you want to display or remove.

Consider the special character ♣, which spans multiple bytes. For text streams containing such characters, it’s advisable to use -c rather than -b. Both work similarly in terms of functionality.

Q3. How Does Cut Work with Delimiters?

The cut command can also work with delimiters using the -d option. Suppose the input file includes comma-separated fields:

Howtoforge, HTF, howtoforge.com
    FaqForge, FF, faqforge.com

To display only the first and third entries, use this command:

cut -d, -f1,3 file1.txt

Here, the -f option specifies the fields you want to display.

Conclusion

The cut command is a time-saver when the task involves selecting specific content from a file. This tutorial explored some basic command-line options available with this toolkit. For more information, you can refer to the tool’s man page.

Frequently Asked Questions (FAQ)

What is the purpose of the cut command in Linux?

The cut command is used to remove sections from each line of files and present selected parts to standard output.

Can cut handle multi-byte characters effectively?

Yes, using the -c option instead of -b allows the cut command to correctly handle multi-byte characters.

What does the -d option do in the cut command?

The -d option specifies a delimiter to use for separating fields in the input data.

How do I specify a range of bytes or characters using cut?

You can specify a range using the -b or -c options followed by numbers separated by a hyphen, e.g., -b1-10.