Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

cut

Take parts of each line: fields split by a separator, or character positions.

Level
RHCSA exam
Group
Text and search
Needs root
No
Package
coreutils

What it does

cut prints selected parts of every line. With -d you give the separator (one character) and with -f the field numbers. With -c you select characters by position.

It is ideal for files with a fixed separator like /etc/passwd (:) or CSV (,). For columns separated by a varying number of spaces, awk is better.

How to write it

cut OPTION... [FILE]...

Options 15

Most used

-f, --fields=list
Select these fields, like 1,3 or 2-4 or 3-.
-d, --delimiter=char
The field separator (default: TAB).
-c, --characters=list
Select these character positions.
-b, --bytes=list
Select these byte positions.
-s, --only-delimited
Do not print lines that have no separator.
--complement
Print everything except the selected parts.
--output-delimiter=string
Use this separator in the output.
-z, --zero-terminated
Lines end with NUL.
-n
Ignored.
--help
Show help.
--version
Show the version.

LIST forms

N
Only field (or character) N.
N-M
From N to M.
N-
From N to the end of the line.
-M
From the first to M.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1All user names

    cut -d: -f1 /etc/passwd

    Field 1 of /etc/passwd is the user name.

  2. 2User name and shell

    cut -d: -f1,7 /etc/passwd
    root:/bin/bash

    Fields 1 and 7, still joined by :.

  3. 3The first 8 characters of each line

    cut -c1-8 /etc/passwd

    Character positions, not fields.

  4. 4Columns of a CSV file

    cut -d, -f2,4 users.csv

    Fields 2 and 4 (note: quoted commas inside fields break this; use a CSV tool then).

  5. 5Everything except field 2

    cut -d: -f2 --complement /etc/passwd

    Removes the password placeholder column.

  6. 6Change the output separator

    cut -d: -f1,6 --output-delimiter=' -> ' /etc/passwd
    root -> /root

    Readable output for a report.

  7. 7Only the group names

    cut -d: -f1 /etc/group | sort

    All local groups, sorted.

  8. 8Get the IP address part of an address/prefix

    ip -4 -o addr show dev enp1s0 | awk '{print $4}' | cut -d/ -f1
    192.168.122.10

    cut -d/ -f1 removes the /24.

  9. 9Field 3 to the end

    cut -d' ' -f3- server.log

    Drops the date and time columns of a log line.

  10. 10Skip lines without the separator

    cut -d= -f2 -s app.conf

    Comment lines without = are not printed.

  11. 11The size column of a TAB separated report

    cut -f2 report.tsv | sort -n | tail -1

    TAB is the default separator.

Watch out

  • cut uses exactly one separator character. Many spaces in a row make empty fields; use awk or tr -s ' ' first.
  • Field numbers start at 1.

Tips to remember

  • cut -d: -f1 = first column of passwd-style files.