Jump to content

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


aum

Recommended Posts

find_command.jpg

 

The 'find' command in Linux/Unix is known to be one of the most important and frequently used commands for searching  files and directories. It supports different arguments and options for finding files and directories. Even if you can find files and folders in Linux via GUI it will be more friendly and easier to use, but the GUI may require a long time to load with larger sizes.. As a result, experienced System Administrators use only the command-line search because of its reliability, speed, and power.

 

In Linux, the 'find' command can be used to find files and folders based on their names, their creation dates, their modification dates, their owners, and their permissions. Once you have mastered these commands, it will be fairly easy to retrieve data from the Terminal.

 

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

 

Global Syntax of the find command:

 

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

 

Table of Contents  show 

 

1. How to search a file with a particular name?
2. How to search a case-insensitive file with a particular name?
3. How to search a file in a particular directory?
4. How to search all directories using the 'type' argument?
5. How to combine the '-type and -name' option to search for a file?
6. How to search for files with a particular format/extension?
7. How to search a file by name and delete it?
8. How to delete a file with confirmation using the execute (-exec) option?
9. How to search all the accessed files older than 10 days ago?
10. How to search all the modified files older than 10 days ago?
11. How to search for all the changed files in less than a day?
12. How to search all the modified files in the last hour?
13. How to search for all the 10MB files in your system?
14. How to search all the files between 10-20MB in size?
15. How to search and delete more than 1GB of files in one-shot?
16. How to search and delete a file with a particular extension/format?
17. How to search for files based on ownership?
18. How to search for files based on group names?
19. How to search for files based on permission?
20. How to find the files with the wrong permissions?
21. How to search only for empty files?
22. How to search only for the empty folder?
23. How to search for empty files and folders?
24. How to search for all the Hidden Files on your system?
25. How to search "text" from multiple files?


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

 

# find . -name "linuxteck.txt"

 

Output:

./linuxteck.txt

 

Note: Here we used the '-name' argument to search a file named "linuxteck.txt" and " . " a dot represents the present working directory. To check your present working directory, use the '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 the 'iname' option to search for 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: You can search for files directly 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 for a file?


# find . -type f -name linuxteck.txt

 

Output :

./linuxteck.txt

 

Note: Use the '-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 it?


# find . -name linuxteck.txt -delete

 

Note: Using the '-delete' argument to delete a file is very easy and simple, but it is quite dangerous, as it will not ask you for confirmation. This option will perform better because it doesn't spawn a new process, hence, it is recommended for Superusers only and for normal users to be able to use the 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 the 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 recent access files either it will be read or written into. The above command will list all the files that were accessed 10 days ago from the current time. To learn 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 the '-mtime' Modification Time - We can find the most recently modified files. The above command will list out all the files that have been 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 in less than a day?


# find / -ctime -1

 

Note: Using the '-ctime' Change Time - You can find the recently updated timestamp of the files. The above command will list out all the files whose inode was updated in 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 match the size of 10MB.

 

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 and 20M in size. Meaning it will list out all the files greater than 10MB and less than 20MB.

 

15. How to search and delete more than 1GB of files 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 them directly without any confirmation. Similarly, you can use plus (+) and minus (-) signs to filter your searches further.

 

To find the size of a file in Linux, aside from the above command we can also use the du command, which stands for Disk Usage. Click here for more information about the 'du' command

 

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 them with 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 belong to the "linuxteck" user, few samples are attached. You can test it based on your requirements.

 

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


# find / -group education

 

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

 

19. How to search for files based on permission?


# find / -perm 644

 

Note: Using the '-perm' option, you 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 permissions for files based on your requirements.

 

20. How to find the files with the wrong permissions?


# find / -type f ! -perm 0777

 

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

 

21. How to search only for empty files?


# find / -type f -empty

 

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

 

22. How to search only for the empty folder?


# find / -type d -empty

 

Note: The above command will only search 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 for 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 for all the Hidden Files on your system?

 

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

 

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

 

25. How to search "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"

 

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!

 

Source

Link to comment
Share on other sites


  • Views 1.2k
  • Created
  • Last Reply

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...