- How to Search for Files Recursively into Subdirectories
- 5 Answers 5
- bash
- Perl
- Python
- find
- How to Search and Find Files Recursively in Linux
- Finding Files Recursively in Linux
- Conclusion
- About the author
- David Adams
- find file linux recursive
- How do I find a file in Linux terminal recursively?
- How do I find files recursively?
- How do I find a file recursively in Unix?
- What is a recursive file search?
- How do I find a file in Linux terminal?
- How do I use grep to find a file in Linux?
- Which command will find all the files without permission 777?
- How do I find a file without knowing the path in Unix?
- Is grep recursive by default?
- How do I see all files in Linux?
- How do I grep all files in a directory?
- How do I find subfolders in Linux?
How to Search for Files Recursively into Subdirectories
I am trying to look for all XML files in a particular directory and all sub-directories (recursively) inside it. ls -R *.xml is only listing files in the current directory. I am quite sure, the sub-folders themselves have several .xml files, but none are showing up. Is this a configuration issue?
5 Answers 5
. is the current directory. If you need to search in another directory, replace . with the directory path.
Does it search for the required file recursively in the directory rooted at current directory. In my case it just checked in the current directory only, didn’t check the subdirectory.
Actually I searched for .php files in current directory. But it returned only .php files in current directory, didn’t searched recursively in sub-directories. That’s why I’m asking whether find command searches recursively or not.
@mostafiz, the find command searches recursively. If you don’t quote the parameter, I think your shell might do an expansion on the * , so it will match the files in the current directory.
sudo find . -print | grep -i '.*[.]xml'
-1 for mixing find and grep , when find can do filtering using both regexes and globs, and not using find ‘s -print0 and grep’s -z when you do need to mix.
ls doesn’t have options to filter the output. For that you would need to use pipe. This passes the output from ls to grep , which then filters them to show just the .xml files.
bash
Using globstar shell option, we can make use of recursive globbing ./**/*
bash-4.3$ shopt -s globstar bash-4.3$ for i in ./**/*.xml; do printf "%s\n" "$i" ; done ./adwaita-timed.xml ./bin/hw5/stuff/book/chapter42servletexample/build/web/META-INF/context.xml ./bin/hw5/stuff/book/chapter42servletexample/build/web/WEB-INF/beans.xml ./bin/hw5/stuff/book/chapter42servletexample/build/web/WEB-INF/web.xml
Perl
Perl has a module Find , which allows for recursive directory tree traversal. Within the special find() function, we can define a wanted subroutine and the directory that we want to traverse, in this example that’s . . The one-liner in such case would be:
bash-4.3$ perl -le 'use File::Find; find(sub,".")' ./adwaita-timed.xml ./CLEAR_DESKTOP/blahblah/hw5/stuff/book/jsf2demo/build/web/WEB-INF/beans.xml ./CLEAR_DESKTOP/blahblah/hw5/stuff/book/jsf2demo/build/web/WEB-INF/web.xml ./CLEAR_DESKTOP/blahblah/hw5/stuff/book/liangweb/build.xml
Python
While Perl has a whole module dedicated to recursive tree traversal, Python has a neat function walk() that is part of os module, and repeatedly returns tuple of topmost path, list of all subdirectories, and list of filenames. We can do the following:
bash-4.3$ python -c 'import os,sys; [ sys.stdout.write(os.path.join(r,i)+"\n") for r,s,f in os.walk(".") for i in f if i.endswith(".xml") ]' ./adwaita-timed.xml ./CLEAR_DESKTOP/blahblah/hw5/stuff/book/jsf2demo/build/web/WEB-INF/beans.xml ./CLEAR_DESKTOP/blahblah/hw5/stuff/book/jsf2demo/build/web/WEB-INF/web.xml ./CLEAR_DESKTOP/blahblah/hw5/stuff/book/liangweb/build.xml
This might be far neater as a script:
#!/usr/bin/env python import os,sys for r,s,f in os.walk("."): for i in f: if i.endswith(".xml") print(os.path.join(r,i))
find
Other answers have mentioned find for recursive traversal, and that’s the go-to tool for the job. What does need mention is the fact that find has multiple command line switches, such as -printf to print output in desired format, -type f to find only regular files, -inum to search by inode number, -mtime to search by modification date, -exec <> \; to execute a particular command to process the file with passing file as argument ( where <> is standard find placeholder for current file) , and many others so please read the manpage for find .
How to Search and Find Files Recursively in Linux
This brief tutorial explains how to search and find the files recursively in the Linux operating systems.
After reading this article, you will be able to find any file recursively using the different techniques including a single file search, multiple files search, find files by permissions, and more. This document is optimized for both new and experienced Linux users. All methods are valid for every Linux distribution.
All examples in this tutorial contain screenshots to make it easy for any Linux user to understand and reproduce them.
Finding Files Recursively in Linux
The find command does not need flags to search the files recursively in the current directory. You only need to define the main directory and the file name using the –name option. This command will search the file within the main directory and all subdirectories.
The syntax is simple, as shown in the following:
If you want to find the 27.jpg file within your home directory and subdirectories, run the following command as shown in the following screenshot:
As you can see, the file was found in the /home/linuxhint/Downloads/recur subdirectory.
An alternative to the previous command is the tree command shown in the following example where you search the same file named 27.jpg within the current directory:
As you can see in the previous figure, the format is pretty different. It seems to be more user friendly or nicer as long as you don’t need to copy the full path to paste it.
The following screenshot shows how to use the find command to recursively search more than a file.
The syntax to search multiple files recursively is the following:
Note that there is a –o flag before the second file name. You can add more than one file by adding more –o –name flags. For example, to find 4 files with the same command, use the following syntax:
In the practical example described in the following image, use this command to find a file named 27.jpg and a file whose name begins with “DIAGRAM” but without specifying its extension. Instead, use a wildcard (*) to find any file named DIAGRAM independently of its type.
As you can see in the previous image, both files were found recursively.
The next example describes how to find the files by extension using the find command. In the following figure, you can see how to recursively find all the .jpg files using the wildcard again. The syntax is pretty simple:
Thus, to find all the .jpg files recursively, run the following command:
As shown in the previous image, all the jpg files including their path are listed successfully. You can replace the .jpg extension for any extension that you want to search like .png, .txt, .c and more.
Now, let’s assume that you don’t want to find a file but a directory recursively. All you need to do is to use the same command that was shown in the first example of this tutorial then add the -type d option. The syntax as follows:
In the following practical example, use the previous syntax to find the recur directory.
As you see in the previous figure, the directory named “recur” was found successfully.
You also can find the files by size using the following syntax where is the main directory containing the subdirectories and the is the size of the files that you can list with their full path.
The following example describes how to find the 10 MB size files. You can replace the M defining units in MB with c for bytes, w for two two byte words, k for kibytes and G for gibibytes (note units are case sensitive).
To find the 10 mebibytes files, execute the following command:
All 10M files were properly listed with their paths.
The syntax to find the files based on their permissions is shown in the following:
Let’s assume that you want to identify and list the files with read, write, and executing permissions (777). The command to run is the following:
The last example of this tutorial shows how to find and list the files and directories by size.
As shown, the files are listed by size with proper units. The 0 size directories and files are empty.
Conclusion
Linux versatility and flexibility allows to find the files (and other functions) recursively in many ways. They can easily be executed by all the Linux users independently of his knowledge level, from the new users to the system administrators. All techniques previously described are valid for all the Linux distributions and even to some Unix systems. According to their man pages, some flags may vary in some distributions, but most of them are universal. In case your Linux distribution does not match any of the previously explained commands, you can read the man page. It is highly recommended to the readers to practice the examples to incorporate this knowledge.
Thank you very much for reading this Linux tutorial. Keep following us for more Linux professional tips.
About the author
David Adams
David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.
find file linux recursive
How do I find a file in Linux terminal recursively?
find is a command for recursively filtering objects in the file system based on a simple conditional mechanism. Use find to search for a file or directory on your file system. Using the -exec flag, files can be found and immediately processed within the same command.
How do I find files recursively?
- Solution: find + grep. For years I always used variations of the following Linux find and grep commands to recursively search subdirectories for files that match a grep pattern: find . — .
- Solution: `grep -r` .
- Search multiple subdirectories. .
- Using egrep recursively. .
- Summary: `grep -r` notes.
How do I find a file recursively in Unix?
grep command: Recursively Search All Files For A String
To ignore case distinctions: grep -ri «word» . To display print only the filenames with GNU grep, enter: grep -r -l «foo» .
What is a recursive file search?
Alternatively referred to as recursive, recurse is a term used to describe the procedure capable of being repeated. For example, when listing files in a Windows command prompt, you can use the dir /s command to recursively list all files in the current directory and any subdirectories.
How do I find a file in Linux terminal?
- Open your favorite terminal app. .
- Type the following command: find /path/to/folder/ -iname *file_name_portion* .
- If you need to find only files or only folders, add the option -type f for files or -type d for directories.
How do I use grep to find a file in Linux?
The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we’re searching for and finally the name of the file (or files) we’re searching in. The output is the three lines in the file that contain the letters ‘not’.
Which command will find all the files without permission 777?
find /home/ -perm 777 -type f
This command will list all the files inside the home directory that has 777 permissions.
How do I find a file without knowing the path in Unix?
- -name file-name – Search for given file-name. .
- -iname file-name – Like -name, but the match is case insensitive. .
- -user userName – The file’s owner is userName.
Is grep recursive by default?
For instance, it’s recursive by default and automatically ignores files and directories listed in . gitignore , so you don’t have to keep passing the same cumbersome exclude options to grep or find. but grep -r is a better answer. Recurse in directories only searching file matching PATTERN .
How do I see all files in Linux?
- find . — name thisfile.txt. If you need to know how to find a file in Linux called thisfile. .
- find /home -name *.jpg. Look for all . jpg files in the /home and directories below it.
- find . — type f -empty. Look for an empty file inside the current directory.
- find /home -user randomperson-mtime 6 -iname «.db»
How do I grep all files in a directory?
To grep All Files in a Directory Recursively, we need to use -R option. When -R options is used, The Linux grep command will search given string in the specified directory and subdirectories inside that directory. If no folder name is given, grep command will search the string inside the current working directory.
How do I find subfolders in Linux?
3 Answers. Try find /dir -type d -name «your_dir_name» . Replace /dir with your directory name, and replace «your_dir_name» with the name you’re looking for. -type d will tell find to search for directories only.
Shell
What is a builtin bash?Is Echo a bash builtin?What commands are built into the bash shell?Is LS a shell builtin?What are bash commands?How do you do i.
Local
Turin’s local authorities have decided to switch to open source and entirely ditch all the Microsoft products, saving alot of money to the local gover.
Shutter
Shutter Screenshot Tool Installation Through GUI The Ubuntu Software utility will open, from where you can search for Shutter by clicking the search b.
Latest news, practical advice, detailed reviews and guides. We have everything about the Linux operating system