Cut Command examples in Linux.

What is “cut” command in Linux?

“Cut” Command is a utility for cutting sections from each line of the Files. It can be used to cut parts of a line by byte position, character and delimiter. It can also be used to cut data from file formats like CSV.

Examples of Cut Command.
You can specify byte position using the option -b, it will show only that letter, I am using the letter JustGeek here.
If you want to display only first letter i.e. J

[[email protected] ]# echo "JustGeek" | cut -b 1

If you want to display the letter J,S,G i.e. 1,3 and 5th Letter then you can specify using “,”

[[email protected] ]# echo "JustGeek" | cut -b 1,3,5
[[email protected] ]#

Cut based on a delimiter:-
To cut using a delimiter use the -d option. This is normally used in conjunction with the -f option to specify the field that should be cut.
Create a file called names.csv with content below


The delimiter can be set to a comma with -d ‘,’. cut can then pull out the fields you want with the -f flag. Example

[[email protected] ]# cat names.csv | cut -d ',' -f 1
[[email protected] ]#

You can comma “,” separate for multiple entries. Example.

[[email protected] ]# cat names.csv | cut -d ',' -f 1,2

If you want display age then you can just specify position with -f. Example

[[email protected] ]# cat names.csv | cut -d ',' -f 3

And if you want to Display City, then:

[[email protected] ]# cat names.csv | cut -d ',' -f 4

If you want to Change Delimeter in display then you can use the option –output-delimiter as shown in example below.

[[email protected] ]# cat names.csv | cut -d ',' -f 1,2 --output-delimiter='---'
[[email protected] ]#

How to Check “cut” command version

 [[email protected] ]# cut --version
 cut (GNU coreutils) 8.22
 Copyright (C) 2013 Free Software Foundation, Inc.
 License GPLv3+: GNU GPL version 3 or later
 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.

Create a large file Quickly on Linux servers.

How can I quickly create a large file on a Linux system?

Normally dd command creates a large file but it reads from /dev/zeor and then writes to drive which can take a longer time which you need a very large file.
If you don’t care what should be the contents and just want to add the file quickly you can use fallocate command to do this.

Below command will create 10GB of file in no time. You can modify it as per your needs

# fallocate -l 10GB /justgeek
[[email protected] /]# du -sh justgeek
9.4G    justgeek
[[email protected] /]#

There are other options you can use for fallocate, you can try help command to explore more.

[[email protected] /]# fallocate --help

 fallocate [options] <filename>

Preallocate space to, or deallocate space from a file.

 -c, --collapse-range remove a range from the file
 -d, --dig-holes      detect zeroes and replace with holes
 -i, --insert-range   insert a hole at range, shifting existing data
 -l, --length <num>   length for range operations, in bytes
 -n, --keep-size      maintain the apparent size of the file
 -o, --offset <num>   offset for range operations, in bytes
 -p, --punch-hole     replace a range with a hole (implies -n)
 -z, --zero-range     zero and ensure allocation of a range
 -x, --posix          use posix_fallocate(3) instead of fallocate(2)
 -v, --verbose        verbose mode

 -h, --help           display this help
 -V, --version        display version

For more details see fallocate(1).
[[email protected] /]#

Find Command examples

Find Command is one of the most important and frequently used command utility in Unix like operating systems. It is used to search and locate the list of files and directories based on conditions you specify for files that match the arguments.

Find all file whose name is justgeek.txt

# find -name justgeek.txt

Find all files whose name starts with justgeek

# find -name justgeek.*

Find all Directories whose name is justgeek

# find -type d -name justgeek

NOTE: you can use iname instead of name if you do want to match exact case.

Find all Directories whose name is justgeek in /home

# find /home -type d -name justgeek

Find all html file in /home folder

# find /home -type f -iname "*.html"

Find all files whose name ends with .tmp in /home folder and delete them.

# find /home -type f "*.tmp" -exec rm -fv {} \;

Find Empty files

# find /tmp -type f -empty

Find empty directories

# find /tmp -type d -empty

Find all files whose size is more than 500 MB

# find /home/justgeek -type f -size +500M

Find particular files types with size more than 100 MB and delete it.

# find / -type f -name message.logs -size +100M -exec rm -fv {} \;

Find files older than 24 hours and delete them

# find /tmp/logs -type f -mmin +1440 -exec rm -fv {} \;

Find all files older then 60 Mins and move them to a different folder

# find /tmp/logs*tar -mmin +60 -exec mv {} /home/backup \;