The UNIX find command, like most UNIX commands, has an intimidating
array of options and switches that can discourage people from learning
its depth -- but true explorers aren't intimidated just because the
territory is vast. A good general principle goes a long way toward
simplifying a complex topic. Start up an xterm, and try the following
command:
$ find . -name *.bmp -exec ls {} \; The -exec parameter holds the real power. When a file is found that matches the search criteria, the -exec parameter defines what to do with the file. This example tells the computer to:
The -exec parameter requires further scrutiny. When a filename is found that matches the search criteria, the find command executes the ls {} string, substituting the filename and path for the {} text. If saturn.gif was found in the search, find would execute this command:
Important Note An important alternative to the -exec parameter is -ok; it behaves the same as -exec, but it prompts you to see if you want to run the command on that file. Suppose you want to remove most of the .txt files in your home directory, but you wish to do it on a file-by-file basis. Delete operations like the UNIX rm command are dangerous, because it's possible to inadvertently delete files that are important when they're found by an automated process like find; you might want to scrutinize all the files the system finds before removing them. The following command lists all the .txt files in your home directory. To delete the files, you must enter Y or y when the find command prompts you for action by listing the filename: $ find . -name "*.sql" -ok rm {} \; Each file found is listed, and the system pauses for you to enter Y or y. If you press the Enter key, the system won't delete the file. [oracle@usha scripts]$ find . -name "*.sql" ./a.sql ./test.sql [oracle@usha scripts]$ find . -name "*.sql" -ok rm {} \; < rm ... ./a.sql > ? < rm ... ./test.sql > ? [oracle@usha scripts]$ find . -name "*.sql" ./a.sql ./test.sql [oracle@usha scripts]$ After each question mark, the system paused; in this case, the Enter key was pressed to continue to the next file. (No files were removed.) The -ok parameter lets you control the automatic processing of each found file, adding a measure of safety to the danger of automatic file removal. If too many files are involved for you to spend time with the -ok parameter, a good rule of thumb is to run the find command with -exec to list the files that would be deleted; then, after examining the list to be sure no important files will be deleted, run the command again, replacing ls with rm. Both -exec and -ok are useful, and you must decide which works best for you in your current situation. Remember, safety first! Use find creatively You can perform myriad tasks with the find command. This section provides some examples of ways you can put find to work as you manage your file system. To keep things simple, these examples avoid -exec commands that involve the piping of output from one command to another. However, you're free to use commands like these in a find's -exec clause. Clean out temporary files You can use find to clean directories and subdirectories of the temporary files generated during normal use, thereby saving disk space. To do so, use the following command: $ find . \( -name a.out -o -name '*.o' -o -name 'core' \) -exec rm {} \; File masks identifying the file types to be removed are located between the parentheses; each file mask is preceded by -name. This list can be extended to include any temporary file types you can come up with that need to be cleaned off the system. In the course of compiling and linking code, programmers and their tools generate file types like those shown in the example: a.out, *.o, and core. Other users have similar commonly generated temporary files and can edit the command accordingly, using file masks like *.tmp, *.junk, and so on. You might also find it useful to put the command into a script called clean, which you can execute whenever you need to clean a directory. Copy a directory's contents The find command lets you copy the entire contents of a directory while preserving the permissions, times, and ownership of every file and subdirectory. To do so, combine find and the cpio command, like this: Listing 2. Combining the find and cpio command $ cd /path/to/source/dir $ find . | cpio -pdumv /path/to/destination/dir The cpio command is a copy command designed to copy files into and out of a cpio or tar archive, automatically preserving permissions, times, and ownership of files and subdirectories. List the first lines of text files Some people use the first line of every text file as a heading or description of the file's contents. A report that lists the filenames and first line of each text file can make sifting through several hundred text files a lot easier. The following command lists the first line in every text file in your home directory in a report, ready to be examined at your leisure with the less command: Listing 3. The less command $ find $HOME/. -name *.txt -exec head -n 1 -v {} \; > report.txt $ less < report.txt Maintain LOG and TMP file storage spaces To maintain LOG and TMP file storage space for applications that generate a lot of these files, you can put the following commands into a cron job that runs daily: Maintaining LOG and TMP file storage spaces $ find $LOGDIR -type d -mtime +0 -exec compress -r {} \; $ find $LOGDIR -type d -mtime +5 -exec rm -f {} \; The first command runs all the directories (-type d) found in the $LOGDIR directory wherein a file's data has been modified within the last 24 hours (-mtime +0) and compresses them (compress -r {}) to save disk space. The second command deletes them (rm -f {}) if they are more than a work-week old (-mtime +5), to increase the free space on the disk. In this way, the cron job automatically keeps the directories for a window of time that you specify. Copy complex directory trees If you want to copy complex directory trees from one machine to another while preserving copy permissions and the User ID and Group ID (UID and GID -- numbers used by the operating system to mark files for ownership purposes), and leaving user files alone, find and cpio once again come to the rescue: Listing 5. Maintaining LOG and TMP file storage spaces $ cd /source/directory $ find . -depth -print | cpio -o -O /target/directory Find links that point to nothing To find links that point to nothing, use the perl interpreter with find, like this: $ find / -type l -print | perl -nle '-e || print'; This command starts at the topmost directory (/) and lists all links (-type l -print) that the perl interpreter determines point to nothing (-nle '-e || print') -- see the Resources section for more information regarding this tip from the Unix Guru Universe site. You can further pipe the output through the rm -f {} functionality if you want to delete the files. Perl is, of course, one of the many powerful interpretive language tools also found in most UNIX toolkits. Locate and rename unprintable directories It's possible in UNIX for an errant or malicious program to create a directory with unprintable characters. Locating and renaming these directories makes it easier to examine and remove them. To do so, you first include the -i switch of ls to get the directory's inode number. Then, use find to turn the inode number into a filename that can be renamed with the mv command: Listing 6. Locating and renaming unprintable directories $ ls -ail $ find . -inum 211028 -exec mv {} newname.dir \; List zero-length files To list all zero-length files, use this command: $ find . -empty -exec ls {} \; After finding empty files, you might choose to delete them by replacing the ls command with the rm command. Clearly, your use of the UNIX find command is limited only by your knowledge and creativity. |