Advanced finds command usages

Playing with basics find command options

For basic Linux Find command usage please click here .

Example 31: Find all the files which are with more than size 100MB and less than 1GB and the owner of the file is xyz and the file name is Adda.txt in /red folder.

find /red -size +100M -size -1G -user xyz -iname adda.txt

Example 32: Find all the files with SGID for the group sales and with size exactly 100MB with file name as pass.txt under /opt.

find /opt -size 100M -group sales -perm g+s -name pass.txt

Linux find command AND Operator

By default find command will use AND option between two options. No need of mentioning any option. For example see below examples.

Example 33: Find all the files which are more than 100MB and less than 1GB in size.

find / -size +100M -size -1G


find / -size +100M -a -size -1G

Like above we can combine many options and your find command use AND operator by default no need of mentioning -a option.

Search for files and execute commands on Found files

Caution: Be careful when using -exec option which you are going to learn below. This is very dangerous option which can change/remove anything if don’t use wisely. There are some instances when you want to execute commands on the found files with find command. -exec is the option used in find command to execute shell commands directly on found files. Let’s discuss this with a basic example.

Example 34: find a file with passwd.txt in /var folder and long list this file for checking file properties.

find /var -iname passwd.txt -exec ls -l {} ;

Let me explain above command. Up to find /var -iname passwd.txt, this command you are aware. This command will search for passwd.txt file in /var folder and give the paths and file names where this file is located.

-exec: with this option we are saying to find command to execute a command(here it’s ls -l) followed by this option

{} -This is used as input to the command which we get as files/folders from find command output.

; –This indicates that find command is completed.

Actually ; is a command chaining capability and it’s a special character. In order to negate this special character we are using before;.

Example 35: Find all the files with name test.txt in /mnt and change the ownership of the files from Surendra to Narendra

find /mnt -user surendra -name test.txt -exec chown narendra: {} ;

-exec command {} ; –for executing a command on find files -inum -For finding a file with inode number

Example 36: Find all the files with name in /abc folder and then grep if for word is there in that file or not

find /abc -name -exec grep ‘for’ {} ;

chmod, grep, ls, rm, mv, cp,md5sum

Example 37: Find all the files with name xyz.txt owned by Surendra in /var/ftp/pub and change the permissions to 775 to them.

find /var/ftp -user surendra -name xyz.txt -exec chmod 775 {} ;

Example 38:  Find all the files with name temp.txt in /xyz folder and backup then compress them to send it for saving

Find /xyz -name xyz.txt -exec tar cvfz temp.tar.gz {} ;

Example 39: Find files with name abc.txt in /home directory and take backup of each file before modifying it.

find /home -name abc.txt -exec cp {} {}.bkf ;

This above command will create files with .bkf extension whenever it finds abc.txt file.

Example 40: Find files which are more than 1GB and not accessed for the past 6 months and delete them.

find / -size +1G -mtime +180 -exec rm -rf {} ;

Example 41: Find all the files with executable permissions and display their checksum value

find / -perm /a=x -exec md5sum {} ;

Example 42: Find all the files with name abc.txt and owner as surendra then move them to /opt folder

find / -user surendra -name abc.txt -exec mv {} /opt/ ;

There are many other commands you can try your own. Some other commands which can work with -exec option is mv, md5sum etc.

Find command with multiple -exec option

Find command is capable of using multiple times using the same option(We seen it for finding files which are more than 200M and less then 1GB). In our the previous examples we used -size to mention the size between two sizes. Similarly we can use -exec command multiple times.

Example43:Find files with abc.txt name in /opt directory change the owner permissions from Surendra to Narendra and change the permissions to 775

find /opt -user Surendra -name abc.txt -exec chown Narendra: {} ; -exec chmod 775 {} ;

Note: We can use this multiple -exec option more than two times.

Search for multiple files

Till this point if you observe we just search for single file. But sometimes there is a requirement to search for multiple files with single find command to save some valuable time. The following two examples we will see how to do that. Example44: Find all the commands which ends with .sh file extension in /opt folder

find /opt -name *.sh

Note: Sometimes the above command will not work properly because your shell will try to parse the * before find command is executed. So you have made * as not a special character or wild character. In order to understand more about this type of characters you have to learn RegExp. Please find our other posts on Basic RegExp and Advanced RegExp.


find /opt -name *.sh


find /opt -name “*.sh”

Note: These two will work, because you negated your shell parsing * wild character.

Example46:Search for all the files which start with abc and ends with different extension in /opt folder

find /opt -name abc.*

Example47:Search for files which start with red and ends with many names such as redhat, redtop, redsoap etc.

find / -name red*

Example 48:How about search for files which always end with dump.

find / -name *dump

Search for files in multiple locations

Search multiple locations using single find command We can accomplish searching multiple folders with single command without any options. Lets see below command.

Example 49: Find abc.txt file in /opt and /var folder at a time

find /opt /var -name abc.txt

The above command will search in only two locations i.e. in /opt and /var Search multiple locations but not in particular location. Example50:Search in entire system expect /proc folder

find / -path /proc -prune -name cpuinfo

Let me explain above command. The -path variable to define the path of a location. And -prune combined with -path will say not to descend in to the mention path /proc

Example51:Search for abc.txt in /opt and /var expect in /var/tmp folder

find /opt /var -path /var/tmp -prune -name abc.txt

Find command OR -o operator

Find have OR operator to do multiple file searches at a time.

Example 52: I want to search for abc.txt and hash.c file at a time. This can be achieved by using -o operator

find / -name abc.txt -o -name hash.c

Here when ever find command sees -o it just or the options on it’s left and right hand side.

Example 53: How about i want to find two directories say opt and var how can i find them?

find / -type d - name opt -o -type d -name var 

This above command may trow error. Try below syntax

find / -type d ( -name opt -o -name var )

let me explain above command. () are used to combine two or more options in to one. So ( -name opt -o -name var ) are combined in to single option and are treated as directories as we given -type is d. We will see more about this operator in our coming post on find command advanced usage.

Linux find command ! Negation operator

Example54: Negation operator is useful for negating a search team. for example we want to find all the files with name abc.txt which don’t have 755 permissions find . -type f ! -perm 755 -name abc.txtIn above command -perm 755 is negated so that all the files with name abc.txt is displayed expect file abc.txt with permissions 755.Stay tuned our next tutorial on find command to do following things.

1) Don’t  search in .cvs folder.
2) Search for files without owner.
3) Search for zero size files.
4) Search for files and don’t show me permission denied error.
5) Searching in hidden folders.
6) Creating alias for frequently used find commands.
7) Search for files with spaces.
8) Search for files and grep for multiple pattern in one find command.
9) Miscellaneous examples.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.