How to Find files and Directories on Linux

Find command example in Linux

Find Command Example In Linux, is our most important post, in our section, in How to learn Linux, let’s see why.

On Linux, and like the Xargs command the Find command is one of the most useful and often used command utility in Unix-like operating systems. The Find command seeks and finds the list of files and directories based on the requirements you define for files that meet the arguments.

Find can be used in several forms. 

For example, you can find files by date, size, users, groups, permissions, and file type.

This article will explore our best Find command example in Linux, experience, and usage in examples.

We will show you the most used 40 Find Commands example in Linux. We have split the section into Five pieces from elementary to advance usage of the find command.

You can practice the find command also combined with additional tools such as grep or sed .

Also, besides finding files and directories, combining the find command with others (there are many possibilities) enables you to drive action on the results. Connecting the -exec argument allows us to run other commands utilities and execute operations like copying, moving, deleting, or modifying the file’s permissions, matching the defined rules such as size, name, and so forth.

First, let’s see some essential arguments, before we move-on to our goad, Find command example in Linux:

Find command example in Linux: Find Files by Type

The find command allows specifying which file type we are looking for. Also, if you set the type, the command runs fasters.

Sometimes you might require to seek particular file types such as standard files, directories, or symlinks. It’s important to remember that on Linux, everything is a file.

To find files based on their type, use the –type argument and one of the subsequent descriptors below to set the file type:

  • f: a regular file
  • d: directory
  • c: character devices
  • p: named pipe (FIFO)
  • l: symbolic link
  • s: socket
  • b: block devices

For example, to find all directories in your home directory, you would use:

$ find ~/ -type d

Find command example in Linux: How to find files and directories?

Scanning for particular files in a directory

This example will look for all those beginning with the letters “image” in the bitslovers directory.

find ./bitslovers -name image*

How to Find specific files by name or extension.

To scan for a particular file, run the subsequent command from the root (/). The command includes the proper name for the file you are searching for.

find / -name bitslovers-info.txt

How find files by extension

To find a file in Linux with a specific extension. For example, file all png files in the current folder.

find . -name *.png

How to Find files and directories by name

Use the command below to scan for files and directories, beginning with the letters bits. On our computer, we have the bitslovers.doc, also bitslovers-post-list.doc files, and a directory by the name bitslovers.

So, to find and list all files and folder that match with “bits*:

find ./ -name "bits*"

How to Find files or directories exclusive?

Sometimes, we would like to find a directory or file, especially if you have a huge folder containing several files and subdirectories. In other words, if we specify that we would like a file, the find will be much faster and will print less result, but more precisely.

So, for this goal, the argument type will help us to achieve this goal, and it’s pretty easy:

find ./ -type f -name "bits*"

You have noticed that the argument type requires an argument too. And the possibilities’ values are “f” for files or “d” for the directory.

So, because we used “f” the command above, we will find all files that begin with “bits”.

Otherwise, if you would like to find only directories that the name starts with “bits,” it will be like this:

find ./ -type d -name "bits*"

It’s pretty easy, right? Let’s jump to our following example.

How find files with case insensitive

For the Find command to find a file or directory by the name, the default behavior is not looking for upper or lower case.

In other words, if you are trying to find a file that starts with “Bits,” but we have specified for the command find “bits,” the find command will not find that file for you. How can we resolve that issue?

To get all cases, we need to use the argument -iname option.

find ./ -iname "bits*"

How to find various files with different extensions

You can use the find command to find various files with different extensions such as *.zip, *.tar *.pdf, etc. This can be performed individually, one extension at a time, or using one command covering all the desired extensions.

find . -type f ( -name "*.zip" -o -name "*.tar" -o -name "*.pdf" )

Note: The argument -o it means “OR.”

Find command example in Linux: How find files from multiple directories

To find the files in various directories, add their paths in the command. In our example, we will examine the directories Documents and Pictures directories.

find ./Documents ./Pictures -name bitslovers-logo.png -type f

How to Find files containing specific text?

Sometimes, you need to find a file containing a specific text inside it but cannot remember its file name or location. This command enables you to seek all the files carrying your target text.

To find all the files containing the word bitslovers, let’s see the example below:

find / -type f -exec grep -l -i "bitslovers" {} ;

In this scenario, we can also ignore upper and lower cases by using the argument -i.

find ./numeric -type f -exec grep -l -i "bitslovers" {} ;

Find command example in Linux: How to Find Files and Directories by Size

You can find files or directories that are smaller, equal, or greater than a specific size, within a particular range, or empty. Use the proper size format depending on the type of files or directories you are seeking for.

Size choices cover:

c – bytes

k – kilobytes

M – Megabytes

G – Gigabytes

How to Find files less than 25MB in the current directory

find . -type f -size -25M

How to Find files of a specific size – equal to 25MB?

To Search, find all 25MB files

find . -size 25M

How to Find files larger than a specified size?

find -size +2M

How to Find files with sizes between 50-100MB

When looking for files within a specific range, such as between 50 and 100 MB

find / -size +50M -size -100M

How to discover directories larger than 1G

find / -type d -size +1G

How to find empty files.

find ./ -type f -size 0

Find command example in Linux: How locate files By modification date

This will scan for files changed within the last 10 hours

find . -mtime -10 -type f

Find for directories changed within the last 2 days

find . -mtime -2 -type d

How to Find files by age or alteration time

Find files older than 10 days on Home Directory.

find ~/ -type f -name '*.zip' -mtime +10

How to find files based on access or modification date

Find files based on date or time accessed. This enables you to view files that have or haven’t been reached within a particular period.

To find files that have NOT been accessed within the last 30 days in the home directory.

find ~/ -atime +30

Or, find files accessed precisely 30 days ago

find ~/ -atime 30

Accessed within the last 30 days

find ~/ -atime -30

How to find files modified within a specific period.

Let’s try to find all files changed within 30 and 60 days ago.

find ~/ -type f -mtime +30 -mtime -60

How to find files accessed within the last 5 minutes

To find the files accessed within the last 5 minutes, use the -amin argument.

find . -amin -5 -type f

How to find Directories that were accessed within the last 5 minutes

find . -amin -5 -type d

How to find files changed within the last 5 days

You can also scan for files in the ~/ directory that was modified within the last 5 days.

find /home -mtime -5

How to find files that match with specific permissions

The Syntax it’s pretty simple:

$ find -perm mode

The mode is the permission which could be either numeric so as 644, 655, 700, 755, 777, or letters such as u=x, a=r+x, etc. Or any other combination.

Also, you can define the mode in three distinct forms:

  1. Without a prefix when you need to obtain files with the specific permissions defined.
  2. With “-” for files with at least the specific permission. This will find the files with the defined as well as higher permissions.
  3. Applying “/” needs defining the owner or Group with permission to the file.

So, let’s understand by practicing some examples.

How to find files with permission 777

find -perm 777
  • The command scans for any files in which the owner, Group and, others have read/write/execute permissions.

How to find files writable by the owner

For this proposal, we need to use the “/” to find files writable by either their owner, Group, or others.

find -perm /222

In this command above, find files that are writable by both their owner or Group.

Also, returns files that are writable by either but not indeed both. To scan files where both hold writable permissions, we can use the – prefix.

find -perm -220

How to find files owned by a specific user

The easy way to find files owned by a specific user just follows the example.

find /tmp -user mike

How to find specific files owned by a user

Find all png image files owned by Mike.

find /home -user mike -iname "*.png"

Find command example in Linux: How to Find and perform on the results.

This section will look at how you can act on the files that match the pattern specified in the find command.

Now, let’s see how we can execute some operations with the command Find returns for us. 

Please, pay attention to the examples below. Chose a folder or computer that not contains data that could be lost.

How can we find files and modify permission?

Find and modify permissions of specific file types. 

In our scenario, we have created several Shell Scripts, and we would like to change the permission from those files to execute them.

But, the easy way is to change all the files at once, to save our time.

Let’s apply permissions 755 for all Shell Scripts files:

find ver -name "*.sh" -type f -exec chmod 755 {} ;

How to find and list files, directories, and permissions

find -name "*.conf" | ls -l

How to find and change file and directory permissions

Scan files with 244 rights and modify them to have 755 permissions

find . -type f -perm 244 -exec chmod 755 {} ;

Find command example in Linux: How to find and copy directories or files

How to find and copy a particular file to a directory

Let’s copy the bitslovers.pdf file and copy it to the ~/Documents directory.

find -iname bitslovers.pdf -exec cp {} ~/Documents

How to find and copy one type of files to a directory

To find PDF files in the current directory and also copy them to another place:

find -iname '*.pdf' -exec cp {} ~/Documents;

Find command example in Linux: How to Find and Delete Files

To delete a collection of files, append the -delete argument to the end of the match expression.

Guarantee you are practicing this argument just when you are sure that the outcome meets the files you need to delete. It is always a great plan to check the matched files before using the -delete argument.

For our scenario, let’s delete all files ending with .bkp from the /tmp:

find /tmp -name `*.bkp` -delete

Use the -delete argument with absolute attention. The find command is executed as an expression and if you append the -delete argument first, the command will delete everything under the origin path you designated.

Aso, for directories, find can delete just empty directories, same behavior as rmdir.

Conclusion

In this post, Find command example in Linux, We have explained how to practice the find command with multiple arguments and examples.

This article should give you a robust knowledge of how to find files on your Linux systems. You may additionally hit the find man page and viewed regarding all different important find command arguments.

If you have some questions or comments, please give a comment below.

Leave a Comment

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

Free PDF with a useful Mind Map that illustrates everything you should know about AWS VPC in a single view.