25 basic 'find' command to search files in Linux with Examples

find command in linux

In Linux/Unix 'find' command is one of the most important and repeatedly used command for searching files and directories. It supports various arguments and options on how to find the files/directories. Even you can find files and folders via GUI in Linux it will be more friendly and easy to use, but the difficult part of the GUI is, when you try with the larger size it may take a long time to get the output. Hence, most of the experienced System Admins use only the command-line search because it is fast, robust and powerful.

We can use various options with 'find' command in Linux and it supports to search by file name, folder name, creation date, modification date, users, groups, and permission. Once you are familiar with these commands it will be extremely easy to get the output from the Terminal.

This guide will help you how to use various options with find commands. All the below examples are tested on RHEL/CENTOS 7.6

The Global Syntax of the find command:

find [path...] [options] [expression]

1. How to search a file with a particular name?

# find . -name "linuxteck.txt"

 

Output:

./linuxteck.txt

Note: Here we used '-name' argument to search a file named "linuxteck.txt" and " . " a dot represents the present working directory. To check your present working directory, use 'pwd' command.

2. How to search a case-insensitive file with a particular name?

# find . -iname linuxteck.txt

 

Output:

./linuxteck.txt
./Linuxteck.txt

Note: Here we used 'iname' option to search a case-insensitive file named "linuxteck.txt". In the above example, it will list out both capital and small letter file named "linuxteck.txt" from the present working directory.

3. How to search a file in a particular directory?

# find /var/test/ -name linuxteck.txt

 

Output:

/var/test/linuxteck.txt

Note: We can directly search files from a specific folder/directory. In the above example, I have given the directory path "/var/test" to find a file named "linuxteck.txt".

4. How to search all directories using the 'type' argument?

# find /var/test/ -type d

 

Output :

/var/test/
/var/test/linuxteck.com
/var/test/linuxteck

Note: The '-type' argument provides the following options when searching for files:

d – directory or folder
f – normal file
l – symbolic link
c – character devices
b – block devices

Using the '-type' argument we can segregate the search output by a file or directory or symbolic link or block devices. In the above example I have used the -type d' to list out all the directories under "/var/test", this will exclude the files, symlink, etc.

5. How to combine the '-type and -name' option to search a file?

# find . -type f -name linuxteck.txt

 

Output :

./linuxteck.txt

Note: Using '-type f' option to find only for files. In this example, it will search for a file named "linuxteck.txt" and exclude the directories, symlinks, etc.

6. How to search for files with a particular format/extension?

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

 

Output:

./logs/error.php
./index.php
./cli/deletefiles.php
./cli/update_cron.php
./cli/finder_indexer.php
./cli/garbagecron.php

Note: In this example, it will list out all the ".php" extension files from the present working directory. In real-time, you can use this command to search for a particular extension.

7. How to search a file by name and delete?

# find . -name linuxteck.txt -delete

Note: Using '-delete' argument to delete a file is very easy and simple, but it is quite dangerous, as it will not ask you the confirmation. This option will perform better because it doesn't spawn a new process, hence, it is recommended for Superusers only and for the normal users can use delete command with confirmation option, so that you can double check before the deletion of files or folders.

8. How to delete a file with confirmation using execute (-exec) option?

# find . -name linuxteck.txt -exec rm -i {} \;
rm: remove regular empty file './linuxteck.txt'? y

Note: In this example, the above command will prompt a confirmation, whether you want to delete linuxteck.txt or not. if you press 'y’ it will delete the file. The basic advantage of using '-exec' can give you some more control over the actual command i.e., you can pass some arguments like rm, mv, etc. This command is recommended concerning deletion.

9. How to search all the accessed files older than 10 days ago?

# find / -atime 10

Note: Using '-atime' Access Time, we can find the most recently access files either it will be read or written into. The above command will list out all the files that were accessed 10 days ago from the current time. To know more about atime,mtime and ctime click here

10. How to search all the modified files older than 10 days ago?

# find / -mtime 10

Note: Using '-mtime' Modification Time - We can find the most recently modified files. The above command will list out all the files that have modified 10 days ago from the current time. We can use plus (+) and minus (-) signs accordingly before or after the number of days.

11. How to search for all the changed files less than a day?

# find / -ctime -1

Note: Using '-ctime' Change Time - We can find the recently updated timestamp of files. The above command will list out all the files whose inode was updated less than a day.

12. How to search all the modified files in the last hour?

# find / -mmin -60

Note: Using '-mmin' min argument - The above example will look for all the files that were modified in the last 1 hour.

13. How to search for all the 10MB files in your system?

# find / -size 10M

Note: The above example will search for all the files in your system that is matched with 10MB size.

14. How to search all the files between 10-20MB in size?

# find / -size +10M -size -20M

Note: The above example will search for all the files in your system between 10M to 20M in size. Means it will list out all the files greater than 10MB and less than 20MB.

15. How to search and delete more than 1GB file in one-shot?

# find / -size +1G -exec rm -rfv {} \;

Note: The above command will search all the files in your system that are more than 1GB in size and delete directly without any confirmation. Similarly, you can use plus (+) and minus (-) signs to filter your searches further.

16. How to search and delete a file with a particular extension/format?

find . -type f -name "*.sql" -size +100M -exec rm -i {} \;

Note: The above example will search all the ".sql" extension files that are greater than 100M and delete it with a confirmation.

17. How to search for files based on ownership?

# find / -user linuxteck

 

Output:

/home/linuxteck/linux
/home/linuxteck/apache
/home/linuxteck/php
/home/linuxteck/mysql
/home/linuxteck/phpmyadmin

Note: This command will list out all the files which belong to the particular user. In the above example it will list out all the files that are belongs to the "linuxteck" user, few samples are attached. You can test it based on your requirement.

18. How to search for files based on group names?

# find / -group education

Note: The above example will list out the files that are belongs to a group named education.

19. How to search for files based on permission?

# find / -perm 644

Note: Using '-perm' option we can search the files based on file permissions. The above example will list out all the files that have only 644 permission, which means in Linux it (644) corresponds to read and write privilege. Similarly, you can play around with different permission of files based on your requirements.

20. How to find out the files with wrong permission?

# find / -type f ! -perm 0777

Note: This command will help you to identify the files with wrong permissions which can lead to a security breach.

21. How to search only the empty files?

# find / -type f -empty

Note: The above command will search only the empty file on your computer.

22. How to search only the empty folder?

# find / -type d -empty

Note: The above command will search only the empty folders on your computer. Similarly, you can search the empty files and folders based on the particular path as per your requirement.

23. How to search the empty files and folders?

# find / -empty

Note: The above command will search and list all the empty files and folders on your computer.

24. How to search all the Hidden Files on your system?

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

Note: The above command will list out all the Hidden Files on your system. In Linux, all the hidden files are marked as "." DOT at the beginning of each file.

25. How to search a "text" from multiple files?

# find / -type f -name "*.txt" -exec grep 'LinuxTeck' {} \;

 

Output:

LinuxTeck.com
LinuxTeck.com

Note: The above example will list out the lines which have "linuxteck"

Thank you for taking the time to read! I hope this article will help you to learn 'find' commands with examples. Drop me your feedback/comments. If you like this article, kindly share it 🙂, so that it may help others as well.

Thank you!

If you like the article ? Please share it and help us grow 🙂

One Comment on “25 basic 'find' command to search files in Linux with Examples”

Leave a Reply

Your email address will not be published. Required fields are marked *