Find by name
The easiest command we can use for finding a file is by a specific name in a directory. The advantage with this command is that bash will find the file even if it present in one of the recursive directories inside the passed directory path. Let’s look at an example:
This command will find the file ifelse4.sh recursively inside the directory Code. Let’s see the output for this command:
The name option we mentioned above is case-sensitive. If you want to find the file irrespective of the case in the name, use the following command:
Finding a file by regular expression
We can use simple regular expressions to find a file in a directory which matches that regular expression. Let’s demonstrate this with a simple command to find all files with any name and with an extension of .txt:
Here is what we get back with this command:
Let us understand what this command means here:
- -regex: This just signifies that we are going to pass a regular expression next.
- In the regular expression, first period (.) signifies that any number of characters in file name shoule be a match.
- Next, with the *, we match any number of repetitions of any character (due to the period).
- Finally, we match all files with .sh extension.
The good thing about a regular expression can be that it can be made as flexible as you can define. Let’s modify above example to find files with .sh and .txt extension as well:
Finding files modified in last n minutes
Finding a file which was modified in last n minutes is easy as well. Let’s look at an example straight away:
Let’s see the output for this command:
Finding files modified in last n days
Finding a file which was modified in last n days is easy as well. Let’s look at an example straight away:
Here is what we get back with this command:
Finding files by permissions
Finding a file with specific permissions is possible as well. We can find files which are associated to a user or a user-group:
Here is what we get back with this command:
We can also apply the same logic to find files belonging to a user group:
Finding files by size
To find files which are bigger than a specified size can be found with the following command:
Here is what we get back with this command:
Apart from the bytes, size of a file can be mentioned as:
- b: 512-byte blocks: This is the default unit if none is specified
- c: bytes
- k: kilobytes
- M: megabytes
- G: gigabytes
Finding files by type
It is possible to find file with a type. We have following types for find command:
- d: directory
- f: regular file
- l: symbolic link
- b: buffered block
- c: unbuffered character
- p: named pipe
- s: socket
Let’s use a command to find a regular file:
Here is what we get back with this command:
Finding files with multiple conditions
As a last example, it is possible to find files by appending multiple conditions as we saw above. Let’s try multiple conditions in a single find command now:
Here is what we get back with this command:
Conclusion
In this lesson, we looked at how we can use the find command to find any files with some name or by permissions or by type. We can even append multiple conditions to find files which satisfy all the conditions. Play with the commands even more to fund the rela power.