35 Practical Examples of the Linux Search Command

s2 0

The Linux Find command is one of the most important and widely used command line utilities in Unix-like operating systems. The find command is used to locate and find the list of files and directories based on conditions you specify on files that match the arguments.

Find command can be used in a variety of conditions as you can find files by Permissions, user, groups, Data types, date, size, and other possible criteria.

[ You might also like: 5 Command Line Tools to Find Files Quickly in Linux ]

In this article we share our daily Linux Find command Experiences and their application in the form of examples.

In this article, we will show you the most common ones 35 Examples of search commands under Linux. We’ve broken the section into five parts, from basic to advanced use of the find command.

  • part One: Basic search commands for finding files by name
  • Part II: Find files based on their permissions
  • Part III: Search files based on owners and groups
  • Part IV: Search files and directories by date and time
  • Part V: Search files and directories by size
  • Part VI: Find multiple filenames in Linux
Part One – Basic search commands for finding files by name

1. Search for files by name in the current directory

Find all files by name tecmint.txt in a current working directory.

# find . -name tecmint.txt

./tecmint.txt

2. Search for files in the base directory

Find all files under /Homeland Directory with the name tecmint.txt.

# find /home -name tecmint.txt

/home/tecmint.txt

3. Find files by name and are case-insensitive

Find all files by name tecmint.txt and contains both uppercase and lowercase letters in /Homeland Directory.

# find /home -iname tecmint.txt

./tecmint.txt
./Tecmint.txt

4. Search for directories by name

Find all the directories by name Tecmint in / Directory.

# find / -type d -name Tecmint

/Tecmint

5. Find PHP files by 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 the PHP files in the directory

Find all php Files in a directory.

# find . -type f -name "*.php"

./tecmint.php
./login.php
./index.php
Part II – Search files based on their permissions

7. Find files with 777 permissions

Find all files whose permissions are 777.

# find . -type f -perm 0777 -print

8. Find files without 777 permissions

Find all files without permission 777.

# find / -type f ! -perm 777

9. Find SGID files with 644 permissions

Find all of them SGID bit Files with permissions on. are set 644.

# find / -perm 2644

10. Find sticky bit files with 551 permissions

Find all of them Sticky bit set files whose permission is 551.

# find / -perm 1551

11. Find SUID files

Find all SOUTH Set files.

# find / -perm /u=s

12. Find SGID files

Find all SGID Set files.

# find / -perm /g=s

[ You might also like: How to Find Files With SUID and SGID Permissions in Linux ]

13. Find read-only files

Find all Read only Files.

# find / -perm /u=r

14. Find executables

Find all Executable Files.

# find / -perm /a=x

15. Find files with 777 permissions and chmod on 644

Find all 777 Permission files and use the chmod Command to set permissions on 644.

# find / -type f -perm 0777 -print -exec chmod 644 {} ;

16. Find directories with 777 permissions and chmod on 755

Find all 777 Permission directories and use the chmod Command to set permissions on 755.

# find / -type d -perm 777 -print -exec chmod 755 {} ;

17. Find and remove individual files

How 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 files

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 {} ;

[ You might also like: 4 Useful Tools to Find and Delete Duplicate Files in Linux ]

19. Find all empty files

To find all empty files under a specific path.

# find /tmp -type f -empty

20. Find all empty directories

To save all empty directories under a certain path.

# find /tmp -type d -empty

21. Discard all hidden files

To find all the hidden files use the following command.

# find /tmp -type f -name ".*"
Part III – Search files based on owners and groups

22. Find single file based on user

How to find all or individual files named tecmint.txt under / root directory of the owner root.

# find / -user root -name tecmint.txt

23. Find all files based on user

To find all files owned by the user Tecmint under /Homeland Directory.

# find /home -user tecmint

24. Find all files based on group

To find all files belonging to the group developer under /Homeland Directory.

# find /home -group developer

25. Find specific files belonging to the user

To find everyone .TXT Files of the user Tecmint under /Homeland Directory.

# find /home -user tecmint -iname "*.txt"
Part IV – Finding Files and Directories Based on Date and Time

26. Find files that have changed in the last 50 days

To find all files that have changed 50 Days back.

# find / -mtime 50

27. Find files that were accessed in the last 50 days

To find all the files that are being accessed 50 Days back.

# find / -atime 50

28. Find files that have changed in the last 50-100 days

To find all files that have been more than changed 50 Days back and less than 100 Days.

# find / -mtime +50 –mtime -100

29. Find files that have changed in the last 1 hour

To find all files that were last changed 1 hour.

# find / -cmin -60

30. Find files that have changed in the last 1 hour

To find all files that were last changed 1 hour.

# find / -mmin -60

31. Look for files that have been accessed in the last hour

To find all files that were last accessed 1 hour.

# find / -amin -60
Part V – Find files and directories based on size

32. Look for 50MB files

To find everyone 50 MB Files, use.

# find / -size 50M

33. Find size between 50MB – 100MB

To find all files larger than 50 MB and less than 100 MB.

# find / -size +50M -size -100M

34. Find and delete 100MB files

To find everyone 100 MB Files and delete them with a single command.

# find / -type f -size +100M -exec rm -f {} ;

35. Find and delete specific files

Find all .mp3 Files with more than 10 MB and delete them with a single command.

# find / -type f -name *.mp3 -size +10M -exec rm {} ;

[ You might also like: How to Find a Specific String or Word in Files and Directories ]

That’s it, we end this post here, in our next article we will discuss more Linux commands in detail with practical examples. Let us know what you think about this article in our comments section.

Tags: