find command practical examples Can Improve Your Skills

Search for files in a directory hierarchy, finding the files and directories in Linux is very easy using find command. Find will search any set of directories you specify for files that match the given search criteria. You can search for files by name, owner, group, type, permissions, data and other criteria. Learn find command practical examples.

Syntax: find <directory path> <Search pattern> <action>

1. Find Command without any options

It will list out all the files and folders in current directory including hidden along with path. $find Or $find . Or $find -print Or $find . -print

[root@ArkIT-Serv ~]# find
find command

find command

2. Search Files and Directories using name

Looking files and directories based there names simple, we have to use -name parameter. In below example, searching for resolvebugs.txt file. Here you have to provide the name exact as file name because -name option is case sensitive.

[root@ArkIT-Serv ~]# find . -name resolvedbugs.txt
./resolvedbugs.txt

Ignore case we have to use -iname option

[root@ArkIT-Serv ~]# find . -iname Resolvedbugs.txt
./resolvedbugs.txt
[root@ArkIT-Serv ~]# find . -name Resolvedbugs.txt
find -name

find -name

3. Search Only files out of all

Only files i want to list out of all directories and files. We have an option -type f to fetch only files, here f means file

[root@ArkIT-Serv ~]# find . -type f -iname file1.txt
find only files

find only files

4. Search Only Directories within Linux Server

List only directories again we have to use -type d to fetch only directories, here d means directory.

[root@sny-fusion ~]# find . -type d -iname file11
Find directories

Find directories

5. Find all files which are end with same file extension

Some times we did not remember what is the file name, we just know file extension, so find all same extension files in particular path. In this case we are using * wild card character which replace one or more characters.

[root@ArkIT-Serv ~]# find . -name "*.txt"
find all text files

find all text files

6. Locate the files based on their permissions

Ultimate feature of find command is to search files / directories based an their permissions. Some times we did not remember file name Or File extension what we simply remember it’s permissions, still you can find files / directories. This option is very useful for us because if would like to find all full permission files and directories because they are vulnerable.

[root@ArkIT-Serv ~]# find . -perm 0777
Find files and directories based on permissions

Find files and directories based on permissions

7. Files without permissions

As we seen in 6th step is to find all full permission files, now find which are not having mentioned permissions. Here ! = not equal to.

[root@ArkIT-Serv ~]# find . ! -perm 0777
exclude provided string

exclude provided string

8. Search for SGID files / directories using find

SGID = Set Group ID for execution.  All files / directories which as SGID permissions. 

[root@ArkIT-Serv ~]# find / -perm 2755

"<yoastmark

9. Search for SGID files / Directories using alpha permission values

Finding files / directories using numerical permission values, in same way we can also find using character based permissions. 

[root@ArkIT-Serv ~]# find / -perm /u+s
find SUID files

find SUID files

10. Find Sticky bit files

Sticky bit is an special permission to execute files with other user permissions

[root@ArkIT-Serv ~]# find / -perm 1755
Find sticky bit files

Find sticky bit files

11. All SUID set of files

Set User ID is also same like SGID. In SGID we execute with Group ID. SUID we execute using Other User ID.

[root@ArkIT-Serv ~]# find / -perm /u=s

12. Search for Executable files in Server

It’s more important that we should not have executable permissions to unwanted files / not required files. Simply find all executable files and remove there permissions so that protecting our own environment.

 [root@sny-fusion ~]# find . -perm /a+x

13. Find Read only files

Restricted with read only, we can find using below command

[root@sny-fusion ~]# find . -perm /u=r

 

find read only files

find read only files

14. Find files based on permissions and replace there permissions

Wow..!! amazing right with single command we can change entire files / directories permissions

[root@ArkIT-Serv ~]# find . -perm 777 -exec chmod 700 {} \;
[root@ArkIT-Serv ~]# find . -perm 777 -print0 | xargs -0 chmod 755

15. Search for file in multiple directories at the same time

Multiple paths can be accepted in find command simply provide multiple paths one after another find will execute search in both the paths

[root@ArkIT find]#find /root/find/ /root/ -name file1.txt
/root/find/file1.txt
/root/find/file1.txt

16. Delete files which are find in search criteria 

Along with find command make use of -exec to execute continuous commands

find /root/ -type f -name "*.txt" -exec rm -f {} \;
remove text files

remove text files

find / -type f -name "*.txt" -print 0 | xargs -0 rm -f

same can be done using xargs as well

17. Remove Empty files

Do you want just delete / remove empty files from multiple paths by single command use below command

find / -type f -empty -exec rm -f {} \;
delete empty files using find command

delete empty files using find command

18. Delete empty directories from multiple paths

Empty directories on system there is no use with them, we can simply delete. Deleting empty directories from system will clear clutter, can be achieved using below command

find / -type d -empty -exec rm -f {} \;

19.  Modified files list in last 24 Hours + 10 minutes 

File’s data was last modified n*24 hours ago. Awesome option, identify list of files which are modified in last 24hours + n minutes. This will help you out in problem solving, if you know which file is modified recently by some other user. -mtime means file modified time.

find /root/find/ -mtime 10

mention range from in between 24hours + ten minutes to 24 Hours -20 minutes

find /root/find/ -mtime +10 -mtime -20

20. Modified Files in Last 10 Minutes

19th option will give modified files list after 24hours but -mmin can provide results within minutes. File’s data was last modified n minutes ago.

find /root/find/ -mmin 1

21. Find files based on user permissions

Search the files based on user ownership. Find-out how many files owned by particular user.

[root@ArkIT ~]#find / -user admin
/var/spool/mail/admin
/home/admin
/home/admin/.mozilla
/home/admin/.mozilla/extensions
/home/admin/.mozilla/plugins

22. Find files and directories based on group ownership

Locate files and directories based on their group ownership permissions. In below example admin is the group name.

find / -group admin

23. Large files in system can be find using -size option

find command support to search the files based on their size, Here M = MB, G = GB

find / -size 100M

24. specify how depth want to search

Descend at most levels (a non-negative integer) levels of directories below the command line arguments.

-maxdepth 0 means only apply the tests and actions to the command line arguments.

find / -maxdepth 3 -name "*file"

25. Find No User and No Group owned files and directories

Files and directories which does not have user assigned / group assigned can be find using below command

find / -nouser -o -nogroup

Conclusion: There are N number of examples, lot many options with find command is available in single article can’t be fit. We try to elaborate in upcoming posts.

Related Posts

ps command in Linux

25 commonly used Linux Commands

ls command 25 practical examples

Thanks for your wonderful Support and Encouragement

ARK

My Name is ARK. Expert in grasping any new technology, Interested in Sharing the knowledge. Learn more & Earn More

You may also like...

1 Response

  1. Siddhartha says:

    Sir what happened to your youtube channel?I was learning shell scripting by watching your videos.

Leave a Reply