How to Search for Files Recursively into Subdirectories












97















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?










share|improve this question


















  • 1





    You can do ls -R | grep .xml

    – Nik-Lz
    Apr 16 '17 at 18:03
















97















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?










share|improve this question


















  • 1





    You can do ls -R | grep .xml

    – Nik-Lz
    Apr 16 '17 at 18:03














97












97








97


38






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?










share|improve this question














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?







directory ls






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 13 '13 at 20:06









Shamim HafizShamim Hafiz

70421017




70421017








  • 1





    You can do ls -R | grep .xml

    – Nik-Lz
    Apr 16 '17 at 18:03














  • 1





    You can do ls -R | grep .xml

    – Nik-Lz
    Apr 16 '17 at 18:03








1




1





You can do ls -R | grep .xml

– Nik-Lz
Apr 16 '17 at 18:03





You can do ls -R | grep .xml

– Nik-Lz
Apr 16 '17 at 18:03










4 Answers
4






active

oldest

votes


















73














Try using Find



sudo find . -print | grep -i '.*[.]xml'





share|improve this answer





















  • 3





    is the sudo must, or it's there to ensure super user privileges?

    – Shamim Hafiz
    Jun 13 '13 at 20:36






  • 3





    I let you decide. Sudo, No Sudo.

    – Mitch
    Jun 13 '13 at 20:44






  • 4





    Just out of interest. What is the advantage of find over ls -R?

    – don.joey
    May 22 '14 at 9:21






  • 1





    @don.joey This might help stackoverflow.com/questions/13830036/…

    – Mitch
    May 22 '14 at 10:08






  • 9





    -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.

    – muru
    Apr 3 '15 at 6:54





















130














You can do it with find only:



find . -name '*.xml'


. is the current directory. If you need to search in another directory, replace . with the directory path.






share|improve this answer





















  • 2





    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.

    – Mostafiz Rahman
    Sep 30 '14 at 20:24






  • 1





    @mostafiz, you need to quote the '*.xml' part. I'll edit my answer.

    – KaeruCT
    Oct 1 '14 at 14:39






  • 1





    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 Rahman
    Oct 1 '14 at 16:52






  • 2





    @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.

    – KaeruCT
    Oct 1 '14 at 19:38













  • All right! May be I'd made some mistake. Now it is working perfectly!

    – Mostafiz Rahman
    Oct 2 '14 at 14:02



















12














Try this command:



ls -R | grep '.*[.]xml'


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.






share|improve this answer



















  • 4





    Anyway to get this to show the directory it came from?

    – AdamO
    Jul 28 '15 at 20:55






  • 1





    Mandatory link: Why not parse ls?

    – Ruslan
    Apr 24 '17 at 11:02



















2














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 "%sn" "$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.






share|improve this answer
























    protected by Community May 6 '16 at 13:10



    Thank you for your interest in this question.
    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



    Would you like to answer one of these unanswered questions instead?














    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    73














    Try using Find



    sudo find . -print | grep -i '.*[.]xml'





    share|improve this answer





















    • 3





      is the sudo must, or it's there to ensure super user privileges?

      – Shamim Hafiz
      Jun 13 '13 at 20:36






    • 3





      I let you decide. Sudo, No Sudo.

      – Mitch
      Jun 13 '13 at 20:44






    • 4





      Just out of interest. What is the advantage of find over ls -R?

      – don.joey
      May 22 '14 at 9:21






    • 1





      @don.joey This might help stackoverflow.com/questions/13830036/…

      – Mitch
      May 22 '14 at 10:08






    • 9





      -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.

      – muru
      Apr 3 '15 at 6:54


















    73














    Try using Find



    sudo find . -print | grep -i '.*[.]xml'





    share|improve this answer





















    • 3





      is the sudo must, or it's there to ensure super user privileges?

      – Shamim Hafiz
      Jun 13 '13 at 20:36






    • 3





      I let you decide. Sudo, No Sudo.

      – Mitch
      Jun 13 '13 at 20:44






    • 4





      Just out of interest. What is the advantage of find over ls -R?

      – don.joey
      May 22 '14 at 9:21






    • 1





      @don.joey This might help stackoverflow.com/questions/13830036/…

      – Mitch
      May 22 '14 at 10:08






    • 9





      -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.

      – muru
      Apr 3 '15 at 6:54
















    73












    73








    73







    Try using Find



    sudo find . -print | grep -i '.*[.]xml'





    share|improve this answer















    Try using Find



    sudo find . -print | grep -i '.*[.]xml'






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jun 15 '15 at 19:42









    DavidM

    32




    32










    answered Jun 13 '13 at 20:16









    MitchMitch

    83.8k14173228




    83.8k14173228








    • 3





      is the sudo must, or it's there to ensure super user privileges?

      – Shamim Hafiz
      Jun 13 '13 at 20:36






    • 3





      I let you decide. Sudo, No Sudo.

      – Mitch
      Jun 13 '13 at 20:44






    • 4





      Just out of interest. What is the advantage of find over ls -R?

      – don.joey
      May 22 '14 at 9:21






    • 1





      @don.joey This might help stackoverflow.com/questions/13830036/…

      – Mitch
      May 22 '14 at 10:08






    • 9





      -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.

      – muru
      Apr 3 '15 at 6:54
















    • 3





      is the sudo must, or it's there to ensure super user privileges?

      – Shamim Hafiz
      Jun 13 '13 at 20:36






    • 3





      I let you decide. Sudo, No Sudo.

      – Mitch
      Jun 13 '13 at 20:44






    • 4





      Just out of interest. What is the advantage of find over ls -R?

      – don.joey
      May 22 '14 at 9:21






    • 1





      @don.joey This might help stackoverflow.com/questions/13830036/…

      – Mitch
      May 22 '14 at 10:08






    • 9





      -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.

      – muru
      Apr 3 '15 at 6:54










    3




    3





    is the sudo must, or it's there to ensure super user privileges?

    – Shamim Hafiz
    Jun 13 '13 at 20:36





    is the sudo must, or it's there to ensure super user privileges?

    – Shamim Hafiz
    Jun 13 '13 at 20:36




    3




    3





    I let you decide. Sudo, No Sudo.

    – Mitch
    Jun 13 '13 at 20:44





    I let you decide. Sudo, No Sudo.

    – Mitch
    Jun 13 '13 at 20:44




    4




    4





    Just out of interest. What is the advantage of find over ls -R?

    – don.joey
    May 22 '14 at 9:21





    Just out of interest. What is the advantage of find over ls -R?

    – don.joey
    May 22 '14 at 9:21




    1




    1





    @don.joey This might help stackoverflow.com/questions/13830036/…

    – Mitch
    May 22 '14 at 10:08





    @don.joey This might help stackoverflow.com/questions/13830036/…

    – Mitch
    May 22 '14 at 10:08




    9




    9





    -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.

    – muru
    Apr 3 '15 at 6:54







    -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.

    – muru
    Apr 3 '15 at 6:54















    130














    You can do it with find only:



    find . -name '*.xml'


    . is the current directory. If you need to search in another directory, replace . with the directory path.






    share|improve this answer





















    • 2





      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.

      – Mostafiz Rahman
      Sep 30 '14 at 20:24






    • 1





      @mostafiz, you need to quote the '*.xml' part. I'll edit my answer.

      – KaeruCT
      Oct 1 '14 at 14:39






    • 1





      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 Rahman
      Oct 1 '14 at 16:52






    • 2





      @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.

      – KaeruCT
      Oct 1 '14 at 19:38













    • All right! May be I'd made some mistake. Now it is working perfectly!

      – Mostafiz Rahman
      Oct 2 '14 at 14:02
















    130














    You can do it with find only:



    find . -name '*.xml'


    . is the current directory. If you need to search in another directory, replace . with the directory path.






    share|improve this answer





















    • 2





      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.

      – Mostafiz Rahman
      Sep 30 '14 at 20:24






    • 1





      @mostafiz, you need to quote the '*.xml' part. I'll edit my answer.

      – KaeruCT
      Oct 1 '14 at 14:39






    • 1





      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 Rahman
      Oct 1 '14 at 16:52






    • 2





      @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.

      – KaeruCT
      Oct 1 '14 at 19:38













    • All right! May be I'd made some mistake. Now it is working perfectly!

      – Mostafiz Rahman
      Oct 2 '14 at 14:02














    130












    130








    130







    You can do it with find only:



    find . -name '*.xml'


    . is the current directory. If you need to search in another directory, replace . with the directory path.






    share|improve this answer















    You can do it with find only:



    find . -name '*.xml'


    . is the current directory. If you need to search in another directory, replace . with the directory path.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 19 '17 at 22:03









    George Birbilis

    1034




    1034










    answered Jun 13 '13 at 20:22









    KaeruCTKaeruCT

    1,401166




    1,401166








    • 2





      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.

      – Mostafiz Rahman
      Sep 30 '14 at 20:24






    • 1





      @mostafiz, you need to quote the '*.xml' part. I'll edit my answer.

      – KaeruCT
      Oct 1 '14 at 14:39






    • 1





      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 Rahman
      Oct 1 '14 at 16:52






    • 2





      @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.

      – KaeruCT
      Oct 1 '14 at 19:38













    • All right! May be I'd made some mistake. Now it is working perfectly!

      – Mostafiz Rahman
      Oct 2 '14 at 14:02














    • 2





      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.

      – Mostafiz Rahman
      Sep 30 '14 at 20:24






    • 1





      @mostafiz, you need to quote the '*.xml' part. I'll edit my answer.

      – KaeruCT
      Oct 1 '14 at 14:39






    • 1





      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 Rahman
      Oct 1 '14 at 16:52






    • 2





      @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.

      – KaeruCT
      Oct 1 '14 at 19:38













    • All right! May be I'd made some mistake. Now it is working perfectly!

      – Mostafiz Rahman
      Oct 2 '14 at 14:02








    2




    2





    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.

    – Mostafiz Rahman
    Sep 30 '14 at 20:24





    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.

    – Mostafiz Rahman
    Sep 30 '14 at 20:24




    1




    1





    @mostafiz, you need to quote the '*.xml' part. I'll edit my answer.

    – KaeruCT
    Oct 1 '14 at 14:39





    @mostafiz, you need to quote the '*.xml' part. I'll edit my answer.

    – KaeruCT
    Oct 1 '14 at 14:39




    1




    1





    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 Rahman
    Oct 1 '14 at 16:52





    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 Rahman
    Oct 1 '14 at 16:52




    2




    2





    @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.

    – KaeruCT
    Oct 1 '14 at 19:38







    @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.

    – KaeruCT
    Oct 1 '14 at 19:38















    All right! May be I'd made some mistake. Now it is working perfectly!

    – Mostafiz Rahman
    Oct 2 '14 at 14:02





    All right! May be I'd made some mistake. Now it is working perfectly!

    – Mostafiz Rahman
    Oct 2 '14 at 14:02











    12














    Try this command:



    ls -R | grep '.*[.]xml'


    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.






    share|improve this answer



















    • 4





      Anyway to get this to show the directory it came from?

      – AdamO
      Jul 28 '15 at 20:55






    • 1





      Mandatory link: Why not parse ls?

      – Ruslan
      Apr 24 '17 at 11:02
















    12














    Try this command:



    ls -R | grep '.*[.]xml'


    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.






    share|improve this answer



















    • 4





      Anyway to get this to show the directory it came from?

      – AdamO
      Jul 28 '15 at 20:55






    • 1





      Mandatory link: Why not parse ls?

      – Ruslan
      Apr 24 '17 at 11:02














    12












    12








    12







    Try this command:



    ls -R | grep '.*[.]xml'


    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.






    share|improve this answer













    Try this command:



    ls -R | grep '.*[.]xml'


    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jun 13 '13 at 20:10









    Rohit JainRohit Jain

    5872615




    5872615








    • 4





      Anyway to get this to show the directory it came from?

      – AdamO
      Jul 28 '15 at 20:55






    • 1





      Mandatory link: Why not parse ls?

      – Ruslan
      Apr 24 '17 at 11:02














    • 4





      Anyway to get this to show the directory it came from?

      – AdamO
      Jul 28 '15 at 20:55






    • 1





      Mandatory link: Why not parse ls?

      – Ruslan
      Apr 24 '17 at 11:02








    4




    4





    Anyway to get this to show the directory it came from?

    – AdamO
    Jul 28 '15 at 20:55





    Anyway to get this to show the directory it came from?

    – AdamO
    Jul 28 '15 at 20:55




    1




    1





    Mandatory link: Why not parse ls?

    – Ruslan
    Apr 24 '17 at 11:02





    Mandatory link: Why not parse ls?

    – Ruslan
    Apr 24 '17 at 11:02











    2














    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 "%sn" "$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.






    share|improve this answer






























      2














      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 "%sn" "$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.






      share|improve this answer




























        2












        2








        2







        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 "%sn" "$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.






        share|improve this answer















        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 "%sn" "$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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 30 '18 at 11:32

























        answered Jul 13 '17 at 2:43









        Sergiy KolodyazhnyySergiy Kolodyazhnyy

        70.6k9147310




        70.6k9147310

















            protected by Community May 6 '16 at 13:10



            Thank you for your interest in this question.
            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



            Would you like to answer one of these unanswered questions instead?



            Popular posts from this blog

            flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

            Mangá

             ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕