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?
You can do it with find only:
.
is the current directory. If you need to search in another directory, replace.
with the directory path.Try using Find
Try this command:
ls
doesn't have options to filter the output. For that you would need to use pipe. This passes the output fromls
togrep
, which then filters them to show just the.xml
files.bash
Using
globstar
shell option, we can make use of recursive globbing./**/*
Perl
Perl has a module
Find
, which allows for recursive directory tree traversal. Within the specialfind()
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:Python
While Perl has a whole module dedicated to recursive tree traversal, Python has a neat function
walk()
that is part ofos
module, and repeatedly returns tuple of topmost path, list of all subdirectories, and list of filenames. We can do the following:This might be far neater as a script:
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 thatfind
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 <command> {} \;
to execute a particular command to process the file with passing file as argument ( where{}
is standardfind
placeholder for current file) , and many others so please read the manpage forfind
.Inside the Gnome Filemanager you can click on the magnifying-glass icon (in the top-right usually) and then start typing to search in the current folder.
For some people (me) this is much easier that using the command-line.