How to find file in subdirectories Linux – find file recursively in Linux. Here is the Unix command to find a file in a directory and subdirectory.
What is Recursive Listing of a Directory
Recursive means if a directory has subdirectories and files, the command will be executed and applied on those files too (recursively).
The simplest way to see the list of files and sub-directories in any specific directory is using tree command.
Tree is a recursive directory listing program that produces a depth indented listing of files. With no arguments, tree lists the files in the current directory. When directory arguments are given, tree lists all the files and/or directories found in the given directories each in turn. Upon completion of listing all files/directories found, tree returns the total number of files and/or directories listed.
tree comamnd
Here is an easier way to perform the recursive search with the tree command:
tree -a
When -a is used with the tree command, all files are printed. By default tree does not print hidden files (those beginning with a dot .’). Also, in no event does tree print the file system constructs.’ (current directory) and `..’ (previous directory).
Combine ‘find’ and ‘grep’
You can also use a combination of two commands in Linux – find
and grep
commands to recursively search subdirectories for files that match a grep
pattern (provided with the argument):
find . -type f -exec grep -l 'directory_name' {} \;
This command makes the task very simple. It searches all files in all subdirectories of the current directory’, and print the filenames. It is a great set of commands to recursively searching files in all subdirectories.
Other Commands to Find Files Recursively
There are many other commands to find files recursively. Linux Ubuntu users can use any one of the following commands:
- ls -R : Use the ls command to get recursive directory listing on Linux systems
- find /dir/ -print : Use the find command to see recursive directory listing in Unix systems
- du -a . : Use the du command to view recursive directory listing on Unix systems