Comenzi Linux

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:

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:

Finding files by type

It is possible to find file with a type. We have following types for find command:

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.

Cele mai bune jocuri Oculus App Lab
Dacă sunteți proprietarul căștilor Oculus, atunci trebuie să fiți informați despre încărcarea laterală. Încărcarea laterală este procesul de instalare...
Top 10 jocuri de jucat pe Ubuntu
Platforma Windows a fost una dintre platformele dominante pentru jocuri din cauza procentului imens de jocuri care se dezvoltă astăzi pentru a sprijin...
Cele mai bune 5 jocuri arcade pentru Linux
În zilele noastre, computerele sunt mașini serioase folosite pentru jocuri. Dacă nu puteți obține noul scor mare, veți ști la ce mă refer. În această ...