We earn commission when you buy through affiliate links.

This does not influence our reviews or recommendations.Learn more.

Besides locating files and directories, combining thefindcommand with others enables you to take action on the results.

In this article, we will start by explaining the basic Linux find commands with examples.

This will show youhow to find filesand directories.

We will also see how to perform actions on the files or directories that the find command locates.

The guide is based on Ubuntu but is applicable to most Linux distributions and versions.

The command contains the exact name for the file you are searching for.

Output

c’mon note that the results include the path.

In this case, you should probably provide the path for the directory where you want to search.

In our computer, we have theqatree.txtandqa.txtfiles as well as a directory by the nameqa.

To find files or directories only, you should probably specify this in the command.

Find files or directories only

For files only, use thetype fswitch.

Files only

Directories only

Add thetype doption to locate directories only.

To get all cases, use the-inameoption.

In our case, we will check in thetestandnumericdirectories.

This command allows you to find all the files containing your target text.

hyperconvergence, Hyperconvergence , etc.

Use the appropriate size format depending on the throw in of files or directories you are searching for.

This allows you to see files that have or havent been accessed within a specified period.

To see files that have not been accessed within the last 10 days in the home directory.

For example, all files modified between 6 and 15 days ago in the home directory.

it’s possible for you to specify the mode in the following three different ways.

To see files, where both have writable permissions, use the prefix.

Find files and change permissions

Find and change permissions of certain file types.

In our case, we will work with PHP files with different permissions as shown below.

Find and copy one file to many directories

Find and copy a single to multiple directories.

This will look for pdf files older than 20 days and move them to the backup1 directory.

Replace thetxtwith another extension such asbak,pdfor any other that you want to remove.

By default, the-rmwill not remove the directories and you better use theroption to ensures a recursive removal.

This ensures the deletion of empty directories and those containing files.

The-foption forces the removal and is used for both the files and directories.

This will delete all.bak filesolder than 20 days.

Removes bothDir22anddir22

To confirm before deletion, use the -i option.

Outputrm: remove directory ./Dir22?

nrm: remove directory ./dir22?

y

In our case, we typednfor directoryDir22which will not be deleted andyfor thedir22which will now be removed.

Another alternative is to use delete instead of remove.

To avoid accidental deletions, it is best practice to use a non-root user account.

Whats next?

Go ahead and try the above Linux find commands in your lab or NON PRODUCTION environment.

See if you could use some to automate the file system cleanup withCrontab.

And, to master Linux, check out thisonline course.