Previous Section  < Day Day Up >  Next Section

find—finds files


find path–name–list expression


find recursively descends the directory hierarchy for each pathname in the pathname list (i.e., one or more pathnames) seeking files that match options. The first argument is the path where the search starts. The rest of the arguments specify some criteria by which to find the files, such as name, size, owner, permissions, and so on. Check the UNIX manual pages for different syntax.

Example A.27.

1   find . -name \*.c -print

2   find .. -type f -print

3   find . -type d -print

4   find / -size 0 - exec rm "{}" \;

5   find ~ -perm 644 -print

6   find . -type f -size +500c -atime +21 -ok rm -f "{}" \;

7   find . -name core -print 2> /dev/null   (Bourne and Korn Shells)

    ( find . -name core -print > /dev/tty ) >& /dev/null   (C shell)

8   find / -user ellie xdev -print

9   find ~ -atime +31 -exec mv {} /old/{} \; -print


EXPLANATION

  1. Starting at the present working directory (dot), finds all files ending in .c and prints the full pathname of the files.

  2. Starting at the parent directory (dot dot), finds all files of type file (i.e., files that are not directories).

  3. Starting at the present directory (dot), finds all directory files.

  4. Starting at the root directory, finds all files of size 0 and removes them. The {} are used as a placeholder for the name of each file as it is found.

  5. Starting at the user's home directory ~ (Korn and C shells), finds all files that have permissions 644 (read and write for the owner, and read permission for the group and others).

  6. Starting at the present working directory, finds files that are over 500 bytes and have not been accessed in the last 21 days and asks if it is okay to remove them.

  7. Starting at the present working directory, finds and displays all files named core and sends errors to /dev/null, the UNIX bit bucket.

  8. Prints all files on the root partition that belong to user ellie.

  9. Moves files that are older than 31 days into a directory, /old, and prints the files as it moves them.

    Previous Section  < Day Day Up >  Next Section