Overview
In this tutorial, you will learn how to use the find
command to find files and directories on your Linux filesystems. You will also learn how to execute commands against files returned by the find command.
Basic Find
The find
command can used to find files, directories or both. By default it will match a string against both files and directories.
The basic syntax of the find command looks like the following
find <path> -name=<string|regex>
For example, to search for a file or directory named “tutorials” from your home directory, you would execute the following command.
find ~/ -name "tutorials"
Finding a File
To narrow your search down to files only you use the -type
flag with the find command. Using the example above, in order to find just files with the name “tutorials” you would execute the following command.
find ~/my/files -name "tutorials" -type f
The -type f
instructs the find command to only match the -name
string against files.
Finding Files of a Specific Size
To narrow your results, the find command can search for files matching a specific size. The size can be represented in kilobytes, megabytes, gigabytes, terabytes, or even petabytes.
The following example searches for files that are 2 gigabytes in size, rounded up by blocks of 512 bytes.
find /my/media -type f -size 2G
Finding Files Owned by a User
Finding files based on ownsership is also possible. We have the option of searching by username or even uid (user id).
To find files owned by a user named jsmith, you use the -flag
in your comand with the name of the user.
find /opt/service -user jsmith
Alternatively, to search by user ID instead you the -uid
flag.
find /opt/service -uid 1024
Finding Files based on Age
Have you ever needed to find a file that was created within the last few minutes or day? Thankfully, the find command also supports finding files and directories based on age.
To find a file a file created within the last 10 minutes you use the -newerct
command.
find /var/log -newerct `10 minutes ago` -print
Finding a Directory
Alternative, to narrow your search down to directories only, you can use use the -type
flag with the value d
. Files will be ignored and they will not be included in your search results.
find ~/ -name "tutorials" -type d
Executing Commands Against Found Files
There are many scenarios where you find yourself needing to execute a command against the returned file list. This may be to set permissions on directories only of a given path, or to copy matching files to a specific location.
To execute commands against discovered files and directories you use the exec
flag.
find <path> -name <string|regex> -exec <command> {} \;
The two brackets ‘{}
‘ near the end of the command are placeholders. The placeholders will be replaced by the files and/or directories that are returned by the find
command. The ‘\;
‘ at the end terminates the find command.
To copy files from your home directory that contain the word “tutorial” in their filename to /temp
, you would execute the following find command.
find ~/ -name="*tutorials*" -type f -exec cp {} /temp \;
Whenever find
matches a file found in your home directory or any of its child directories, it will execute the cp
command to copy it to the /temp
directory.
Conclusion
The find command in Linux is a very powerful tool. You can find virtually anything on your filesystems, whether its a file or directory. You can also execute commands against the files or directories that you find.