Easily Locate Files With The FIND Command In Linux

Table of Contents

Every file wants to be found, but how do you “find” a file in Linux? You could use a file manager like Thunar, Dolphin, or Nautilus, but like anything else on Linux, the terminal version is better. That said, find is a new skill to add to the ol’ terminal toolkit. The find command in linux is a great utility for locating files and directories on your system. This tool supports searching by a variety of search criteria such as name, creation, modification date, permissions and etc. You can even execute commands on your search results using the -exec switch.

Very cool!

Find Command In Linux Usage

Let’s start with some basic syntax, to use the find command, you must first specify the starting directory for the search (if no path is given then the current directory is used by default), provide options (which control the behavior of find, of which there are only 5), then the search expression and file/directory.

				
					find [search_location] [-options] [expression]
				
			

It should be noted however, the options of the command can be omitted entirely because the available options you can pass to find restrict the search result or provide diagnostic info. For the sake of coverage, I’ll briefly outline their uses in the table below.

Option Action
-P
Don’t follow symbolic links, when on it ignores the properties of the actual file and presents the symbolic link properties. On by Default.
-L
Follow symbolic links, does the inverse of the above.
-H
Same as -P but behaves like -L when processing commands on the search result.
-D options
Useful when used in combination with other commands as it provides diagnostic information about search. (More detail is in the man pages)
-O(0-3)
Enables query optimization by filtering results based on search criteria. Think of this as a pseudo “speed” setting, useful on wide searches with unspecific search queries

Finding files…

By name

Let’s look at an actual example of the find command.

				
					find . -name .bashrc
				
			

In the above case we are looking for a file called .bashrc in our current directory (indicated by the dot). If we wanted to find every dot-file we have in the current directory we could use the following:

				
					find . -name "*.txt"
				
			

Here we are telling find to search for all dot files that match the search pattern of “*.txt” passed by -name in the current directory. The * sign here being a wildcard. Pretty simple, right? Let’s look at a few more ways to search for files.

By time

The -mtime option allows us to search for files last modified within a certain number of days, where N is the number of days.

				
					find -mtime -N
				
			

For example, say you wanted to search for files modified within the last week, your command would look like this.

				
					find -mtime -7
				
			

This tells the find command to search for files in the current directory that have been modified within the last 7 days.

By file size

You can also search based on file size, using the -size option, where N is the file size, byte, word, KB, MB, or GB.
				
					find . -size +100N
				
			

The -size option is used to specify the size of the file you are looking for. It can look for any file size, look to the table below for more detail.

Option Action
b
for 512 byte blocks (used by default)
c
bytes
w
two byte words
k
kibibytes (KiB, 1024 bytes)
M
mebibytes (MiB, 1024*1024)
G
gibibytes (GiB, 1024 * 1024 * 1024)

If we wanted to search for all files that are larger than 100MB we would use the following command.

				
					find . -size +100M
				
			

Here, we are telling find to search for files that are larger than 100MB, regardless of file name or location.

By permissions

To search by permissions, you can use the following:

				
					find . -perm 664
				
			

This will return all files that have read and write permission for owner and group but, other users can only read and not write.

Searching using multiple options

Multiple options can be chained together to narrow down search results. For example, using the previous commands we learned we can search for all .txt files that are larger than 100KB and were modified in the last 7 days:

				
					find . -name "*.txt" -size +100k -mtime -7
				
			

Now let’s look at using -exec to make our searches with find a bit more safe.

Using -exec on search result

Let’s say we are trying to perform a batch operation on multiple files. We would use the -exec option to perform other commands on the result. For example, you could perform a batch delete, move, or copy operation by using the appropriate UNIX commands. The following command will search for all text files in the current directory and prompts you to delete each one.

				
					find . -name "*.txt" -exec rm -i {} \;
				
			

It goes without saying, do not actually run this command or say "Yes" to this prompt as you will LOSE data if you do this.

This example is purely illustrative!

Searching content inside a file

Let’s try a safer use of -exec to look inside of files with the use of grep.
				
					find . -type f -name "*.txt" -exec grep 'Hello World'  {} \;
				
			

The above makes use of another option -type f, which tells find that it’s looking for a regular file and any .txt file that contains the phrase “Hello World”.

Conclusion

find has a ton of more advanced options which, if you are curious, are available under find’s man page to get more detail and use cases. You can also checkout find —help output for more exotic commands. find is a powerful utility and can be very useful when you need to locate specific files on your system. Below you’ll find a table of some beginner options that will be helpful getting started with find. Also, check out other useful and important commands everyone should know in Linux.

Option Action
-exec cmd
execute commands if the search criteria are met
-type ( d, f, l)
Useful to filter out results on a search. d is for directory, f is for regular file and l is for symbolic links. More options are listed under the man page
-name file
search for files, where “file’ is the search term
-newer file
Search for files that were modified after “file”.
-empty
will return empty files and directories
-user name
Search for files by username.

Meet the Author

Leave a Reply