Simplify management of your file system
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 *.gif -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:
Search from the current directory on down, using the dot (.) just after find.
Locate all files that have a name ending in .gif (graphic files).
List all found files, using the ls command.
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:
$ ls ./gif_files/space/solar_system/saturn.gif
The rest of the article builds on this general principle: Thoughtful use of the find command can make the management of UNIX file systems a much easier task. For example, the find command can process commands based on the type of file system where the file is found, if you use the -fstype parameter. And it's often useful to have the find command prompt you before it executes commands on a found file; you can tell it to do so by using the -ok parameter, as you'll see next.
-------
Optional execution
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 $HOME/. -name *.txt -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. Listing 1 shows some sample results:
Listing 1. Sample results
< rm ... /home/bill/./.kde/share/apps/karm/karmdata.txt > ?
< rm ... /home/bill/./archives/LDDS.txt > ?
< rm ... /home/bill/./www/txt/textfile1.txt > ?
< rm ... /home/bill/./www/txt/faq.txt > ?
< rm ... /home/bill/./www/programs/MIKE.txt > ?
< rm ... /home/bill/./www/programs/EESTRING.txt > ?
.
.
.
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!
--------------
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.
No comments:
Post a Comment