Wednesday, May 2, 2012

Learn Linux/Unix Find command with 60+ Practical examples

http://www.linuxnix.com/2012/04/learn-linuxunix-find-command-50-practical-examples-part-i.html


find command is very much powerful command which can do good work when its needed to find files with conditions. Find command  is useful when finding files with complex requirement such as size, permissions etc. Suppose we want to find a file which is a regular file and its size is more than 1GB and its accessed more than 90 days back and its owner is none and then delete it. This entire requirement is done with single command without even writing a shell script. Let’s see how we can use find command from basics to advanced in this post.
find command can find files according to
1) File names

2) File types

3) Permissions

4) Owners

5) Modified date and time

6) Size 
Advanced finds command usages
1) Mix of all the above things
2) AND operator
3) Search for files and execute commands on them
           a) chown, chmod, grep, ls, rm, mv, cp,md5sum

4) Multiple execute commands

5) Search for multiple files
            a) With different files with same extension
            b) Same file With different extensions
6) Search in multiple locations
            a) Exclude one location
            b) Search in multiple locations
         
7) OR( –o ) operator
 8) ! Negation operator
9) Linux find command with Regular Expressions
10) Linux find commnd practical examples

Basics of file/folders search using find command

 find files with name

Syntax:
find path options filename
Example1: find all the files in /home with name test.txt. Here –name is used to specify the filename.
find /home –name test.txt
Example2: find the files whose name is test.txt and in present working directory
find . –name test.txt
Or
find –name test.txt
Example3: find all the files whose name contains both capital letters and small letters in it.
find /home –iname test.txt
-iname option is used to mention ignore the case sensitivity of a file.
Search for files depending on their File types:
Example4: Search for only directories whose name is var in / directory
find / -type d –name var
Example5: Search for an mp3 files whose name is temp.mp3
find / -type f –name temp.mp3
Below are the file types supported by find command, to know more about file types in Linux/Unix please have a look at our other post on File types.
Sl.no Symbol Type
1 f Regular file
2 d Directory file
3 b Block file
4 c Character file
5 p Pipe file
6 l Symbolic link file
7 s Socket file.

Search for files depending on their Permissions

Example6:Search for a file name test.txt and its permissions are 775 in a given box
find / -perm 775 –name test.txt
Example7: How about searcing files with SUID bit set and file permissions are 755?
find / -perm 4755
Example8:How can i find SGID bit set files with 644 permissions?
find / -perm 2644
Example9: How can i find Sticky bit set files in my system with permissions 551?
find / -perm 1551
Example10:Search for all the files whose SUID bit is set
find / -perm /u=s
Example11: Search for all the files whose SGID bit is set
find / -perm /g+s
Note: We can use = or + interchangeably to check if a permissions is set or not as shown in above two examples.
Example12: Search for all the files  whose StickyBit is set
find / -perm /o=t
Example13: Search for all the files whose owener permissions is read only.
find / -perm /u=r
Example14:Search for all the files which have user, group and others with executable permissions
find / -perm /a=x
To know about more on the permissions you have look at our other posts on chmod command.

Search according to Owners and group owners.

Example15: Search for all the files with name test.txt and the owner of this file is Surendra
find / -user Surendra –name test.txt
Example16: find all the files whos name is test.txt and owned by a group called redcluster
find / -group redcluster –name test.txt
to know more about owners and groups you have to look at our previous post on chown command.

Search according to Modified date and time.

Below is the matrix which give you brief idea on how to search according to modified date, accessed date etc.
Sl. No -ctime -mtime -atime
+90 File status changed more then 90 days back Modified more than 90 days back Accessed more than 90 days back
90 File status changed exactly 90 days back Modified exactly 90 days back Accessed exactly 90 days back
-90 File status changed less than 90 days Modified less than 90 days Accessed less than 90 days back
Example17: Search for a file: test.txt whose file status is changed more than 90 days back
find / -ctime +90 –name test.txt
Example18: Search for all the files which are modified exactly 90 days back
find / -mtime 90
Example19: Search for all the files with name test.txt which is accessed less than 90 days
find / -atime -90
Example20: find all the files which are modified more than 90 days back and less than 180 days
find / -mtime +90 –mtime -180
Below is the matrix which gives you brief idea on how to search according to modified time, accessed time in minutes etc.
Sl. No -cmin -mmin -amin
+30 File status changed more then 30 mins back Modified more than 30 mins back Accessed more than 30 mins back
30 File status changed exactly 30 mins back Modified exactly 30 days back Accessed exactly 30 mins back
-30 File status changed less than 30 mins Modified less than 30 mins Accessed less than 30 mins

Example21: find all the files changed less than 30mins
find / -cmin -30
Example22: find all the files modified exactly 30 mins back
find / -mmin 30
Example23: find all the files accessed more than 30 mins back
find / -amin +30
Example24: find all the files which are modified more than 5mins back and less than 25mins
find / -mmin +5 –mmin -25
Example25: I have new file called test.txt which is just created, now I want to get all the files which are created later this file creation.
find / -newer test.txt

Search for files/folders depending on the size with –size option

Sl.no +10 10 -10
c for bytes(8 bits) Search for files more than 10c size Search for files exactly 10b size Search for files less than 10b size
k for kilobytes Search for files more than 10k size Search for files exactly 10k size Search for files less than 10k size
M for Megabytes Search for files more than 10M size Search for files exactly 10M size Search for files less than 10M size
G for Gigabytes Search for files more than 10G size Search for files exactly 10G size Search for files less than 10G size

Example26: Search for files whose size is more than 10bytes
find / -size +10c
Example27: Search for files which are exactly 10kb in /opt folder
find /opt –size 10k
Example28: Search for files which are less than 10MB in /var folder
find /var –size -10M
Example29: Search for files which are more than 1GB size in /usr folder
find /usr –size +1G
Example30: find all the empty files in my system
find / -size 0k
Example31: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
Example32: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.
Example33: 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

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.
Example34: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 xvfz 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
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.
Example45:
find /opt –name \*.sh
Or
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.
Example49: 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.
Example52: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 its left and right hand side.
Example53: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.txt
In 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)Dont search in .cvs folder
2)Search for files with out 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

No comments:

Post a Comment