Ever done some work on a LINUX system and cannot remember where it was saved? Enter the super-heros of LINUX - the 'find' and 'xargs' commands. Each is a powerful command in its own right, but when combined produce very useful output. FindThe Linux 'find' command will list files and directories that match the arguments to the command and pass specified tests. For example, to find all 'txt' files in the home directory, the following command can be used: find ~ -name *.txt The '~' means start at the user's home directory and the -name is a test which means list only files with that particular name. Find has many tests but some of the most useful for finding files that have been updated recently are the -mtime and -mmin tests.
Some examples will make this clearer. Find All Files Modified in the Last 90 Minutesfind ~ -mmin -90 Note that the number of minutes (90) is prefixed with a '-'. The reason for this will become apparent later. A sample output could be /home/linux /home/linux/dns/mysql-outfile /home/linux/dns/mysql-outfile/dnsx.txt /home/linux/dns/mysql-outfile/dnsx.doc It looks as if the 'find' command has listed files and directories. If we only want to see the files, add the -type test to the command to specify files only. find ~ -type f -mmin -90 The sample output would then become /home/linux/dns/mysql-outfile/dnsx.txt /home/linux/dns/mysql-outfile/dnsx.doc The information is now more concise, but which was the last file modified? You could issue a 'ls' command against each file in turn, but that could be very time consuming if there are more than a couple of files. What is needed is our other super-hero - xargs - with different powers. Adding Xargs to FindXargs' power lies in the fact that it can take the output of one command, in this case find, and use that output as arguments to another command. So, using the basic find command above, let us pass the output of find to xargs and get xargs to issue multiple 'ls -l' commands. find ~ -type f -mmin -90 | xargs ls -l The sample output would then become -rw-rw-r-- 1 linux lunux 10209032 Jun 30 13:28 /home/linux/dns/mysql-outfile/dnsx.doc -rw-rw-rw- 1 mysql mysql 10209032 Jun 30 12:53 /home/linux/dns/mysql-outfile/dnsx.txt Negative, Positive and Unsigned TimesIn the above examples the number of minutes was specified as a negative number. This tell the find command that you wish to list files modified LESS THAN the specified number of minutes ago. If you had used a positive value, for example +300 , find would have returned a list of files modified MORE THAN 300 minutes ago. If you had used an unsigned value, for example 10 , find would have returned a list of files modified EXACTLY 10 minutes ago. This unsigned operand is not very useful for the mmins test. Find All Files Modified More Than 5 Years AgoIn this example of the find and xargs command, we will use a positive time and the mtime test. The operand value to mtime is the number of 24 hour periods. It is not the number of days and the reason why will become apparent if you read 'man find'. So, to see files over 5 years old will will use +1825 as the value for mtime. We have ignored leap years in calculating 5 years as 5*365 days. The command issued is find ~ -type f -mtime +1825 |xargs ls -l A sample output could be-rw-r--r-- 1 linux linux 244 Jun 27 2000 /home/linux/marks/class4/ACCBLUE.TXT -rw-r--r-- 1 linux linux 12288 Jun 27 2000 /home/linux/marks/class4/ACCBLUE.XLS -rw-r--r-- 1 linux linux 266 Jun 27 2000 /home/linux/marks/class4/ACCCRABB.TXT -rw-r--r-- 1 linux linux 11776 Jun 27 2000 /home/linux/marks/class4/ACCCRABB.XLS -rw-r--r-- 1 linux linux 584 Jun 27 2000 /home/linux/marks/class4/Acclisa.txt -rw-r--r-- 1 linux linux 13824 Jun 27 2000 /home/linux/marks/class4/Acclisa.xls -rw-r--r-- 1 linux linux 264 Jun 27 2000 /home/linux/marks/class4/ACCPEAK.TXT -rw-r--r-- 1 linux linux 11776 Jun 27 2000 /home/linux/marks/class4/ACCPEAK.XLS -rw-r--r-- 1 linux linux 180 Jun 27 2000 /home/linux/marks/class4/ACCPHBH.TXT -rw-r--r-- 1 linux linux 12288 Jun 27 2000 /home/linux/marks/class4/ACCPHBH.XLS Tell xargs when to Quit
One problem with the above approach is that a complete listing of the current directory will occur
if the find command does not find any files!
Xargs sees a list of zero files, but still issues the 'ls -l' command.
To overcome this, simply use the xargs option '-r' which means
find ~ -type f -mtime +1825 |xargs -r ls -l Note that the '-r' is an xargs option and therefore comes after the xargs command name but before the command to be issued - in this case 'ls -l'. SummaryThe find command is a very useful command for finding files in a LINUX system. Combining find with xargs makes it a very powerful command. |