Finding Forgotten Files with find

Where, oh where, did that file go? Sometimes finding a file requires more than cursing at your computer or listing directory contents with ls. Instead, you can use the find command, which lets you search in dozens of ways, including through the entire directory tree (Code Listing 2.11) or through directories you specify (Code Listing 2.12).

Code Listing 2.11. Use find to locate a missing file.
$ find . -name lostfile -print
./Projects/schedule/lostfile
$

Code Listing 2.12. By using wildcards and specifying multiple directories, you can make find yet more powerful.
$ find /home/deb -name  'pending*' -print
/home/deb/Projects/schedule/pending.tasks
$ find /home/deb /home/ejr -name'pending*' -print
/home/deb/Projects/schedule/pending.tasks
/home/ejr/pending.jobs.to.do.today.to.do
$

To Find a File:

  • find . -name lostfile -print
    

    Along with the find command, this specifies to start in the current directory with a dot (.), provide the filename (-name lostfile), and specify that the results be printed onscreen (-print). See Code Listing 2.11.

To Find Files Starting in a Specific Directory:

  • find /home/deb -name  'pending*'
    → -print
    

    This command finds all of the files with names starting with pending under Deb’s home directory. You must use single quotes if you include a wildcard to search for.

Or, you can find files under multiple directories at one time, like this:

  • find /home/deb /home/ejr -name
    → 'pending*'  -print
    

    This command finds files with names starting with pending in Deb’s and Eric’s home directories or any subdirectories under them (Code Listing 2.12).

To Find and Act on Files:

  • find ~/ -name '*.backup' -ok rm {} ;
    

    Type find with a wildcard expression, followed by -ok (to execute the following command, with confirmation), rm (the command to issue), and {} ; to fill in each file found as an argument (an additional piece of information) for the command. If you want to, say, compress matching files without confirmation, you might use find ~/ -name ‘*.backup’ -exec compress {} ; to do the work for you.

✓ Tips

  • On some Unix systems, you may not need the -print flag. Try entering find without the -print flag. If you see the results onscreen, then you don’t need to add the -print flag.

  • Avoid starting the find command with the root directory, as in find / -name the.missing.file -print. In starting with the root directory (indicated by the /), you’ll likely encounter a pesky error message for each directory you don’t have access to, and there will be a lot of those. Of course, if you’re logged in as root, this doesn’t apply.

  • If you know only part of the filename, you can use quoted wildcards with find, as in find . -name ‘info*’ -print.

  • find offers many chapters worth of options. If you’re looking for a specific file or files based on any characteristics, you can find them with find. For example, you can use find /home/shared -mtime -3 to find all files under the shared directory that were modified within the last three days. See Appendix C for a substantial (but not comprehensive) listing of options.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.185.123