| ||||||
|
|
Go to the previous, next chapter. Common TasksThe sections that follow contain some extended examples that both give a good idea of the power of these programs, and show you how to solve common real-world problems. Viewing And EditingTo view a list of files that meet certain criteria, simply run your file viewing program with the file names as arguments. Shells substitute a command enclosed in backquotes with its output, so the whole command looks like this: less `find /usr/include -name '*.h' | xargs grep -l mode_t` You can edit those files by giving an editor name instead of a file viewing program. ArchivingYou can pass a list of files produced by One common use of find . -depth -print0 | cpio --create --null --format=crc --file=/dev/nrst0 You could restore that archive using this command: cpio --extract --null --make-dir --unconditional \ --preserve --file=/dev/nrst0 Here are the commands to do the same things using find . -depth -print0 | tar --create --null --files-from=- --file=/dev/nrst0 tar --extract --null --preserve-perm --same-owner \ --file=/dev/nrst0 Here is an example of copying a directory from one machine to another: find . -depth -print0 | cpio -0o -Hnewc | rsh other-machine "cd `pwd` && cpio -i0dum" Cleaning UpThis section gives examples of removing unwanted files in various situations. Here is a command to remove the CVS backup files created when an update requires a merge: find . -name '.#*' -print0 | xargs -0r rm -f You can run this command to clean out your clutter in /tmp. You might place it in the file your shell runs when you log out (.bash_logout, .logout, or .zlogout, depending on which shell you use). find /tmp -user $LOGNAME -type f -print0 | xargs -0 -r rm -f To remove old Emacs backup and auto-save files, you can use a command like the following. It is especially important in this case to use null-terminated file names because Emacs packages like the VM mailer often create temporary file names with spaces in them, like #reply to David J. MacKenzie#. find ~ \( -name '*~' -o -name '#*#' \) -print0 | xargs --no-run-if-empty --null rm -vf Removing old files from /tmp is commonly done from find /tmp /var/tmp -not -type d -mtime +3 -print0 | xargs --null --no-run-if-empty rm -f find /tmp /var/tmp -depth -mindepth 1 -type d -empty -print0 | xargs --null --no-run-if-empty rmdir The second Strange File Names
rm -i some*pattern*that*matches*the*problem*file
find . -maxdepth 1 tests -ok rm '{}' \;
where tests uniquely identify the file. The -maxdepth 1 option
prevents ls -i Suppose you have a file whose name contains control characters, and you have found that its inode number is 12345. This command prompts you for whether to remove it:
find . -maxdepth 1 -inum 12345 -ok rm -f '{}' \;
If you don't want to be asked, perhaps because the file name may contain a strange character sequence that will mess up your screen when printed, then use -exec instead of -ok. If you want to rename the file instead, you can use
find . -maxdepth 1 -inum 12345 -ok mv '{}' new-file-name \;
Fixing PermissionsSuppose you want to make sure that everyone can write to the directories in a certain directory tree. Here is a way to find directories lacking either user or group write permission (or both), and fix their permissions: find . -type d -not -perm -ug=w | xargs chmod ug+w You could also reverse the operations, if you want to make sure that directories do not have world write permission. Classifying FilesIf you want to classify a set of files into several groups based on different criteria, you can use the comma operator to perform multiple independent tests on the files. Here is an example: find / -type d \( -perm -o=w -fprint allwrite , \ -perm -o=x -fprint allexec \) echo "Directories that can be written to by everyone:" cat allwrite echo "" echo "Directories with search permissions for everyone:" cat allexec
|
|
|
Email addresses listed on this site may NOT be used for unsolicited commercial email. Ready-to-Run Software, Inc Privacy Statement Portions (c)Copyright, 1996-2005 by
Ready-to-Run
Software, Inc |