find command

When specifying time with find options such as -mmin (minutes) or -mtime (24 hour periods, starting from now), you can specify a number "n" to mean exactly n, "-n" to mean less than n, and "+n" to mean more than n.

Fractional 24-hour periods are truncated!  That means that "find -mtime +1" says to match files modified two or more days ago.

For example:

find . -mtime 0   # find files modified between now and 1 day ago  # (i.e., within the past 24 hours)
find . -mtime -1  # find files modified less than 1 day ago  (i.e., within the past 24 hours, as before)

find . -mtime 1   # find files modified between 24 and 48 hours ago
find . -mtime +1  # find files modified more than 48 hours ago

find . -mmin +5 -mmin -10 # find files modified between  # 6 and 9 minutes ago

 

If you are only interested in files of a certain type, use the -type argument, followed by one of the following characters:

+---------------------------------------------------+
|Character Meaning |
+---------------------------------------------------+
|b Block special file (see mknode(8)) |
|c Character special file (see mknode(8)) |
|d Directory |
|f Plain file |
|p Named Pipe File |
|l Symbolic link |
|s Socket |
+---------------------------------------------------+

Unless you are a system administrator, the important types are directories, plain files, or symbolic links (i.e. types d, f, or l).

Using the -type option, another way to recursively list files is:

find . -type f -print | xargs ls -l

It can be difficult to keep track of all of the symbolic links in a directory. The next command will find all of the symbolic links in your home directory, and print the files your symbolic links point to.

find . -type l -print | xargs ls -ld | awk '{print $10}'

Comments

find with exec and xargs

There is one more special argument that find treats differently: {}. These two characters are used as the variable whose name is the file find found. Don't bother re-reading that last line. An example will clarify the usage. The following is a trivial case, and uses the -exec option to mimic the "-print' option.

find . -exec echo {} ;

The C shell uses the characters { and }, but doesn't change {}, which is why it is not necessary to quote these characters. The semicolon must be quoted, however. Quotes can be used instead of a backslash:

find . -exec echo {} ';'

as both will pass the semicolon past the shell to the find command. As I said before, find can even call find. If you wanted to list every symbolic link in every directory owned by group "staff" you could execute:

find `pwd` -group staff -exec find {} -type l -print ;

To search for all files with group write permission, and remove the permission, you can use

find . -perm -20 -exec chmod g-w {} ;

or

find . -perm -20 -print | xargs chmod g-w

The difference between -exec and xargs are subtle. The first one will execute the program once per file, while xargs can handle several files with each process. However, xargs may have problems with files that contain embedded spaces.