Linux find command

Linux Find: How to Search Files with Example Commands

The Linux find is among the very powerful and flexible command-line utility from the daily toolbox – you should spend some time learning it.

Linux find command

A simple command of using the find is:

find /

This command will find and print each file on the system. And because everything is a file, you’ll find a great deal of output to sort through. So you will need to consider what you are trying to find.

The –name argument

Perhaps you wish to find all of the PNG files in your current directory. The -name argument lets you limit your results to documents that match the specified pattern. For example:

find ~ -name ‘*png’

The –iname argument

What if a number of them have an uppercase extension? The -iname is similar to -name , but it’s case-insensitive. An example of using the –iname:

find ~ -iname ‘*png’

How to only find files?

But what if you’ve got some directories that end in png or pdf etc. We can modify our control with the -type argument to search only for files.

Further Reading:  Why using zip in Linux?

For example:

find ~ \( -iname ‘*png’ -o -iname ‘*png’ \) -type f

Search with duration example

It turns out you have been taking plenty of pictures lately, so let’s narrow this down to documents which have changed in the past ten days.

find ~ \( -iname ‘*jpeg’ -o -iname ‘*jpg’ \) -type f -mtime -10

You can do time filters based on file status change time (ctime), adjustment period (mtime), or get time (atime). All these are in days, so in case you would like finer-grained control, you are able to express it in minutes rather (cmin, mmin, and amin, respectively). Unless you understand precisely the time you need, you will likely prefix the number with + (more than) or – (less than).

Searching on the basis of file size

You would like to find all of the gigantic (let us define that as”greater than one gigabyte”) files from the log directory:

find /var/log -size +1G

How to search newer files?

The’-newer’ parameter aids in searching the files that are newer than the mentioned file. Consider the below command:

find . -newer test.txt

The above command will display all of the files that are newer than ‘test.txt’ in the present working directory.

Further Reading:  The echo Command in Linux
Find and deleting a file example

The ‘-delete’ option is used to delete a particular file. We will need to be very careful when using this command because there’s no undo option if it’s once executed. Consider the below command:

find . -name test.txt -delete

This command should find and delete the test.txt file.

Permission based searching

You can also search for files based on permissions. Maybe you wish to find all of the world-readable files in your house directory to ensure that you’re not sharing “all” the files. For example:

find ~ -perm -o=r

How to find and replace files?

To find and replace files, we must combine find command with the sed command. To run on files, use the ‘-exec‘ option with the find command. Have a look at the command below:

find ./Newdirectory -type f -exec sed -i ‘s/find/replace/g’ {} \;