Finding Files and Directories in Linux System
Tags: linux, ubuntu, beginners, computerscience
Published: 2020-06-06 13:37:40

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](https://dev-to-uploads.s3.amazonaws.com/i/4345ek38dqf51s20lkcg.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/t8pggxabls1kjbbif6vg.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/z97mdqj6l0rqys7ymdun.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/17c74zftju1u8nw3o30n.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/h4zb2s1n5i6b6uep2fhb.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/zlb5qhd3uu73vchhbg8l.png) 

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](https://dev-to-uploads.s3.amazonaws.com/i/5bupgkxzipfotvyigpos.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/s1bmm71m1pgvtfmb9sac.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/j63m7n8c6ackanmeww9y.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/z1btdff7cbha8u6lr491.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/x6ifzph93cw6ywj7yi2s.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/bd67digb37tbncp844d0.png)

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.