Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 2. Using Directories and Files > Finding forgotten files with find - Pg. 31

Using Directories and Files 31 Finding forgotten files with find Where, oh where, did that file go? Sometimes locating 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). Procedure 2-11. 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 file name (-name lostfile), and specify that the results be printed onscreen (- print) (Code Listing 2.11). Procedure 2-12. 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 di- rectory. 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 and Eric's home directories or any subdirectories under them (Code Listing 2.12). Procedure 2-13. 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 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. Tip 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. Tip 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. Of course, if you're logged in as root, this doesn't apply. Tip If you know only part of the file name, you can use quoted wildcards with find, as in find . -name 'info*' -print.