Linux Find Command Tutorial

Linux Find Command Tutorial
In this quick tutorial, we will see how we can use find command in an Ubuntu machine to quickly find files based on various patterns, regular expressions and inside directories in a recursive manner. Let’s play through various conditions so that we can master basic find commands which can make our everyday life easier with Ubuntu.

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:

find Code -name ifelse4.sh

This command will find the file ifelse4.sh recursively inside the directory Code. Let’s see the output for this command:

Find file by name


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:

find Code -iname ifelse4.sh

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:

find Code -regex ".*.sh"

Here is what we get back with this command:

Finding file by regex


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:

find Code -regex ".*.sh|.txt"

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:

find Code -mmin -90

Let’s see the output for this command:

Finding file modified in last 90 minutes

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:

find Code -mtime 0

Here is what we get back with this command:

Find file by days

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:

find . -user shubham

Here is what we get back with this command:

Finding files belonging to a user


We can also apply the same logic to find files belonging to a user group:

find . -group root

Finding files by size

To find files which are bigger than a specified size can be found with the following command:

find Code -size +500

Here is what we get back with this command:

Find file by size


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:

find . -type f

Here is what we get back with this command:

Find files by type

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:

find . -size +1c -and -name "*.sh"

Here is what we get back with this command:

Find files with multiple conditions

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.

Related Posts
Leave a Reply

Your email address will not be published.Required fields are marked *