Friday, 22 January 2016

Find Command in UNIX with examples



1. Find Files Using Name in Current Directory
Find all the files whose name is tecmint.txt in a current working directory.
# find . -name tecmint.txt

./tecmint.txt
2. Find Files Under Home Directory
Find all the files under /home directory with name tecmint.txt.
# find /home -name tecmint.txt

/home/tecmint.txt
3. Find Files Using Name and Ignoring Case
Find all the files whose name is tecmint.txt and contains both capital and small letters in /homedirectory.
# find /home -iname tecmint.txt

./tecmint.txt
./Tecmint.txt
4. Find Directories Using Name
Find all directories whose name is Tecmint in / directory.
# find / -type d -name Tecmint

/Tecmint
5. Find PHP Files Using Name
Find all php files whose name is tecmint.php in a current working directory.
# find . -type f -name tecmint.php

./tecmint.php
6. Find all PHP Files in Directory
Find all php files in a directory.
# find . -type f -name "*.php"

./tecmint.php
./login.php
./index.php
Part II – Find Files Based on their Permissions
7. Find Files With 777 Permissions
Find all the files whose permissions are 777.
# find . -type f -perm 0777 -print
8. Find Files Without 777 Permissions
Find all the files without permission 777.
# find / -type f ! -perm 777
9. Find SGID Files with 644 Permissions
Find all the SGID bit files whose permissions set to 644.
# find / -perm 2644
10. Find Sticky Bit Files with 551 Permissions
Find all the Sticky Bit set files whose permission are 551.
# find / -perm 1551
11. Find SUID Files
Find all SUID set files.
# find / -perm /u=s
12. Find SGID Files
Find all SGID set files.
# find / -perm /g+s
13. Find Read Only Files
Find all Read Only files.
# find / -perm /u=r
14. Find Executable Files
Find all Executable files.
# find / -perm /a=x
15. Find Files with 777 Permissions and Chmod to 644
Find all 777 permission files and use chmod command to set permissions to 644.
# find / -type f -perm 0777 -print -exec chmod 644 {} \;
16. Find Directories with 777 Permissions and Chmod to 755
Find all 777 permission directories and use chmod command to set permissions to 755.
# find / -type d -perm 777 -print -exec chmod 755 {} \;
17. Find and remove single File
To find a single file called tecmint.txt and remove it.
# find . -type f -name "tecmint.txt" -exec rm -f {} \;
18. Find and remove Multiple File
To find and remove multiple files such as .mp3 or .txt, then use.
# find . -type f -name "*.txt" -exec rm -f {} \;

OR

# find . -type f -name "*.mp3" -exec rm -f {} \;
19. Find all Empty Files
To file all empty files under certain path.
# find /tmp -type f -empty
20. Find all Empty Directories
To file all empty directories under certain path.
# find /tmp -type d -empty
21. File all Hidden Files
To find all hidden files, use below command.
# find /tmp -type f -name ".*"
Part III – Search Files Based On Owners and Groups
22. Find Single File Based on User
To find all or single file called tecmint.txt under / root directory of owner root.
# find / -user root -name tecmint.txt
23. Find all Files Based on User
To find all files that belongs to user Tecmint under /home directory.
# find /home -user tecmint
24. Find all Files Based on Group
To find all files that belongs to group Developer under /home directory.
# find /home -group developer
25. Find Particular Files of User
To find all .txt files of user Tecmint under /home directory.
# find /home -user tecmint -iname "*.txt"
Part IV – Find Files and Directories Based on Date and Time
26. Find Last 50 Days Modified Files
To find all the files which are modified 50 days back.
# find / -mtime 50
27. Find Last 50 Days Accessed Files
To find all the files which are accessed 50 days back.
# find / -atime 50
28. Find Last 50-100 Days Modified Files
To find all the files which are modified more than 50 days back and less than 100 days.
# find / -mtime +50 –mtime -100
29. Find Changed Files in Last 1 Hour
To find all the files which are changed in last 1 hour.
# find / -cmin -60
30. Find Modified Files in Last 1 Hour
To find all the files which are modified in last 1 hour.
# find / -mmin -60
31. Find Accessed Files in Last 1 Hour
To find all the files which are accessed in last 1 hour.
# find / -amin -60
Part V – Find Files and Directories Based on Size
32. Find 50MB Files
To find all 50MB files, use.
# find / -size 50M
33. Find Size between 50MB – 100MB
To find all the files which are greater than 50MB and less than 100MB.
# find / -size +50M -size -100M
34. Find and Delete 100MB Files
To find all 100MB files and delete them using one single command.
# find / -size +100M -exec rm -rf {} \;
35. Find Specific Files and Delete
Find all .mp3 files with more than 10MB and delete them using one single command.
# find / -type f -name *.mp3 -size +10M -exec rm {} \;
That’s it, We are ending this post here, In our next article we will discuss more about other Linux commands in depth with practical examples. Let us know your opinions on this article using our comment section.

FIND COMMAND IN UNIX AND LINUX EXAMPLES

Find is one of the powerful utility of Unix (or Linux) used for searching the files in a directory hierarchy. The syntax of find command is


find [pathnames] [conditions]

Let see some practical exercises on using find command.

1. How to run the last executed find command?


!find

This will execute the last find command. It also displays the last find command executed along with the result on the terminal.

2. How to find for a file using name?


find -name "sum.java"
./bkp/sum.java
./sum.java

This will find all the files with name "sum.java" in the current directory and sub-directories.

3. How to find for files using name and ignoring case?


find -iname "sum.java"
./SUM.java
./bkp/sum.java
./sum.java

This will find all the files with name "sum.java" while ignoring the case in the current directory and sub-directories.

4. How to find for a file in the current directory only?


find -maxdepth 1 -name "sum.java"
./sum.java

This will find for the file "sum.java" in the current directory only

5. How to find for files containing a specific word in its name?


find -name "*java*"
./SUM.java
./bkp/sum.java
./sum.java
./multiply.java

It displayed all the files which have the word "java" in the filename

6. How to find for files in a specific directory?


find /etc -name "*java*"

This will look for the files in the /etc directory with "java" in the filename

7. How to find the files whose name are not "sum.java"?


find -not -name "sum.java"
.
./SUM.java
./bkp
./multiply.java

This is like inverting the match. It prints all the files except the given file "sum.java".

8. How to limit the file searches to specific directories?


find -name "sum.java"
./tmp/sum.java
./bkp/var/tmp/files/sum.java
./bkp/var/tmp/sum.java
./bkp/var/sum.java
./bkp/sum.java
./sum.java

You can see here the find command displayed all the files with name "sum.java" in the current directory and sub-directories.

a. How to print the files in the current directory and one level down to the current directory?


find -maxdepth 2 -name "sum.java"
./tmp/sum.java
./bkp/sum.java
./sum.java

b. How to print the files in the current directory and two levels down to the current directory?


find -maxdepth 3 -name "sum.java"
./tmp/sum.java
./bkp/var/sum.java
./bkp/sum.java
./sum.java

c. How to print the files in the subdirectories between level 1 and 4?


find -mindepth 2 -maxdepth 5 -name "sum.java"
./tmp/sum.java
./bkp/var/tmp/files/sum.java
./bkp/var/tmp/sum.java
./bkp/var/sum.java
./bkp/sum.java

9. How to find the empty files in a directory?


find . -maxdepth 1 -empty
./empty_file

10. How to find the largest file in the current directory and sub directories


find . -type f -exec ls -s {} \; | sort -n -r | head -1

The find command "find . -type f -exec ls -s {} \;" will list all the files along with the size of the file. Then the sort command will sort the files based on the size. The head command will pick only the first line from the output of sort.

11. How to find the smallest file in the current directory and sub directories


find . -type f -exec ls -s {} \; | sort -n -r | tail -1

Another method using find is


find . -type f -exec ls -s {} \; | sort -n  | head -1

12. How to find files based on the file type?

a. Finding socket files


find . -type s

b. Finding directories


find . -type d

c. Finding hidden directories


find -type d -name ".*"

d. Finding regular files


find . -type f

e. Finding hidden files


find . -type f -name ".*"

13. How to find files based on the size?

a. Finding files whose size is exactly 10M


find . -size 10M

b. Finding files larger than 10M size


find . -size +10M

c. Finding files smaller than 10M size


find . -size -10M

14. How to find the files which are modified after the modification of a give file.


find -newer "sum.java"

This will display all the files which are modified after the file "sum.java"

15. Display the files which are accessed after the modification of a give file.


find -anewer "sum.java"

16. Display the files which are changed after the modification of a give file.


find -cnewer "sum.java"

17. How to find the files based on the file permissions?


find . -perm 777

This will display the files which have read, write, and execute permissions. To know the permissions of files and directories use the command "ls -l".

18. Find the files which are modified within 30 minutes.


find . -mmin -30

19. Find the files which are modified within 1 day.


find . -mtime -1

20. How to find the files which are modified 30 minutes back


find . -not -mmin -30

21. How to find the files which are modified 1 day back.


find . -not -mtime -1

22. Print the files which are accessed within 1 hour.


find . -amin -60

23. Print the files which are accessed within 1 day.


find . -atime -1

24. Display the files which are changed within 2 hours.


find . -cmin -120

25. Display the files which are changed within 2 days.


find . -ctime -2

26. How to find the files which are created between two files.


find . -cnewer f1 -and ! -cnewer f2

So far we have just find the files and displayed on the terminal. Now we will see how to perform some operations on the files.

1. How to find the permissions of the files which contain the name "java"?


find -name "*java*"|xargs ls -l

Alternate method is


find -name "*java*" -exec ls -l {} \;

2. Find the files which have the name "java" in it and then display only the files which have "class" word in them?


find -name "*java*" -exec grep -H class {} \;

3. How to remove files which contain the name "java".


find -name "*java*" -exec rm -r {} \;

This will delete all the files which have the word “java" in the file name in the current directory and sub-directories.

Similarly you can apply other Unix commands on the files found using the find command. I will add more examples as and when i found.
basic 'find file' commands
--------------------------
find / -name foo.txt -type f -print             # full command
find / -name foo.txt -type f                    # -print isn't necessary
find / -name foo.txt                            # don't have to specify "type==file"
find . -name foo.txt                            # search under the current dir
find . -name "foo.*"                            # wildcard
find . -name "*.txt"                            # wildcard
find /users/al -name Cookbook -type d           # search '/users/al'
 
search multiple dirs
--------------------
find /opt /usr /var -name foo.scala -type f     # search multiple dirs
 
case-insensitive searching
--------------------------
find . -iname foo                               # find foo, Foo, FOo, FOO, etc.
find . -iname foo -type d                       # same thing, but only dirs
find . -iname foo -type f                       # same thing, but only files
 
find files with different extensions
------------------------------------
find . -type f \( -name "*.c" -o -name "*.sh" \)                       # *.c and *.sh files
find . -type f \( -name "*cache" -o -name "*xml" -o -name "*html" \)   # three patterns
 
find files that don't match a pattern (-not)
--------------------------------------------
find . -type f -not -name "*.html"                                # find all files not ending in ".html"
 
find files by text in the file (find + grep)
--------------------------------------------
find . -type f -name "*.java" -exec grep -l StringBuffer {} \;    # find StringBuffer in all *.java files
find . -type f -name "*.java" -exec grep -il string {} \;         # ignore case with -i option
find . -type f -name "*.gz" -exec zgrep 'GET /foo' {} \;          # search for a string in gzip'd files
 
5 lines before, 10 lines after grep matches
-------------------------------------------
find . -type f -name "*.scala" -exec grep -B5 -A10 'null' {} \;
     (see http://alvinalexander.com/linux-unix/find-grep-print-lines-before-after-...)
 
find files and act on them (find + exec)
----------------------------------------
find /usr/local -name "*.html" -type f -exec chmod 644 {} \;      # change html files to mode 644
find htdocs cgi-bin -name "*.cgi" -type f -exec chmod 755 {} \;   # change cgi files to mode 755
find . -name "*.pl" -exec ls -ld {} \;                            # run ls command on files found
 
find and copy
-------------
find . -type f -name "*.mp3" -exec cp {} /tmp/MusicFiles \;       # cp *.mp3 files to /tmp/MusicFiles
 
copy one file to many dirs
--------------------------
find dir1 dir2 dir3 dir4 -type d -exec cp header.shtml {} \;      # copy the file header.shtml to those dirs
 
find and delete
---------------
find . -type f -name "Foo*" -exec rm {} \;                        # remove all "Foo*" files under current dir
find . -type d -name CVS -exec rm -r {} \;                        # remove all subdirectories named "CVS" under current dir
 
find files by modification time
-------------------------------
find . -mtime 1               # 24 hours
find . -mtime -7              # last 7 days
find . -mtime -7 -type f      # just files
find . -mtime -7 -type d      # just dirs
 
find files by modification time using a temp file
-------------------------------------------------
touch 09301330 poop           # 1) create a temp file with a specific timestamp
find . -mnewer poop           # 2) returns a list of new files
rm poop                       # 3) rm the temp file
 
find with time: this works on mac os x
--------------------------------------
find / -newerct '1 minute ago' -print
 
find and tar
------------
find . -type f -name "*.java" | xargs tar cvf myfile.tar
find . -type f -name "*.java" | xargs tar rvf myfile.tar
     (see http://alvinalexander.com/blog/post/linux-unix/using-find-xargs-tar-crea...
     for more information)
 
find, tar, and xargs
--------------------
find . -name -type f '*.mp3' -mtime -180 -print0 | xargs -0 tar rvf music.tar
     (-print0 helps handle spaces in filenames)
     (see http://alvinalexander.com/mac-os-x/mac-backup-filename-directories-space...)
 
find and pax (instead of xargs and tar)
---------------------------------------
find . -type f -name "*html" | xargs tar cvf jw-htmlfiles.tar -
find . -type f -name "*html" | pax -w -f jw-htmlfiles.tar
     (see http://alvinalexander.com/blog/post/linux-unix/using-pax-instead-of-tar)


locate command
--------------
locate tomcat.sh          # search the entire filesystem for 'tomcat.sh' (uses the locate database)
locate -i spring.jar      # case-insensitive search

Basic find command examples

This first Linux find example searches through the root filesystem ("/") for the file named Chapter1. If it finds the file, it prints the location to the screen.
find / -name Chapter1 -type f -print
On Linux systems and modern Unix system you no longer need the -print  option at the end of thefind command, so you can issue it like this:
find / -name Chapter1 -type f
The -type f option here tells the find command to return only files. If you don't use it, the find command will returns files, directories, and other things like named pipes and device files that match the name pattern you specify. If you don't care about that, just leave the -type f option off your command.
This next find command searches through only the /usr and /home directories for any file namedChapter1.txt:
find /usr /home -name Chapter1.txt -type f
To search in the current directory -- and all subdirectories -- just use the . character to reference the current directory in your find commands, like this:
find . -name Chapter1 -type f
This next example searches through the /usr directory for all files that begin with the letters Chapter, followed by anything else. The filename can end with any other combination of characters. It will match filenames such as Chapter, Chapter1, Chapter1.bad, Chapter-in-life, etc.:
find /usr -name "Chapter*" -type f
This next command searches through the /usr/local directory for files that end with the extension.html. These file locations are then printed to the screen.
find /usr/local -name "*.html" -type f

Find directories with the Unix find command

Every option you just saw for finding files can also be used on directories. Just replace the -f option with a -d option. For instance, to find all directories named build under the current directory, use this command:
find . -type d -name build

Find files that don't match a pattern

To find all files that don't match a filename pattern, use the -not argument of the find command, like this:
find . -type f -not -name "*.html"
That generates a list of all files beneath the current directory whose filename DOES NOT end in .html, so it matches files like *.txt, *.jpg, and so on.

Finding files that contain text (find + grep)

You can combine the Linux find and grep commands to powerfully search for text strings in many files.
This next command shows how to find all files beneath the current directory that end with the extension .java, and contain the characters StringBuffer. The -l argument to the grep command tells it to just print the name of the file where a match is found, instead of printing all the matches themselves:
find . -type f -name "*.java" -exec grep -l StringBuffer {} \;
(Those last few characters are required any time you want to exec a command on the files that are found. I find it helpful to think of them as a placeholder for each file that is found.)
This next example is similar, but here I use the -i argument to the grep command, telling it to ignore the case of the characters string, so it will find files that contain string, String, STRING, etc.:
find . -type f -name "*.java" -exec grep -il string {} \;

Acting on files you find (find + exec)

This command searches through the /usr/local directory for files that end with the extension .html. When these files are found, their permission is changed to mode 644 (rw-r--r--).
find /usr/local -name "*.html" -type f -exec chmod 644 {} \;
This find command searches through the htdocs and cgi-bin directories for files that end with the extension .cgi. When these files are found, their permission is changed to mode 755 (rwxr-xr-x). This example shows that the find command can easily search through multiple sub-directories (htdocs, cgi-bin) at one time:
find htdocs cgi-bin -name "*.cgi" -type f -exec chmod 755 {} \;

Running the ls command on files you find

From time to time I run the find command with the ls command so I can get detailed information about files the find command locates. To get started, this find command will find all the *.pl files (Perl files) beneath the current directory:
find . -name "*.pl"
In my current directory, the output of this command looks like this: 
./news/newsbot/old/3filter.pl
./news/newsbot/tokenParser.pl
./news/robonews/makeListOfNewsURLs.pl
That's nice, but what if I want to see the last modification time of these files, or their filesize? No problem, I just add the ls -ld command to my find command, like this:
find . -name "*.pl" -exec ls -ld {} \;
This results in this very different output:
-rwxrwxr-x 1 root root 2907 Jun 15  2002 ./news/newsbot/old/3filter.pl
-rwxrwxr-x 1 root root 336  Jun 17  2002 ./news/newsbot/tokenParser.pl
-rwxr-xr-x 1 root root 2371 Jun 17  2002 ./news/robonews/makeListOfNewsURLs.pl
The "-l" flag of the ls command tells ls to give me a "long listing" of each file, while the -d flag is extremely useful in this case; it tells ls to give me the same output for a directory. Normally if you use the ls command on a directory, ls will list the contents of the directory, but if you use the -d option, you'll get one line of information, as shown above.

Find and delete

Be very careful with these next two commands. If you type them in wrong, or make the wrong assumptions about what you're searching for, you can delete a lot of files very fast. Make sure you have backups and all that, you have been warned.
Here's how to find all files beneath the current directory that begin with the letters 'Foo' and delete them.
find . -type f -name "Foo*" -exec rm {} \;
This one is even more dangerous. It finds all directories named CVS, and deletes them and their contents. Just like the previous command, be very careful with this command, it is dangerous(!), and not recommended for newbies, or if you don't have a backup.
find . -type d -name CVS -exec rm -r {} \;

Find files with different file extensions

The syntax to find multiple filename extensions with one command looks like this:
find . -type f \( -name "*.c" -o -name "*.sh" \)
Just keep adding more "-o" (or) options for each filename extension. Here's a link to

Case-insensitive file searching

To perform a case-insensitive search with the Unix/Linux find command, use the -iname option instead of -name. So, to search for all files and directories named foo, FOO, or any other combination of uppercase and lowercase characters beneath the current directory, use this command:
find . -iname foo
If you're just interested in directories, search like this:
find . -iname foo -type d
And if you're just looking for files, search like this:
find . -iname foo -type f

Find files by modification time

To find all files and directories that have been modified in the last seven days, use this find command:
find . -mtime -7
To limit the output to just files, add the "-type f" option as shown earlier:
find . -mtime -7 -type f
and to show just directories:
find . -mtime -7 -type d