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{-f && $_ =~ /.xml$/ && print $File::Find::name},".")'
./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 <command> {} \;
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
.
ls -R | grep .xml