Handout #3


Finding Files which match a particular name pattern or a set of attributes "find":


At the Unix prompt, enter:


find . -name filename -print


This section lists the options for the "find" command, followed by a list of useful examples.


The general form of the command is:


find (starting directory) (matching criteria and actions)


The find command will begin looking in the starting directory you specify and proceed to search through all accessible subdirectories. More than one starting directory may be specified for searching.


There are several options for for matching criteria:


-atime n File was accessed "n" days ago

-mtime n File was modified "n" days ago

-size n File is "n" 512-byte blocks big

-type c Specifies file type: f=plain text, d=directory

-name nam The filename is "nam"

-user usr The file's owner is "usr"

-group grp The file's group owner is "grp"

-perm p The file's access mode is "p" (integer)


You can use plus + and minus - modifiers with the "atime", "mtime", and "size" criteria to increase their usefulness. Some examples:


-mtime +7 Matches files modified more than 7 days ago

-atime -2 Matches files accessed less than 2 days ago

-size +100 Matches files larger than 100 blocks (50K)


Multiple options are joined by AND by default. OR may be specified with the -o flag and the use of grouped parentheses. To match all files modified more than 7 days ago and accessed more than 30 days ago, use:


\( -mtime +7 -o -atime +30 \)


NOT may be specified with an exclamation point. To match all files ending in .txt except the file "notme.txt", use:


\! -name notme.txt -name \*.txt


You can specify the following actions for the list of files that the "find" command locates:


-print Display pathnames of matching files

-exec cmd Execute command "cmd" on a file

-ok cmd Prompt before executing command "cmd" on a file


Executed commands must end with \; and may use {} as a placeholder for each file found by "find". For example, for a long listing of each file found, use:


-exec ls -l {} \;


Matching criteria and actions may be placed in any order, and are evaluated from left to right.


Full examples:


To find and report all C language source code files starting at the current directory, enter:


find . -name \*.c -print


To report all files starting in the directories "/mydir1" and "/mydir2" larger than 2000 blocks (about 1000K) AND that have not been accessed in over 30 days, enter:


find /mydir1 /mydir2 -size +2000 -atime +30 -print


To remove (with prompting) all files starting in the "/mydir" directory that have not been accessed in over 100 days, enter:


find /mydir -atime +100 -ok rm {} \;


To show a long listing starting in "/mydir" of files not modified in over 20 days OR not accessed in over 40 days, enter:


find /mydir \(-mtime +20 -o -atime +40\) -exec ls -l {} \;


To list and remove all regular files named "core" starting in the directory "/prog" that are larger than 500K, enter:


find /prog -type f -size +1000 -print -name core -exec rm {} \;

Accessing the contents of a file by different names "ln":


By default, ln makes a direct (hard) link between the contents of the file you give it as its first

argument and the filename you give it as the second argument. The old and new filenames point to the same piece of information on disk.(that is, the two directory entries point to the same actual file on disk). A hard link (the default) is a standard directory entry just like the one made when the file was created. Hard links can only be made to existing files. To remove a file, all hard links to it must be removed, including the name by which it was first created; removing the last hard link releases the inode associated with the file.



Symbolic or soft links are created by using ln with the -s option. A symbolic link file points to a file in a more indirect way. You can think of this as if the symbolic link is pointing to the filename dynamically rather to the actual information the file contains




For creating a soft link ,


The command is ln, with -s argument.




ln -s source_file newfilename




In the example, source_file is an existing file (which can be any existing file or directory across the file systems), and newfilename is a symbolic link to it, created by the ln command. After the symbolic link is made, you can do an operation on or execute newfilename, just as the source_file. You can use normal file management commands (cp, rm, etc) on the symbolic link.


Note: once the source_file is deleted or moved to a different location, your symbolic file still exists, and the only thing you can do is either delete or move it. If you try to use it for other purposes (edit, or execute, for example), the system will send a "file nonexistent" message.







Sample Exam Questions (Do the following questions in your account , you are also required to turnin a handout ):




  1. In the directory homework/hw4 create a file which has the following line in it . "This file will be used for testing the use of links on files ." Name this file "sourcefile". Create a hard link to this file , name it "hardlink". Create a soft link to this file name it "softlink". Now rename the file "sourcefile" as "newfile".
    1. Now can you access the contents of "newfile" using "softlink" ?
    2. Can you access the contents of "newfile" using "hardlink" ?
    3. Can you explain this difference ?
    4. Do the above 3 questions if the file "sourcefile" was deleted rather than moved .




  1. Use the find command to find all files that end with the letters "ile" in your account , use the –exec option to display the information about these files in the long form . Redirect the output to a file called "ilelist". What was the command that you used for doing this ?