Next | Prev | Up | Top | Contents | Index

Finding and Manipulating Files Automatically

The IRIX system provides several tools for manipulating large numbers of files quickly. Some of the most common are described below:


Using find to Locate Files

The find(1) command is used to find files and possibly execute commands on the files found. It starts at a given directory and searches all directories below the starting directory for the specified files. A basic find command line looks like this:

find . -name <filename> -print

This command searches the current directory and all subdirectories downward from the current directory until it finds all matches to the <filename> and then displays their locations on your screen. You can also use regular expressions (see "Using Regular Expressions and Metacharacters") in your searches.

The following command line searches for files that have been changed after the time /tmp/file was last modified. If you use touch(1) to create /tmp/file with an old date, this command can help you find all files changed after that date.

find / -local -newer /tmp/file -print

You can use find to locate files and then to run another command on the found files. This example shows how to locate a file in a user's directory:

cd /usr/people/trixie

find . -name 'missingfile' -print

In this example, the period (.) indicates the current directory, the -name option indicates that the next argument in quotes is the name of the file you are looking for, and the -print option tells find to display the pathname of the file when the file is located.

The next example shows how to change the permissions on all the files in the current directory and in all subdirectories:

find . -name '*' -local -exec chmod 644 {} \;

The option immediately following the find command is a period (.). This indicates to find that the search is to begin in the current directory and include all directories below the current one. The next flag, -name, indicates the name of the files that are being found. In this case, all files in the directory structure are selected through the use of the asterisk metacharacter (*). See "Using Regular Expressions and Metacharacters" for more information on metacharacters and regular expressions.

The -local option indicates to find that the search is limited to files that physically reside in the directory structure. This eliminates files and directories that are mounted via the Network File System (NFS). The -exec option causes find to execute the next argument as a command, in this case chmod 644. The braces, { }, refer to the current file that find is examining.

The last two characters in the command line are part of the chmod command that will be executed (with the -exec option) on all files that match the search parameters. The backslash (\) is necessary to keep the shell from interpreting the semicolon (;). The semicolon must be passed along to the chmod process. The semicolon indicates a carriage return in the chmod command.

The find command has several other useful options:

-inum n

Locate files by their inode number (n) instead of their name.

-mtime n

Identify files that haven't been modified within a certain amount of time (n).

-perm [-]||onum


Identify files with permissions matching onum, an octal number that specifies file permissions. See the chmod(1) reference page. Without the minus sign (-), only file permissions that match exactly are identified.

If you place a minus sign in front of onum, only the bits that are actually set in onum are compared with the file permission flags.

-type x

Identifies files by type, where x specifies the type. Types can be b, c, d, l, p, f, or s for block special file, character special file, directory, symbolic link, FIFO (a named pipe), plain file, or socket respectively.

-links n

Matches files that have n number of links.

-user uname

Identifies files that belong to the user uname. If uname is a number and does not appear as a login name in the file /etc/passwd, it is interpreted as a user ID.

-group gname

Identifies files that belong to the group gname. If gname is a number and does not appear in the file /etc/group, it is interpreted as a group ID.

-size n [c]

Identifies files that are n blocks long (512 bytes per block). If you place a c after the n, the size is in characters.

-ok cmd

Works like -exec, except a question mark (?) prompts you to indicate whether you want the command (cmd) to operate on the file that is found. This is useful for such operations as selectively removing files.

Using find to Copy Directories or Directory Hierarchies

The find and cpio commands can be used to easily and safely copy a directory or a directory hierarchy as long as the user has permissions to access the directory. To copy a directory with all its files, or an entire hierarchy of directories and their files, use commands like the following:

mkdir new_directory_name

cd the_directory_you_want_to_copy

find . -print | cpio -pdlmv new_directory_name

This command sequence preserves the symbolic links in the new directory as well as transparently cross file system boundaries.


Next | Prev | Up | Top | Contents | Index