Most of new users when dealing with Linux machine is how to find files they are looking for, files on linux can be found in many different ways. You can use ‘find’ or ‘locate’, I will cover the tutorial for locate later, because in this post we are focusing on the find command. find is one of the best tool to find files, find has a huge number of parameters that can be use to finds exactly those files that you were searching for.
find can search files by name, group, owner, type, permission, date, etc. find also can search on all sub directories too, that makes it recursive. The structure of the command is looks like this :
$ find where-to-find criteria what-to-do
So How to find file in linux with ‘find’ ? Just follow this tutorial.
Find Search by Name or Type
This is the most obvious and common way of searching a files, to find file by its name, type and execute this command :
$ find directory_location -name "file_name"
Using -name will be a case sensitive search, that means a search for “file” and “File” is different.
To find a file and ignore the case you need to change the parameter a bit like this :
$ find directory_location -iname "file_name"
Also if you want to invert the search you can add “-not” parameter like this :
$ find directory_location -not -name "file_name"
Find also has feature to find files by it file type, you can specify the type of files with the “-type“ parameter. It would be like this :
$ find -type file_type
Here is the most common descriptors that you can use to specify the file type :
- d : directory
- f : regular file
- I : sybolic link
- c : character devices
- b : block devices
For example, if we want to filter the find just for directory only, we can use this command :
$ find . -type d
Filter Result by Time and Size
Find gives you many ways to filter the result, one of them is by its time information (access times, change times, modification times). The parameter that use for this filter is :
- -atime for access time
- -ctime for change time
- -mtime for modification time
These parameter can use with plus and minus symbols to specify greater than or less than, but if you want to filter by exact time do not add the plus and minus symbol.
For example if we want to find a file that were accesses less than one day ago, we can type :
$ find directory_location -atime -1
We also can find a file by minutes instead of days, here is the example to find a file that accessed less than one minute ago :
$ find directory_location -atime -1
You can filter the result by the file size by using the “-size” parameter
We also can add a suffix on the end of the value that specifies the file size, these are the options :
- b: 512-byte blocks
- c: bytes
- k: kilobytes
- M: megabytes
- G: Gigabytes
Also like the time parameter we can use the plus and minus symbol to specify greater or less than.
For example if we want to find a file with size greater than 10 bytes, we can use this command :
$ find directory_location -size +10c
Finding by Permissions, Owner, and Group
find has a feature that let you to find files by its permission, to find by permission use the “-perm“ parameter. Also just like the other parameters, you can use plus and minus symbol too.
If you want to find an exact set of permissions, type :
$ find directory_location -perm 644
If you want to find anything with at least those permissions, type :
$ find directory_location -perm -644
You can also search for files by the file owner by using “-user” parameter
For example we want to find a file owned by user root :
$ find directory_location -user root
Beside search by the file owner, find can also search by the group owner.
For example we want to find a file that owned by the root group :
$ find directory_location -group root
Find is a powerful tools to find files on your system, find can be more useful by combining them with other utilities through the pipelines like wc,sort, and grep. So get yourself experiment with it !.
That is all for my tutorial How to find file in linux with ‘find’, hope this tutorial help you to find your file. If you have suggestion or you found a mistake on my tutorial, let me know it by leaving a comment below, thank you !