I recently wrote a letter to my congressperson. I don’t remember the name of the file, but I created it less than 7 days ago, it was smaller than 100k, and it contained the work ‘Nesto’. How would I use locate to find this file?
Using find:
find /home/rwcitek -type f -size -100k -atime -7 -print0 | xargs -0 grep 'Nesto' /dev/null
this returned:
/home/rwcitek/linux/topics/sssca/congress:To: Someone Nesto
Brief tutorial:
find decends through the directory heirarchy and applies the ‘logic string’ to each item in the folder. In the above example, find starts in my home folder (abreviated as ~). Then for each item in that folder, it applys the ‘logic string.’ In this case: -type f -size -100k -atime -7 -print0. Each truth test is evaluated, and if true, moves on to the next truth test. If the entry is a directory, find decends into it.
Break down of truth tests for the example:
- ‘-type f’ is the entry a file?
- ‘-size -100k’ is the entry less than 100k?
- ‘-atime -7’ is the access time less than 7 days ago?
- ‘-print0’ print the entry (this evaluates to true)
If any test fails, find stops processing the rest of the truth tests and moves on to the next entry. Using the example above, any file that is less than 100k fails the ‘-size -100k’ test, will not be tested for its access time, and will not be printed.
By default each truth test is a logical AND, i.e. the -a option is implied. However, you can also use OR (-o) and NOT (! or -not), and force precedence with parentheses.
Here’s another find to search for broken symlinks (Windows shortcuts):
find . -type l ! -exec test -e true ; -print
Out of curiosity, how would I do this in Windows (without using Cygwin or other Unix-like CLI)?
In contrast, locate is just a database of filenames on your system.
Written by Robert Citek for the St. Louis Unix Users Group DISCUSS listserv, 11 April 2002