If you want to find a file that is 30 days old, use the -mtime option:
find . -mtime 30 -print
An alternate way is to specify a range of times:
find . -mtime +30 -mtime -40 -print
Finding Files of a Specific Type
You may want to perform a find operation to produce a list of files only and not include directories in the operation. The following find is similar to what we performed earlier, but this time it produces a list of files only. This is achieved by specifying that we are looking for type f for files:
# find /home -type f -print
You can search for a variety of different types such as f for a file as shown in the example, b for a block special file, l for a symbolic link, and so on.
Find Empty Files and Directories
A useful test when performing a find operation is to locate empty files. The following example searches for all empty files and directories on the system with the -empty operator that is available on some UNIX variants, including Linux. This is only a partial output because it’s so long:
# find / -empty -print
All the files and directories listed as a result of this find operation are empty. The long listing of /auto shown as part of the example, confirms this fact. Keep in mind that -empty is not available on all UNIX variants.
Finding Files By Name, Size, and Both Name and Size
Let’s perform a couple of independent finds and then combine the criteria of the finds. The finds in the following example are performed on a small, desktop system. This allows me to search the entire system for files meeting various criteria. You may be working on a much larger, more elaborate system, so use caution when searching the entire system for files. You may slow down other users for a long period of time, depending on the number of files on the system both locally and accessible over the network.
First, let’s find for all files on the system ending in.c with the command below:
# find / -name *.c -print
You can see from this find that there are many files on the system ending in.c. I included only the beginning and end of this search because the entire output would be too long.
I also ran this command and piped the output to wc, which showed 1737 files on the system ending in.c, as shown in the following example:
# find / -name *.c | wc
1737 1737 77044
#
Now we can search for all files on the system greater than 500,000 characters in size with the find command below:
# find / -size +500000c -print
I also ran this command and piped the output to wc, which showed 215 files on the system with a size greater than 500,000 characters, as shown in the following example:
# find / -size +500000c -print | wc
215 215 6281
#
Let’s now combine the two commands and see how many of the files on the system ending in.c are also greater than 500,000 characters in size:
# find / -name *.c -size +500000c -print
/usr/src/drivers/scsi/advansys.c
#
Of the 1737 files on the system ending in.c and the 215 files greater than 500,000 characters in size, only one file, advansys.c, meets both criteria. There is an implied and in the previous find command. We could have explicitly specified an and; however, it is implied in the find command. The find did indeed result in files that end in.c and are greater than 500,000 characters. The upcoming find uses the or operator, which is not implied.
What if we wanted to find both files ending in.c and.o that are greater than 500000 characters in size? We could use the -o operator which would “or” the files ending in.c and.o. The following example shows this find operation:
find / -size +500000c \( -name *.c -o -name *.a \) -print
find / -size +500000c \( -name *.c -o -name *.a \) -print | wc
# find / -size +500000c \( -name *.c -o -name *.a \) -print
The two file extensions for which we are searching are placed in parentheses. A backslash is required before the open and close parentheses because the parentheses have meaning to the shell and we want them to be used only to specify the precedence in our find command and not to be used by the shell. The result of this find shows that many files ending in.a and.c meet the criteria of greater than 500,000 characters.
Let’s now pipe the output of this commnd to wc to see precisely the number of files ending in either.c or.o and have a size greater than 500,000 characters:
# find / -size +500000c \( -name *.c -o -name *.a \) -print | wc
39 39 982
#
Of the 39 files that meet the criteria, we know that 38 ended in.a because our earlier example showed that only one file ending in.c met the criteria.
Finding Files By Owner, Type, and Permissions
You can find objects on the system owned by particular users and groups. To find all of the objects owned by user “news” on the system, we would use the following command:
# find / -user news -print
Using the -user operator we can specify either the name of the user, in this case “news”, or the user identification number. The following example shows performing the same find operation using the user identification number of “news,” in this case “9,” instead of the name “news”:
# find / -user 9 -print
This find operation produced exactly the same results using the name “news” and the user idenfication number “9.”
You can search for a variety of different types such as f for a file, as shown in the example, b for a block special file, l for a symbolic link, and so on. We could add type -d to find only directories belonging “news” as in the following command:
# find / -user news -type d -perm 775 -print
This is another example of using the implied and of find meaning that the find will print items that are both owned by “news” and are directories only.
Let’s now add a specific permission for the directories to our implied and. We’ll find only objects belonging to “news” that are directories with a permission of 775 in the following example:
# find / -user news -type d -perm 775 -print
We searched for directories belonging to “news” in which both the owner and those in the group have read-write-execute permission, and others have read-execute access. This is a common type of find operation for system administrators to perform – looking for files and directories belonging to a specific user and having specific permissions.