Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [154]

By Root 959 0


Next, we suppress directory descent:

$ find -prune

Find without looking inside this directory

.

$ find . -prune

Another way to do the same thing

.

$ find * -prune

Find files in this directory

one

sub

two

$ ls -d *

List files, but not directory contents

one sub two

Notice that a missing file or directory argument is equivalent to the current directory, so the first two simply report that directory. However, the asterisk matches every nonhidden file, so the third find works like ls -d, except that it shows one file per line.

Now it is time to try out some of the more powerful selection options in find's repertoire. Let's start with owner and group selection. The options -group and -user each require a following symbolic name or numeric identifier. Thus, find / -user root starts a long-running search for files in the entire tree that are owned by root. Unless this command is run by root, directory permissions will almost certainly hide major parts of the tree.

You probably expect that all of the files in your login directory tree are owned by you. To make sure, run the command find $HOME/. ! -user $USER. The exclamation argument means not, so in English, this command says: start at my home directory and list all files that do not belong to me. Both HOME and USER are standard shell variables customized to your login, so this command works for everyone. We used $HOME/. rather than just $HOME so that the command also works if $HOME is a symbolic link.

The -perm option requires a following permission mask as an octal string, optionally signed. When the mask is unsigned, an exact match on the permissions is required. If it is negative, then all of the bits set are required to match. If it has a plus sign, then at least one of the bits set must match. This is pretty complex, so we present some common idioms in Table 10-1.

Table 10-1. Common permission settings for find

Option

Meaning

-perm -002

Find files writable by other.

-perm -444

Find files readable by everyone.

! -perm -444

Find files not readable by everyone.

-perm 444

Find files with exact permissions r--r--r--.

-perm +007

Find files accessible by other.

! -perm +007

Find files not accessible by other.

The -size option requires a following numeric argument. By default, the size is in 512-byte blocks, although many find implementations permit the number to be suffixed by c for characters (bytes), or k for kilobytes. If the number is unsigned, then only files of exactly that size match. If it is negative, then only files smaller than that (absolute) size match. Otherwise, with a plus sign, only files bigger than that size match. Thus, find $HOME/. -size +1024k finds all files in your login tree that are bigger than 1MB, and find . -size 0 finds all files in the current directory tree that are empty.

The -type option requires a following single-letter argument to specify the file type. The important choices are d for directory, f for ordinary file, and l for symbolic link.

The -follow option asks find to follow symbolic links. You can use this to find broken links:

$ ls

Show that we have an empty directory

$ ln -s one two

Create a soft (symbolic) link to a nonexistent file

$ file two

Diagnose this file

two: broken symbolic link to one

$ find .

Find all files

.

./two

$ find . -type l

Find soft links only

./two

$ find . -type l -follow

Find soft links and try to follow them

find: cannot follow symbolic link ./two: No such file or directory

The -links option requires a following integer number. If it is unsigned, it selects only files having that many hard links. If it is negative, only files with fewer than that many (in absolute value) links are selected. If it has a plus sign, then only files with more than that many links are selected. Thus, the usual way to find files with hard links is find . -links +1.

The -atime (access time), -ctime (inode-change time), and -mtime (modification time) options require a following integer number, measured in days. If unsigned,

Return Main Page Previous Page Next Page

®Online Book Reader