DEV Community

Cover image for Finding Files and Directories in Linux System
yash sugandh
yash sugandh

Posted on

Finding Files and Directories in Linux System

In the last post we looked at the locate command, although blazing fast but has few drawbacks.

Today we are going to explore another command-line utility find for searching files and directories in Linux System.

find command

The find command searches through the file system based on simple pattern you specify and returns the result.

The syntax for find command

find-syntax

To find anything, we just use find command along with the simple_pattern we want to search

Let's take an example where we want to find the contents of Documents directory

find folder content

In the above example, we used the command find ~/Documents where

find represents the find command
~/Documents represents the simple pattern which specifies path till the Documents directory.

In the above example we get the files as well as the directories for the specified pattern, but what if we only wanted files?

find files only

In the above example we used the command find -type f
find represents the find command
-type represents the "type" option which specifies the type of content
f specifies the content type "file"

In the response, we got all the files present in the current working directory

Okay, what about only getting directories in response

find directories only

In the above example we used the command find -type d where
find represents the find command
-type represents the "type" option which specifies the type of content
d specifies the content type "directory"

In the response we only got the directories . i.e. current working directory and the logs direcory.

This is all great but what about finding a file using filename?

  • find a file log1.txt

find-by-name

In the above example, we use the command find -name log1.txt where
find represents the find command
-name represnts the option to find by name
log1.txt represents the name of the file to be found

Let's take one more step ahead and get the files ignoring case

  • find files by name and ignore case

find-by-name-ignore

In the above example, we used the command find -iname textFile1.txt where
find represents the find command
-iname represents the option to find by name and ignore case
textFile1.txt represents the name of the file to be found

These are all the basic scenarios covered with the find command.

Now let's look at some interesting real world scenarios

  • Find all the files that are greater than or equal to 5mb

find-greater-than-5mb

In the above example, we used the command find -type f -size +5M where
find represents the find command
-type f represents the type of content which is file(f) in our case
-size represents the option to find files of required size
+5M represents the files that are greater than or equal to 5mb

  • find all the files that are greater than 2mb but less than 10mb

find-less-than-2-greater-than-10

In the above example, we use the command find -type f -size +2M -size -10M where
find represents the find command
-type f represents the type of content which is file(f) in our case
-size represents the option to find files of required size
+2M represents the files that are greater than or equal to 2mb
-10M represents the files that are less than 10M

What if we only wanted the zip files

  • Find all the zip files

find-zip-files

In the above example, we used the command find -type f -name *.zip where
find represents the find command
-type f represents the type of content which is file(f) in our case
-name represents the option to find files with specific name
*.zip represents all the zip files

What if we wanted to find the files that were either greater than 2mb but less than 5M or files that were greater than or equal to 10mb

  • Find files with multiple conditions

find multiple conditions

In the above example, we used the command find -type f -size +2M -size -5M -o -size +10M where
find represents the find command
-type f represents the type of content which is file(f) in our case
-size represents the option to find files of required size
+2M represents files greater than or equal to 2mb
-5M represents files less than 5mb
-o represents logical OR
+10M represents files greater than or equal to 10mb

Simiarly we can use other Logical AND(-a), NOT(!) and so on

What if we want to perform some operations after we find the files

  • find all the files greater than 2mb and less than 5mb and copy them to directory "files_filtered"

find-filter-copy

In the above example, we used the command sudo find -type f -size +2M -size -5M -exec cp {} ~/files_filtered \; where
find represents the find command
-type f represents the type of content which is file(f) in our case
-size represents the option to find files of required size
+2M represents files greater than or equal to 2mb
-5M represents files less than 5mb
-exec means execute command
cp {} represents command to execute on the output i.e. copy in our case
~/files_filtered represents the path to output directory
\; represents that command has ended

But there is a problem with the above command, the exec does not ask us before perfoming the operation which will work in case of copy but in case of move or delete this can create a lot of problems

A safer alternative to -exec for move and delete operation is -ok

  • find all the files greater than 2mb and less than 5mb and move them to directory "files_filtered"

find-move-filtered

In the above example, we used the command sudo find -type f -size +2M -size -5M -ok mv {} ~/files_filtered \; where

find represents the find command
-type f represents the type of content which is file(f) in our case
-size represents the option to find files of required size
+2M represents files greater than or equal to 2mb
-5M represents files less than 5mb
-ok means execute command with check
mv {} represents command to execute on the output i.e. move in our case
~/files_filtered represents the path to output directory
\; represents that command has ended

Okay, so that’s all we need to know about find command.

I hope you understood find command-line utility in Linux. Please, let me know if there are any questions and suggestions on what you want us to explore next.

Top comments (3)

Collapse
 
msfjarvis profile image
Harsh Shandilya

This was awesome! I always found find (no pun intended :P) to be a very human unfriendly tool. There's just so many things that made less than zero sense and I eventually got way too tired of having to account myself for .git, files and folders from .gitignore and things like that so I switched to fd and never looked back. Certainly a worthwhile alternative to look at for people like me who can never remember find syntax :)

Collapse
 
yashsugandh profile image
yash sugandh • Edited

@HarshShandilya
Thanks for the comment. The purpose of this post was to enlighten with the basics before moving towards the easier and friendlier alternative. I am definitely going to post about fd so that everyone can make use of simple, fast, and user-friendly alternative to 'find'.

Collapse
 
msfjarvis profile image
Harsh Shandilya

Oh I definitely get the sentiment about the post, my mention of fd was more about how frustrating find can be to use with just the manpages. If I had this post 2 years ago I'd probably never have foundfd!