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: find all the files which are more than 100MB and less than 1GB in size.
find / -size +100M -size -1G
or
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
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 its 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;.
Example35: 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
Example36:Find all the files with name test.sh in /abc folder and then grep if for word is there in that file or not
find /abc -name test.sh -exec grep ‘for’ {} ;
chmod, grep, ls, rm, mv, cp,md5sum
Example37: 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 {} ;
Example39: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.
Example40: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 {} ;
Example41:Find all the files with executable permissions and display their checksum value
find / -perm /a=x -exec md5sum {} ;
Example42: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 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
No comments:
Post a Comment