- Shell: find files in a list under a directory
- 4 Answers 4
- 25+ most used find commands in Linux [Cheat Sheet]
- Syntax to use find command
- Different examples to use find command
- 1. Find all files and directories in the current directory
- 2. Find files in the specific directory
- 3. Find only directories with find command
- 4. Find only files with find command
- 5. Find symbolic link files only
- 6. Find files by name
- 7. Find directories by names
- 8. Find files by case insensitive name
- 9. Find specific files by extension
- 10. Find files having specific patterns
- 11. Find empty files and directories
- 12. Find files with executable permission
- 13. Find files based on size
- 14. Find files by size range
- 15. Find files by permission
- 16. Find files by modification time
- 17. Find files by last accessed time
- 18. Find files by changed time
- 19. Find all files owned by specific user
- 20. Find all files owned by specific group name
- 21. Find and delete files
- 22. Find text in files
- 23. Find text files and view their content
- 24. Find the bash script file and execute it
- 25. Find files by limiting the directory depth to search
- 26. Find and print files that do not match the pattern
- Conclusion
- What’s Next
- Further Reading
Shell: find files in a list under a directory
I have a list containing about 1000 file names to search under a directory and its subdirectories. There are hundreds of subdirs with more than 1,000,000 files. The following command will run find for 1000 times:
cat filelist.txt | while read f; do find /dir -name $f; done
4 Answers 4
If filelist.txt has a single filename per line:
(The -f option means that grep searches for all the patterns in the given file.)
sed 's@^@/@; s/$/$/; s/\([\.[\*]\|\]\)/\\\1/g' filelist.txt > processed_filelist.txt find /dir | grep -f processed_filelist.txt
The call to sed runs the commands s@^@/@ , s/$/$/ and s/\([\.[\*]\|\]\)/\\\1/g on each line of filelist.txt and prints them out. These commands convert the filenames into a format that will work better with grep.
- s@^@/@ means put a / at the before each filename. (The ^ means «start of line» in a regex)
- s/$/$/ means put a $ at the end of each filename. (The first $ means «end of line», the second is just a literal $ which is then interpreted by grep to mean «end of line»).
The combination of these two rules means that grep will only look for matches like . / , so that a.txt doesn’t match ./a.txt.backup or ./abba.txt .
s/\([\.[\*]\|\]\)/\\\1/g puts a \ before each occurrence of . [ ] or * . Grep uses regexes and those characters are considered special, but we want them to be plain so we need to escape them (if we didn’t escape them, then a file name like a.txt would match files like abtxt ).
$ cat filelist.txt file1.txt file2.txt blah[2012].txt blah[2011].txt lastfile $ sed 's@^@/@; s/$/$/; s/\([\.[\*]\|\]\)/\\\1/g' filelist.txt /file1\.txt$ /file2\.txt$ /blah\[2012\]\.txt$ /blah\[2011\]\.txt$ /lastfile$
Grep then uses each line of that output as a pattern when it is searching the output of find .
25+ most used find commands in Linux [Cheat Sheet]
find command is one of the popular and commonly used tools in Unix-based operating systems. As its name suggests, it finds the files and directories in a directory hierarchy. You can pass different parameters and search files by their name, extension, type, size, permissions, modification time, owner, groups, and more. It performs a recursive search on the specified directory which means it searches in all sub-directories too.
Syntax to use find command
The syntax to use the find command is:
$ find [options] [path] [expression]
- options : It includes the options that must appear before the first path name: -H , -L , -P , -D , and -O .
- path : It indicates the directory path where you want to search the files.
- expression : It defines search options, patterns to match, and actions to perform on the files.
The default path is the current working directory. When the find command is used without any parameters, it lists all files and folders in the current directory and its sub-directories.
Different examples to use find command
1. Find all files and directories in the current directory
Use find command followed by . symbol to search for all files and directories in the current working directory.
Sample Output:
2. Find files in the specific directory
To find files in the specific directory, use the following syntax.
The following example searches for all files and directories present in the /etc/apt directory.
Sample Output:
3. Find only directories with find command
The -type option followed by d searches for directories only. The following example finds all directories in the home/golinux/computing directory.
$ find /home/golinux/computing -type d
Sample Output:
4. Find only files with find command
The -type f is used for regular files. It lists all files present in the specified directory.
$ find /home/golinux/computing -type f
Sample Output:
5. Find symbolic link files only
The -type l will search for symbolic links only.
Sample Output:
6. Find files by name
You can find files by name using the -name option. It is case-sensitive. It is useful for checking whether the file is present in the specified directory hierarchy.
Run this command to locate new.txt files in the current working directory.
Sample Output:
7. Find directories by names
Similarly, you can search for directories by name with the following command. It finds all directories having a name spark in the current working directory.
Sample Output:
8. Find files by case insensitive name
You can use -iname option to search for files by name where the match is case insensitive.
Sample Output:
9. Find specific files by extension
With this method, you can find all files in the directory which has the same extension. For instance, to locate all .cpio files, use the command below.
Sample Output:
10. Find files having specific patterns
You can use find command to search files by specific patterns, like files whose name begins with prep . The following command locates files whose name starts with prep in the /tmp directory.
$ find /home/golinux/record -name "tes*"
Sample Output:
# find /tmp/ -type f -name "prep*" /tmp/prepTspInstall18457.log /tmp/prepTspInstall2897.log /tmp/prepTspInstall11221.log /tmp/prepTspInstall15505.log /tmp/prepTspInstall13806.log
11. Find empty files and directories
The -empty option searches for the empty files and directories only.
$ find /home/golinux/test -empty
Sample Output:
12. Find files with executable permission
The -executable flag matches files that are executable and directories that are searchable. To search executable files only in the current directory, you can run the following command.
Sample Output:
13. Find files based on size
You can find files by size using the -size option. The following suffixes are used with -size .
- b : 512 byte blocks (default, if no suffix is used)
- c : bytes
- w : two-byte words
- k : Kilobytes (1024 bytes)
- M : Megabytes
- G : Gigabytes
The following command finds all files that are exactly 960 bytes in the specified directory.
$ find /home/golinux/record -type f -size 960c
Sample Output:
To find files that are greater or smaller than the specified size, use the + or — operator respectively.
This command finds files that are greater than 20MB in the Downloads directory.
$ find /home/golinux/Downloads -type f -size +20MB
The following command finds files that are smaller than 20MB in the Downloads directory.
$ find /home/golinux/Downloads -type f -size -20MB
14. Find files by size range
You can also search files in a specific size range. For example, this command searches for files having sizes between 1 and 20 MB.
$ find /home/golinux/Downloads -type f -size +1M -size -20M
Sample Output:
15. Find files by permission
You can use -perm option to search for files on the basis of permissions. The following command finds all files which have read and write permission for their owner and group but are only readable by other users.
Sample Output:
Similarly, to search for files that have full access (read/write/execute) to anyone, you can use:
16. Find files by modification time
The -mtime option finds files that were last modified n*24 hours ago. The following command will search for all files that are modified 15 days ago.
$ find /home/golinux/test -mtime 15
Sample Output:
You can also use + or — operator for specifying the greater or smaller than n value. For example, this command will search files that are modified less than 15 days ago.
$ find /home/golinux/test -mtime -15
Sample Output:
17. Find files by last accessed time
The -amin option finds files that were last accessed n minutes ago. You can use the following command to search for files that were last accessed 5 minutes ago.
$ find /home/golinux/test -amin 5
Sample Output:
The -atime option finds files that were last accessed n*24 hours ago. This command will search files that were last accessed 5 days ago.
$ find /home/golinux/test -atime 5
18. Find files by changed time
The -cmin option allows you to search files that are changed n minutes ago. You can run the following command to find all files that are changed 5 minutes ago.
Sample Output:
To find files that are changed n days ago, you have to use the -ctime option. The following command searches for all files that are changed 5 days ago.
19. Find all files owned by specific user
You can use -user option to find files that are owned by the specified user. For example, this command will search all files that are owned by the user deepak .
Sample Output:
20. Find all files owned by specific group name
The -group option allows you to search files based on the group name. The following command finds all files in the current directory that belong to the group student .
Sample Output:
21. Find and delete files
You can use -delete flag at the end of a command to delete files if found in the specified location. For example, the following command will remove all .txt files from the directory /home/golinux/test .
$ find /home/golinux/test -name "*.txt" -delete
Sample Output:
It is always the best choice to check files before deleting them.
22. Find text in files
The -exec flag runs the specified command on the files. The following command finds the text book in all .txt files in the current working directory.
$ find . -name "*.txt" -exec grep 'book' <> \;
Here, the -exec executes the grep command with a pattern book to match. The brackets <> are replaced by the filename found by the find command. The semicolon ; denotes the end of a command argument.
Sample Output:
23. Find text files and view their content
Similarly, you can run the cat command with the -exec option to view the content of all text files.
$ find /home/golinux/test -name "*.txt" cat <> \;
Sample Output:
24. Find the bash script file and execute it
The following command finds a bash script file script.sh in the current working directory and runs the script.
$ find . -name "script.sh" -exec bash <> \;
Sample Output:
25. Find files by limiting the directory depth to search
You can limit the recursion of the depth using the -maxdepth or -mindepth flag.
The following command searches for all .txt files in the directories that are not more than 3 levels deeper than the current working directory.
$ find . -maxdepth 3 -name "*.txt"
Sample Output:
The following example includes only the directories that are at least 3 levels deeper than the current working directory.
$ find . -maxdepth 3 -name "*.txt"
Sample Output:
To search for all .txt files in the directories that are at least 2 levels and not more than 4 levels deeper than the current working directory, you can use this command.
$ find . -mindepth -2 -maxdepth 4 -name "*.txt"
26. Find and print files that do not match the pattern
You can use the -not or ! option to search files that do not match the search pattern. For example, to find all files that are not owned by user deepak in the directory /home/golinux/test , you can use the command below.
$ find /home/golinux/test -type f -not -user deepak
$ find /home/golinux/test -type f ! -user deepak
Sample Output:
Conclusion
You have learned various examples of the find command to search files and directories based on different conditions. We have also shown you how to find files and perform operations on them using the find command. find is one of the most powerful commands and very useful for searching files and directories. Now you should know how to use find command in Linux. If you have any questions, please let us know in the comment section.
What’s Next
Further Reading
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!