In our earlier articles on the find command, we discussed how to find files modified before X hours, X mins or X seconds. find command options mtime, mmin, newer, etc. are used to get the desired list of files.
How to find the files modified before X mins or X seconds in a system which does not support the 'newer' option or the 'mmin' options of the find command?
The answer for this find2perl. This is a tool which comes in-built along with perl installation. Hence, any Unix flavor in which perl is present should have the find2perl utility present in it. Perl has some modules for finding files which is as good as the Unix find. This utility creates a perl script which uses the File::Find module to find files, and gives an output which is nothing but a perl script equivalent of the Unix find command. On running this script, the desired list of files could be retrieved.
The syntax of find2perl is exactly like the find command. Many of the options present in the find command is also present with the find2perl command. Let us see some examples on how to use find2perl:
1. find all files with the .txt extension:
$ find2perl . -name "*.txt" > f1f1 contains the perl script generated by the find2perl utility. On executing the file f1, the files will be retrieved and displayed.
$ chmod 755 f1 $ ./f12. find the files whose inode number is 9962606
$ find2perl . -inum 9962606 > f1 $ ./f1As shown, the options provided to the find2perl are exactly like the find command.
3. To find files modified in the last 1 day:
$ find2perl . -mtime -1 > f1 $ ./f11 indicates 1 day. -1 indicates files modified in the last one day. +1 indicates files modified before 1 day.
Unlike the Unix find command, find2perl does not support the mtime, atime, ctime options. Still, using find2perl, we can find files modified in the last x minutes using the mtime switch itself, however, we need to edit the intermediate file generated by the find2perl. Its a kind of hacking the intermediate file and customizing it for our purpose. This will be a 3 step process:4. To find files modified in the last 1 hour:
- Run the find2perl command and re-direct the output to a file.
- Edit the file and remove the word "int" and save the file.
- Run the file to execute the script.
$ find2perl . -mtime -1/24 > f1Open the generaeted file f1. In the line containing the statement: (int(-M _) < 1/24) delete the word "int". Save the file f1. And execute the file now.
$ ./f11 in the mtime option indicates 1 day, and hence 1/24 indicates 1 hour.
5. To find files modified before 1 hour:
$ find2perl . -mtime +1/24 > f1Remove the word "int" as in the example 4 in the file f1 and save it.Run the file f1.
$ ./f1The only change here is in place of -, we have +.
6. To find files modified in the last 5 hours:
$ find2perl . -mtime -5/24 > f1Remove the word "int" as in the example 4 in the file f1 and save it. Run the file f1.
$ ./f1The change here is: For 5 hours, simply multiply the duration for one hour by 5.
7. To find all files modified in the last 40 mins:
$ find2perl . -mtime "-40/(24*60)" > f1Remove the word "int" as in the example 4 in the file f1 and save it.Run the file f1.
As said earlier, the number in mtime indicates number of days. Hence, to get to the hour level, say for 1 hour: 1/24. Similarly, to get to the minute level, say 1 minute: 1/(24*60). Now, for 40 minutes, multiply it by 40, and hence we get: 40/(24*60).
8. Similarly, to find files modified exactly 40 minutes back:
$ find2perl . -mtime "40/(24*60)" > f1Remove the word "int" as in the example 4 in the file f1 and save it.Run the file f1.
$ ./f1All we did is removing the negative sign in the mtime option.
9. To find all files modified in the last 2000 seconds :
$ touch -d "2000 seconds ago" tmpFile $ find2perl . -newer tmpFile > f1 $ ./f1This is the same way as we did in an example of find files modified in X hours and X mins and X seconds. A temporary file is created whose time stamp is 2000 seconds before the current time. find2perl tries to find all files which are newer than the tmpFile, in other words, all files modified after the file tmpFile is created.
10. To find files modified in the last 2000 seconds without using the newer option:
In case, your Unix system does not contain the "-d" option in the touch command, you will not be able to use the above approach. However, you can still use the -mtime instead of -newer for this requirement:
$ find2perl . -mtime "-2000/(24*60*60)" > f1As above, remove the word "int" as in the example 4 in the file f1 and save it. Run the file f1.
$ ./f1Note: Though we discussed the find2perl to use in case of not having the mtime option, the same holds good for all the 3 time stamps: mtime, ctime and atime.