Linux Commands basic to advance Usage Part-2(Find, Join, Change the Case, Xargs, Sort, Uniq, Cut)
Find Command
Find is frequently used command to find files in the UNIX filesystem based on numerous conditions. Let us review some practice examples of find command.
Syntax: find [pathnames] [conditions]
How to find files containing a specific word in its name?
The following command looks for all the files under /etc directory with mail in the filename.
# find /etc -name "*mail*"
How to find all the files greater than certain size?
The following command will list all the files in the system greater than 100MB.
# find / -type f -size +100M
How to find files that are not modified in the last x number of days?
The following command will list all the files that were modified more than 60 days ago under the current directory.
# find . -mtime +60
How to find files that are modified in the last x number of days?
The following command will list all the files that were modified in the last two days under the current directory.
# find . –mtime -2
How to delete all the archive files with extension *.tar.gz and greater than 100MB?
Please be careful while executing the following command as you don’t want to delete the files by mistake. The best practice is to execute the same command with ls –l to make sure you know which files will get deleted when you execute the command with rm.
# find / -type f -name *.tar.gz -size +100M -exec ls -l {} \;
# find / -type f -name *.tar.gz -size +100M -exec rm -f {}
How to archive all the files that are not modified in the last x number of days?
The following command finds all the files not modified in the last 60 days under /home/jsmith directory and creates an archive files under /tmp in the format of ddmmyyyy_archive.tar.
# find /home/jsmith -type f -mtime +60 | xargs tar -cvf
/tmp/`date '+%d%m%Y'_archive.tar
Join Command
Join command combines lines from two files based on a common field.
In the example below, we have two files – employee.txt and salary.txt.
Both have employee-id as common field. So, we can use join command to combine the data from these two files using employee-id as shown below.
$ cat employee.txt
100 Jason Smith
200 John Doe
300 Sanjay Gupta
400 Ashok Sharma
$ cat bonus.txt
100 $5,000
200 $500
300 $3,000
400 $1,250
$ join employee.txt bonus.txt
100 Jason Smith $5,000
200 John Doe $500
300 Sanjay Gupta $3,000
400 Ashok Sharma $1,250
Change the Case
Convert a file to all upper-case
$ cat employee.txt
100 Jason Smith
200 John Doe
300 Sanjay Gupta
400 Ashok Sharma
$ tr a-z A-Z < employee.txt
100 JASON SMITH
200 JOHN DOE
300 SANJAY GUPTA
400 ASHOK SHARMA
Convert a file to all lower-case
$ cat department.txt
100 FINANCE
200 MARKETING
300 PRODUCT DEVELOPMENT
400 SALES
$ tr A-Z a-z < department.txt
100 finance
200 marketing
300 product development
400 sales
Xargs Command
xargs is a very powerful command that takes output of a command and pass it as argument of another command.
The following are some practical examples on how to use xargs effectively.
1. When you are trying to delete too many files using rm, you may get error message: /bin/rm Argument list too long – Linux. Use xargs to avoid this problem.
find ~ -name ‘*.log’ -print0 | xargs -0 rm -f
2. Get a list of all the *.conf file under /etc/. There are different ways to get the same result. Following example is only to demonstrate the use of xargs. The output of the find command in this example is passed to the ls –l one by one using xargs.
# find /etc -name "*.conf" | xargs ls –l
3. If you have a file with list of URLs that you would like to download, you can use xargs as shown below.
# cat url-list.txt | xargs wget –c
4. Find out all the jpg images and archive it.
# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
5. Copy all the images to an external hard-drive.
# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory
Sort Command
Sort command sorts the lines of a text file. Following are several practical examples on how to use the sort command based on the following sample text file that has employee information in the format: employee_name:employee_id:department_name.
$ cat names.txt
Emma Thomas:100:Marketing
Alex Jason:200:Sales
Madison Randy:300:Product Development
Sanjay Gupta:400:Support
Nisha Singh:500:Sales
Sort a text file in ascending order
$ sort names.txt
Alex Jason:200:Sales
Emma Thomas:100:Marketing
Madison Randy:300:Product Development
Nisha Singh:500:Sales
Sanjay Gupta:400:Support
Sort a text file in descending order
$ sort -r names.txt
Sanjay Gupta:400:Support
Nisha Singh:500:Sales
Madison Randy:300:Product Development
Emma Thomas:100:Marketing
Alex Jason:200:Sales
Uniq Command
Uniq command is mostly used in combination with sort command, as uniq removes duplicates only from a sorted file. i.e In order for uniq to work, all the duplicate entries should be in the adjacent lines. The following are some common examples.
1. When you have an employee file with duplicate entries, you can do the following to remove duplicates.
$ sort namesd.txt | uniq
$ sort –u namesd.txt
2. If you want to know how many lines are duplicates, do the following.
The first field in the following examples indicates how many duplicates where found for that particular line. So, in this example the lines beginning with Alex and Emma were found twice in the namesd.txt file.
$ sort namesd.txt | uniq –c
2 Alex Jason:200:Sales
2 Emma Thomas:100:Marketing
1 Madison Randy:300:Product Development
1 Nisha Singh:500:Sales
1 Sanjay Gupta:400:Support
3. The following displays only the entries that are duplicates.
$ sort namesd.txt | uniq –cd
2 Alex Jason:200:Sales
2 Emma Thomas:100:Marketing
Cut Command
Cut command can be used to display only specific columns from a text file or other command outputs.
The following are some of the examples.
Display the 1st field (employee name) from a colon delimited file
$ cut -d: -f 1 names.txt
Emma Thomas
Alex Jason
Madison Randy
Sanjay Gupta
Nisha Singh
Display 1st and 3rd field from a colon delimited file
$ cut -d: -f 1,3 names.txt
Emma Thomas:Marketing
Alex Jason:Sales
Madison Randy:Product Development
Sanjay Gupta:Support
Nisha Singh:Sales
Display only the first 8 characters of every line in a file
$ cut -c 1-8 names.txt
Emma Tho
Alex Jas
Madison
Sanjay G
Nisha Si
• cut -d: -f1 /etc/passwd
Displays the unix login names for all the users in the system.
• free | tr -s ' ' | sed '/^Mem/!d' | cut -d" " -f2
Displays the total memory available on the system.
That's a wrap 🌯.....