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.
27. Finding the Top 5 Big Files
The following command will display the top 5 largest file in the current directory and its subdirectory. This may take a while to execute depending on the total number of files the command has to process.# find . -type f -exec ls -s {} \; | sort -n -r | head -5
28. Finding the Top 5 Small Files
Technique is same as finding the bigger files, but the only difference the sort is ascending order.# find . -type f -exec ls -s {} \; | sort -n | head -5
In the above command, most probably you will get to see only the ZERO byte files ( empty files ). So, you can use the following command to list the smaller files other than the ZERO byte files.
# find . -not -empty -type f -exec ls -s {} \; | sort -n | head -5
29. Find Files Based on file-type using option -type
Find only the socket files.# find . -type s
Find all directories
# find . -type d
Find only the normal files
# find . -type f
Find all the hidden files
# find . -type f -name ".*"
Find all the hidden directories
# find -type d -name ".*"
30. Find Files by Size
Using the -size option you can find files by size.Find files bigger than the given size
# find ~ -size +100M
Find files smaller than the given size
# find ~ -size -100M
Find files that matches the exact given size
# find ~ -size 100M
Note: – means less than the give size, + means more than the given size, and no symbol means exact given size.
No comments:
Post a Comment