Table of Contents
In this article, I will take you through 52 Useful cut command in Linux/Unix with Examples for Beginners. cut command is a very useful utility in Linux/UNIX based System to cut specific area of text based on delimiter and field. This tool proves to be much useful when used with other Linux tools like sed, grep, awk, more, less etc. We will look into the multiple usages of this tool with the help of examples in the below sections.
SYNOPSIS
cut OPTION… [FILE]…
Cut command in Linux/Unix with Examples
Also Read: How to Install LEMP (Linux, Nginx, MySQL and PHP) Stack on CentOS 8 Using 12 Best Steps
Example 1: How to Check cut command version
If you want to check cut command version then you need to use cut --version
command as shown below. As you can see from below output current installed version is 8.22
.
[root@localhost ~]# cut --version cut (GNU coreutils) 8.22 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by David M. Ihnat, David MacKenzie, and Jim Meyering.
NOTE:
root
user to run all the below commands.You can use any user with sudo
access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo
access to the User.Example 2: How to Show the nth byte of /etc/passwd file in Linux/Unix
If you want to show the nth byte of /etc/passwd
file then you need to use -b
option with cut command as shown below. In this example we are trying to cut the 3rd byte from /etc/passwd file using cut -b 3 /etc/passwd
command.
[root@localhost ~]# cut -b 3 /etc/passwd o n e m : n u l i e m p
-b : select only these bytes. More info on cut command Man Page.
Example 3: How to Display 1st Field of /etc/passwd file based on colon(:) separator
If you want to show the 1st field of /etc/passwd
file based on colon separator then you need to use -d
option with cut command as shown below. In this example, we are trying to cut the first field based on colon separator using cut -d ":" -f1 /etc/passwd
command.
[root@localhost ~]# cut -d ":" -f1 /etc/passwd root bin daemon adm lp sync shutdown halt mail operator games ftp nobody systemd-network dbus polkitd sshd postfix
-d : use DELIM instead of TAB for field delimiter. More info on cut command Man Page.
-f : select only these fields; also print any line that contains no delimiter character, unless the -s option is specified.
Example 4: How to cut from 1st field to 3rd field based on Colon(:) Separator
If you want to cut 1st field to 3rd field from /etc/passwd
file based on Colon(:)
separator then you need to use -f1-3
option with cut command as shown below.
[root@localhost ~]# cut -d ":" -f1-3 /etc/passwd root:x:0 bin:x:1 daemon:x:2 adm:x:3 lp:x:4 sync:x:5 shutdown:x:6 halt:x:7 mail:x:8 operator:x:11 games:x:12 ftp:x:14 nobody:x:99 systemd-network:x:192 dbus:x:81 polkitd:x:999 sshd:x:74
Example 5: How to Display 1st and 6th Field of /etc/passwd based on Colon(:) Separator
If you want to display 1st and 6th field of /etc/passwd
file based on Colon(:)
separator then you need to use -f1,6
option with cut command as shown below.
[root@localhost ~]# cut -d ":" -f1,6 /etc/passwd root:/root bin:/bin daemon:/sbin adm:/var/adm lp:/var/spool/lpd sync:/sbin shutdown:/sbin halt:/sbin mail:/var/spool/mail operator:/root games:/usr/games ftp:/var/ftp nobody:/ systemd-network:/ dbus:/ polkitd:/ sshd:/var/empty/sshd postfix:/var/spool/postfix chrony:/var/lib/chrony
Example 6: How to Display First 20 characters of /etc/ssh/sshd_config file
If you want to display first 20 characters of /etc/ssh/sshd_config
file then you need to use cut -c -20 /etc/ssh/sshd_config
command as shown below.
[root@localhost ~]# cut -c -20 /etc/ssh/sshd_config # $OpenBSD: sshd_con # This is the sshd s # sshd_config(5) for # This sshd was comp # The strategy used # OpenSSH is to spec # possible, but leav # default value. # If you want to cha # SELinux about this # semanage port -a -
-c : select only these characters
Example 7: How to Check all Uncommented or Set Parameters in /etc/ssh/sshd_config file
If you want to check all Uncommented or set parameters in /etc/ssh/sshd_config
file then you need to use --complement
option with -d "#"
option to ignore all the Lines starting with #
as shown below.
[root@localhost ~]# cut --complement -d "#" -f1- /etc/ssh/sshd_config HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key SyslogFacility AUTHPRIV AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication yes ChallengeResponseAuthentication no GSSAPIAuthentication yes GSSAPICleanupCredentials no UsePAM yes X11Forwarding yes
–complement : complement the set of selected bytes, characters or fields.
Example 8: How to cut only a Single Byte from Input Stream using cut command
If you want to cut only first byte from Input Stream then you need to use -b 1
option with cut command as shown below.
[root@localhost ~]# echo "This is from CyberIThub" | cut -b 1 T
Example 9: How to cut First 4 Bytes from Input Stream using cut command
If you want to cut only first byte from Input Stream then you need to use -b 1
option with cut command as shown below.
[root@localhost ~]# echo "This is from CyberIThub" | cut -b 1-4 This
Example 10: How to cut from 5th to 12th bytes from Input Stream using cut command
If you want to cut from 5th byte to 12th byte from Input Stream then you need to use -b 5-12
option with cut command as shown below.
[root@localhost ~]# echo "This is from CyberIThub" | cut -b 5-12 is from
Example 11: How to cut all the bytes after 5th byte from Input Stream using cut command in Linux
If you want to cut all the bytes after 5th byte from Input Stream then you need to use -b 5-
option with cut command as shown below.
[root@localhost ~]# echo "This is from CyberIThub" | cut -b 5- is from CyberIThub
Example 12: How to cut all the bytes upto 5th byte from Input Stream using cut command in Linux
If you want to cut all the byte upto 5th byte from Input Stream then you need to use -b -5
option with cut command as shown below.
[root@localhost ~]# echo "This is from CyberIThub" | cut -b -5 This
Example 13: How to cut all the bytes upto 5th byte from /etc/ssh/sshd_config file
If you want to cut all the bytes up to 5th byte from /etc/ssh/sshd_config
then you need to use -b -5
option with cut command as shown below.
[root@localhost ~]# cut -b -5 /etc/ssh/sshd_config # $Op # Thi # ssh # Thi # The # Ope # pos # def # If # SEL # sem
Example 14: How to cut all the bytes after 5th byte from /etc/ssh/sshd_config file
If you want to cut all the bytes after 5th byte from /etc/ssh/sshd_config
file then you need to use -b 5-
option with cut command as shown below.
[root@localhost ~]# cut -b 5- /etc/ssh/sshd_config penBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $ is is the sshd server system-wide configuration file. See hd_config(5) for more information. is sshd was compiled with PATH=/usr/local/bin:/usr/bin e strategy used for options in the default sshd_config shipped with enSSH is to specify options with their default value where ssible, but leave them commented. Uncommented options override the fault value. you want to change the port on a SELinux system, you have to tell Linux about this change. manage port -a -t ssh_port_t -p tcp #PORTNUMBER t 22 ressFamily any tenAddress 0.0.0.0 tenAddress ::
Example 15: How to cut 5th to 12th Byte from /etc/ssh/sshd_config file
If you want to cut from 5th to 12th byte from /etc/ssh/sshd_config
file then you need to use -b 5-12
option with cut command as shown below.
[root@localhost ~]# cut -b 5-12 /etc/ssh/sshd_config penBSD: is is th hd_confi is sshd e strate enSSH is ssible, fault va you wan Linux ab manage p
Example 16: How to cut only 4th and 12th byte from Input Stream using cut command in Linux
If you want to cut only 4th and 12th byte from Input Stream then you need to use -b 4,12
option with cut command as shown below.
[root@localhost ~]# echo "This is from CyberIThub" | cut -b 4,12 sm
Example 17: How to cut only 4th and 12th byte from /etc/ssh/sshd_config file using cut command
If you want to cut only 4th and 12th byte from /etc/ssh/sshd_config
file then you need to use cut -b 4,12 /etc/ssh/sshd_config
command as shown below.
[root@localhost ~]# cut -b 4,12 /etc/ssh/sshd_config O hh si h he ps o ea fn Eb ep
Example 18: How to cut characters from 1st to 5th field of Input Stream using cut command
If you want to cut characters from 1st field to 5th field of Input stream then you need to use -c 1-5
option with cut command as shown below.
[root@localhost ~]# echo "This is from CyberIThub" | cut -c 1-5 This
Example 19: How to cut 2nd to 10th character from Input Stream using cut command
If you want to cut 2nd to 10th character from Input stream then you need to use -c 2-10
option with cut command as shown below.
[root@localhost ~]# echo "This is from CyberIThub" | cut -c 2-10 his is fr
Example 20: How to cut all the characters after 5th character from Input Stream
If you want to cut all the characters after 5th character from Input Stream then you need to use -c 5-
option with cut command as shown below.
[root@localhost ~]# echo "This is from CyberIThub" | cut -c 5- is from CyberIThub
Example 21: How to cut all the characters after 5th character from /etc/ssh/sshd_config file
If you want to cut all the characters after 5th character from /etc/ssh/sshd_config
file then you need to use -c 5-
option with cut command as shown below. The only difference you can see in this example as compared to above example is that here input is a file instead of an Input Stream.
[root@localhost ~]# cut -c 5- /etc/ssh/sshd_config penBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $ is is the sshd server system-wide configuration file. See hd_config(5) for more information. is sshd was compiled with PATH=/usr/local/bin:/usr/bin e strategy used for options in the default sshd_config shipped with enSSH is to specify options with their default value where ssible, but leave them commented. Uncommented options override the fault value. you want to change the port on a SELinux system, you have to tell Linux about this change. manage port -a -t ssh_port_t -p tcp #PORTNUMBER t 22 ressFamily any tenAddress 0.0.0.0 tenAddress ::
Example 22: How to cut 2nd to 10th character from /etc/ssh/sshd_config file
If you want to cut 2nd to 10th character from /etc/ssh/sshd_config
file then you need to use -c 2-10
option with cut command as shown below.
[root@localhost ~]# cut -c 2-10 /etc/ssh/sshd_config $OpenBSD This is sshd_con This ssh The stra OpenSSH possible default If you w SELinux semanage Port 22 AddressFa
Example 23: How to cut 1st to 5th characters from /etc/ssh/sshd_config file using cut command
If you want to cut 1st to 5th characters from /etc/ssh/sshd_config
file then you need to use -c 1-5
option with cut command as shown below.
[root@localhost ~]# cut -c 1-5 /etc/ssh/sshd_config # $Op # Thi # ssh # Thi # The # Ope # pos # def # If # SEL # sem # #Port #Addr
Example 24: How to check the value of “PasswordAuthentication” parameter in /etc/ssh/sshd_config file using cut command
If you want to check the value of PasswordAuthentication
Parameter in /etc/ssh/sshd_config
file then you need to cut out the second field after grepping the parameter from the file.
[root@localhost ~]# grep -i PasswordAuthentication /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 yes
Example 25: How to check if “PermitEmptyPasswords” value is set or not in /etc/ssh/sshd_config file using cut command
If you want to check some Parameter value is set or not in /etc/ssh/sshd_config
then you can use below cut command with grep command to filter out the values. If you do not see any value in output then it means this value is either not set or it is a commented Parameter.
[root@localhost ~]# grep -i PermitEmptyPasswords /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2
Example 26: How to check the value of “UsePAM” Parameter in /etc/ssh/sshd_config file using cut command
If you want to check the value of UsePAM
parameter from /etc/ssh/sshd_config
file then you need to use below cut command.
[root@localhost ~]# grep -i UsePAM /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 yes
Example 27: How to check the value of “ChallengeResponseAuthentication” in /etc/ssh/sshd_config file using cut command
If you want to check the value of ChallengeResponseAuthentication
parameter in /etc/ssh/sshd_config
file then you need to use below cut command.
[root@localhost ~]# grep -i ChallengeResponseAuthentication /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 no
Example 28: How to check the value of “SyslogFacility” in /etc/ssh/sshd_config file using cut command
If you want to check the value of SyslogFacility
parameter in /etc/ssh/sshd_config
file then you need to use below cut command.
[root@localhost ~]# grep -i SyslogFacility /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 AUTHPRIV
Example 29: How to sort the value of “SyslogFacility” Parameter in /etc/ssh/sshd_config file using cut command
If you sort the cut command output then you can use sort command with cut command as shown below. In this example, we are getting multiple output of SyslogFacility
parameter from /etc/ssh/sshd_config
file which is then sorted with the help of sort command.
[root@localhost ~]# grep -i SyslogFacility /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 | sort -rn AUTHPRIV
Example 30: How to save cut command output in a File using redirection Operator
You can also save the cut command output in a file by using redirection operator as shown below. Here we are checking the value of SyslogFacility
parameter from /etc/ssh/sshd_config
and redirecting the output to output.txt
file.
[root@localhost ~]# grep -i SyslogFacility /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 | sort -rn >> output.txt [root@localhost ~]# cat output.txt AUTHPRIV
Example 31: How to remove the duplicate value of “X11DisplayOffset” from the output
If you want to remove the duplicate value of X11DisplayOffset
parameter from the output then you need to use uniq
command with cut command as shown below.
[root@localhost ~]# grep -i X11DisplayOffset /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 | uniq 10
Example 32: How to check the value of “PermitTTY” in /etc/ssh/sshd_config file using cut command
If you want to check the value of PermitTTY
in /etc/ssh/sshd_config
file then you need to use below cut command in Linux/UNIX.
[root@localhost ~]# grep -i PermitTTY /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 yes
Example 33: How to check the value of “TCPKeepAlive” in /etc/ssh/sshd_config file using cut command
You can also check TCPKeepAlive
parameter value using below cut command in Linux.
[root@localhost ~]# grep -i TCPKeepAlive /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 yes
Example 34: How to check the value of “UseLogin” in /etc/ssh/sshd_config file using cut command
Sometimes you might need to check the value UseLogin
parameter in /etc/ssh/sshd_config
file. You can simply use below cut command to determine the value of 2nd field after grepping UseLogin
parameter from /etc/ssh/sshd_config
file.
[root@localhost ~]# grep -i UseLogin /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 no
Example 35: How to check the value of “PermitTunnel” from /etc/ssh/sshd_config file using cut command
Like above example, if you want to check the value of PermitTunnel
parameter from /etc/ssh/sshd_config
file then you need to use below cut command.
[root@localhost ~]# grep -i PermitTunnel /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 no
Example 36: How to check the value of “PidFile” in /etc/ssh/sshd_config file using cut command
If you want to check the value of PidFile
parameter in /etc/ssh/sshd_config
file then you need to use below cut command.
[root@localhost ~]# grep -i PidFile /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 /var/run/sshd.pid
Example 37: How to check the value of “MaxStartups” in /etc/ssh/sshd_config file using cut command
If you want to check the value of MaxStartups
parameter in /etc/ssh/sshd_config
file then you need to use below cut command.
[root@localhost ~]# grep -i MaxStartups /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 10:30:100
Example 38: How to check the value of “PrintMotd” in /etc/ssh/sshd_config file using cut command
If you want to check the value of PrintMotd
parameter in /etc/ssh/sshd_config
file then you need to use below cut command.
[root@localhost ~]# grep -i PrintMotd /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 yes
Example 39: How to check the value of “X11UseLocalhost” in /etc/ssh/sshd_config file using cut command
If you want to check the value of X11UseLocalhost
parameter in /etc/ssh/sshd_config
file then you need to use below cut command.
[root@localhost ~]# grep -i X11UseLocalhost /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 | uniq yes
Example 40: How to use cut command with sed command to replace Colon(:) with Tab in the Output
If you want to replace colon(:)
of /etc/passwd
file with Tab(\t)
and cut out only the first field then you can use below cut command with sed command.
[root@localhost ~]# sed 's/:/\t/g' /etc/passwd | cut -f 1 root bin daemon adm lp sync shutdown
Example 41: How to Replace Colon(:) with Tab using sed command and cut the output from 1st field to 3rd field.
If you want to replace colon(:) of /etc/passwd
file with Tab and cut out from 1st field to 3rd field then you can use below cut command with sed command.
[root@localhost ~]# sed 's/:/\t/g' /etc/passwd | cut -f 1-3 root x 0 bin x 1 daemon x 2 adm x 3 lp x 4 sync x 5
Example 42: How to Replace Colon(:) with Tab using sed command and cut everything from the output after 3rd Field
You can also use cut command with sed command in Linux. For example, let’s say if you want to replace colon field of /etc/passwd
file with tab in the output then you can use 's/:/\t/g'
expression with sed command and feed the output to cut command to cut everything after 3rd field using cut -f 3-
command as shown below.
[root@localhost ~]# sed 's/:/\t/g' /etc/passwd | cut -f 3- 0 0 root /root /bin/bash 1 1 bin /bin /sbin/nologin 2 2 daemon /sbin /sbin/nologin 3 4 adm /var/adm /sbin/nologin 4 7 lp /var/spool/lpd /sbin/nologin 5 0 sync /sbin /bin/sync 6 0 shutdown /sbin /sbin/shutdown 7 0 halt /sbin /sbin/halt 8 12 mail /var/spool/mail /sbin/nologin 11 0 operator /root /sbin/nologin 12 100 games /usr/games /sbin/nologin
Example 43: How to check the value of All /etc/ssh/sshd_config file parameters using cut command in Linux
If you want to check the value of all the set parameters of /etc/ssh/sshd_config
file then you need to cut out the 2nd field based of space
delimiter as shown below.
[root@localhost ~]# cat /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 | uniq 22 /etc/ssh/ssh_host_rsa_key /etc/ssh/ssh_host_ecdsa_key /etc/ssh/ssh_host_ed25519_key AUTHPRIV 2m yes AuthorizedKeysFile .ssh/authorized_keys yes no yes no
Example 44: How to check the value of GatewayPorts in /etc/ssh/sshd_config file using cut command
If you want to check the value of GatewayPorts
parameter from /etc/ssh/sshd_config
file then you need to use below cut command.
[root@localhost ~]# grep -i GatewayPorts /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 | uniq no
Example 45: How to check the value of AllowTcpForwarding in /etc/ssh/sshd_config file using cut command
If you want to check the value of AllowTcpForwarding
parameter from /etc/ssh/sshd_config
file then you need to use below cut command.
[root@localhost ~]# grep -i AllowTcpForwarding /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 | uniq yes
Example 46: How to check the value of UsePrivilegeSeparation in /etc/ssh/sshd_config file using cut command
If you want to check the value of UsePrivilegeSeparation
parameter from /etc/ssh/sshd_config
file then you need to use below cut command.
[root@localhost ~]# grep -i UsePrivilegeSeparation /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 | uniq sandbox
Example 47: How to check the value of UseDNS in /etc/ssh/sshd_config file using cut command
if you want to check the value of UseDNS
parameter from /etc/ssh/sshd_config
file then you need to use below cut command.
[root@localhost ~]# grep -i UseDNS /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 | uniq yes
Example 48: How to check the value of Compression in /etc/ssh/sshd_config file using cut command
If you want to check the value of Compression
parameter from /etc/ssh/sshd_config
file then you need to use below cut command.
[root@localhost ~]# grep -i Compression /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 | uniq delayed
Example 49: How to check the value of PrintLastLog in /etc/ssh/sshd_config file using cut command
If you want to check the value of PrintLastLog
parameter from /etc/ssh/sshd_config
file then you need to use below cut command.
[root@localhost ~]# grep -i PrintLastLog /etc/ssh/sshd_config | cut --complement -d "#" -f1- | cut -d " " -f2 | uniq yes
Example 50: How to check all the Other Options of cut command in Linux.
If you want to check all the other options available with cut command in Linux then you need to use cut --help
command as shown below.
[root@localhost ~]# cut --help Usage: cut OPTION... [FILE]... Print selected parts of lines from each FILE to standard output. Mandatory arguments to long options are mandatory for short options too. -b, --bytes=LIST select only these bytes -c, --characters=LIST select only these characters -d, --delimiter=DELIM use DELIM instead of TAB for field delimiter -f, --fields=LIST select only these fields; also print any line that contains no delimiter character, unless the -s option is specified -n with -b: don't split multibyte characters --complement complement the set of selected bytes, characters or fields -s, --only-delimited do not print lines not containing delimiters --output-delimiter=STRING use STRING as the output delimiter the default is to use the input delimiter --help display this help and exit --version output version information and exit
Example 51: How to check man page of cut command in Linux/Unix
If you want to check man page of cut command in Linux then you need to use man cut
command as shown below.
[root@localhost ~]# man cut CUT(1) User Commands CUT(1) NAME cut - remove sections from each line of files SYNOPSIS cut OPTION... [FILE]... DESCRIPTION Print selected parts of lines from each FILE to standard output. Mandatory arguments to long options are mandatory for short options too. -b, --bytes=LIST select only these bytes -c, --characters=LIST select only these characters -d, --delimiter=DELIM use DELIM instead of TAB for field delimiter
Example 52: How to Replace Colon(:) with Tab using sed command and cut the output upto 5th field
As shown in above examples, you can replace the Colon(:)
with Tab(\t)
using sed command and cut out the 5th field from the output using cut -f -5
command as shown below.
[root@localhost ~]# sed 's/:/\t/g' /etc/passwd | cut -f -5 root x 0 0 root bin x 1 1 bin daemon x 2 2 daemon adm x 3 4 adm lp x 4 7 lp sync x 5 0 sync shutdown x 6 0 shutdown halt x 7 0 halt mail x 8 12 mail operator x 11 0 operator
Popular Recommendations:-
How to Install PHP on Ubuntu 18.04
How to Install Ruby on Ubuntu 18.04 with Easy Steps
How to Install Ruby on CentOS/RedHat 7 in 5 Easy Steps
33 Practical Examples of ulimit command in Linux/Unix for Professionals
Install Node.js in 6 Easy Steps on Ubuntu 18.04
How to Install NVM for Node.js on Ubuntu 18.04
How to Limit CPU Limit of a Process Using CPULimit in Linux (RHEL/CentOS 7/8)
How to Install Rust Programming Language in Linux Using 6 Best Steps