Finding by Name:
To find a file by name, type:
To find a file by name, but ignore the case of the query, type:
Finding by Type:
You can specify the type of files you want to find with the "-type" parameter. It works like this:
Some of the most common descriptors that you can use to specify the type of file are here:
We can search for all files that end in ".conf" like this:
Filtering by Time and Size:
You can filter by size with the use of the "-size" parameter.
We add a suffix on the end of our value that specifies how we are counting. These are some popular options:
To find all files that are exactly 50 bytes, type:
To find all files less than 50 bytes, we can use this form instead:
To Find all files more than 700 Megabytes, we can use this command:
Time
Linux stores time data about access times, modification times, and change times.
To find files that have a modification time of a day ago, type:
If we want files that were accessed in less than a day ago, we can type:
Finding by Owner and Permissions
Similarly, we can specify files owned by the "shadow" group by typing:
We can also search for files with specific permissions.
If we want to match an exact set of permissions, we use this form:
Filtering by Depth:
You can specify the maximum depth of the search under the top-level search directory:
To find "file1" only in the "level1" directories and above, you can specify a max depth of 2 (1 for the top-level directory, and 1 for the level1 directories):
You can also specify a minimum directory if you know that all of the files exist past a certain point under the current directory:
We can use this to find only the files at the end of the directory branches:
You can combine the min and max depth parameters to focus in on a narrow range:
You can execute an arbitrary helper command on everything that find matches by using the "-exec" parameter. This is called like this:
For instance, we could find the files in the previous section that had "644" permissions and modify them to have "664" permissions:
To find a file by name, type:
Code:
#find -name "query"
Code:
#find -iname "query"
You can specify the type of files you want to find with the "-type" parameter. It works like this:
Code:
#find -type type_descriptor query
• f: regular file
• d: directory
• l: symbolic link
• c: character devices
• b: block devices
• d: directory
• l: symbolic link
• c: character devices
• b: block devices
Code:
#find / -type c
Code:
#find / -type f -name "*.conf"
You can filter by size with the use of the "-size" parameter.
We add a suffix on the end of our value that specifies how we are counting. These are some popular options:
• c: bytes
• k: Kilobytes
• M: Megabytes
• G: Gigabytes
• b: 512-byte blocks
• k: Kilobytes
• M: Megabytes
• G: Gigabytes
• b: 512-byte blocks
Code:
#find / -size 50c
Code:
#find / -size -50c
Code:
#find / -size +700M
Linux stores time data about access times, modification times, and change times.
• Access Time: Last time a file was read or written to.
• Modification Time: Last time the contents of the file were modified.
• Change Time: Last time the file's inode meta-data was changed.
• Modification Time: Last time the contents of the file were modified.
• Change Time: Last time the file's inode meta-data was changed.
Code:
#find / -mtime 1
Code:
#find / -atime -1
You can also search for files by the file owner or group owner.
You do this by using the "-user" and "-group" parameters respectively. Find a file that is owned by the "syslog" user by entering:
You do this by using the "-user" and "-group" parameters respectively. Find a file that is owned by the "syslog" user by entering:
Code:
# find / -user syslog
Code:
find / -group shadow
If we want to match an exact set of permissions, we use this form:
Code:
#find / -perm 644
You can specify the maximum depth of the search under the top-level search directory:
Code:
#find -maxdepth num -name query
Code:
#find -maxdepth 2 -name file1
Code:
#find -mindepth num -name query
Code:
#find -mindepth 4 -name file
Code:
#find -mindepth 2 -maxdepth 3 -name file
Code:
Executing and Combining Find Commands:
You can execute an arbitrary helper command on everything that find matches by using the "-exec" parameter. This is called like this:
Code:
#find find_parameters -exec command_and_params {} \;
The "{}" is used as a placeholder for the files that find matches. The "\;" is used so that find knows where the command ends.
Code:
#find . -type f -perm 644 -exec chmod 664 {} \;
Comment