The find command can be used to search the Unix file system. This command can be used in the following way:

find directory [directory …] criterion [criterion …] action [action …]

The first thing that needs to be specified is what directories (and their underlying subdirectories) must be searched. This can be one or more (absolute or relative) directory path names.

Next, you must specify the selection criteria for the files that must be searched for. Some examples of possible search criteria:

option description
-name look for files with a name matching a given pattern
-mtime look for files that have not been changed since a given timestamp
-type look for files of a given type (e.g. d for a directory, f for a regular file)
-inum look for files with a given inode number

The list of criteria that can be used with the find command seems almost endless. Check the man page of the find command if you look for a specific search criterion.

Finally, you must specify the actions that need to be performed on the matching files. Examples of such actions are:

option description
-print output the file name to stdout (default action)
-ls output additional details on stdout (analog output to ls -l)
-exec execute a command on each matched file
-ok execute a command on each matched file (after explicit user permission)

For instance, if you want to output a list of all files in the /dev directory and all its underlying subdirectories, this can be done using the following command:

$ find /dev -name '*' -print

Because -print is the default action, it could also have been left out from the above command. Also note that the shell gives special meaning to some characters, for example to apply filename expansion. However, for some arguments that are passed to the find command you want to avoid that the shell performs filename expansion (or any other expansion). This can be done by escaping those characters using a backslash or single/double quotes.

Assignment

Give Unix commands that perform each of the following tasks. Make sure that each command suppresses possible error messages (i.e. the error messages should not appear in the terminal or somewhere on the file system). In performing the tasks you may assume that the current directory contains a file participants.txt1 and a directory files that itself contains a subdirectory music.

  1. Write to stdout the list of all files below the current directory (i.e. including all its underlying subdirectories) that have not been changed more recently than the file participants.txt2.

  2. Remove all empty files (including empty directories) below the directory files. Make sure that no output is generated.

    Tip

    You can easily create empty files using the touch command. What would this command owe its name to? What else could it be used for, besides creating empty files?

  3. Use the gzip command to separately compress all regular files below the current directory (i.e. including all its underlying subdirectories) that are larger than 10 kilobytes.

  4. Write to stdout the list of all directories below the current directory, excluding the directories below the directory music. However, make sure the name of the directory music is itself written to stdout.

  5. Write to stdout the list of everything below the directory /etc that are executable for all other users and whose name starts or ends with a digit.