Find all files in the current directory and its subdirectories with the extension .png [duplicate]












0
















This question already has an answer here:




  • How do I locate all files that include specific word as part of name and end with specific extension?

    3 answers




In a directory and its subfolders I need to see all the files with the png extension.



For this, I used the command ls -R *.png



I get an error saying that the directory *.png doesn't exist. I am surprised that my regular expression is not recognized.



ls: Cannot read '*.png': the file or the directory doesn't exist









share|improve this question















marked as duplicate by karel, Charles Green, Thomas, Eric Carvalho, guiverc Jan 28 at 1:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    ... to find all the files with extension .png in the current directory and all its subdirectories, use find . -name '*.png' rather than trying to filter the output of ls -R

    – steeldriver
    Jan 18 at 13:25











  • @steeldriver Tranks you, this command run well.

    – Mr Brown
    Jan 18 at 13:35


















0
















This question already has an answer here:




  • How do I locate all files that include specific word as part of name and end with specific extension?

    3 answers




In a directory and its subfolders I need to see all the files with the png extension.



For this, I used the command ls -R *.png



I get an error saying that the directory *.png doesn't exist. I am surprised that my regular expression is not recognized.



ls: Cannot read '*.png': the file or the directory doesn't exist









share|improve this question















marked as duplicate by karel, Charles Green, Thomas, Eric Carvalho, guiverc Jan 28 at 1:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    ... to find all the files with extension .png in the current directory and all its subdirectories, use find . -name '*.png' rather than trying to filter the output of ls -R

    – steeldriver
    Jan 18 at 13:25











  • @steeldriver Tranks you, this command run well.

    – Mr Brown
    Jan 18 at 13:35
















0












0








0









This question already has an answer here:




  • How do I locate all files that include specific word as part of name and end with specific extension?

    3 answers




In a directory and its subfolders I need to see all the files with the png extension.



For this, I used the command ls -R *.png



I get an error saying that the directory *.png doesn't exist. I am surprised that my regular expression is not recognized.



ls: Cannot read '*.png': the file or the directory doesn't exist









share|improve this question

















This question already has an answer here:




  • How do I locate all files that include specific word as part of name and end with specific extension?

    3 answers




In a directory and its subfolders I need to see all the files with the png extension.



For this, I used the command ls -R *.png



I get an error saying that the directory *.png doesn't exist. I am surprised that my regular expression is not recognized.



ls: Cannot read '*.png': the file or the directory doesn't exist




This question already has an answer here:




  • How do I locate all files that include specific word as part of name and end with specific extension?

    3 answers








command-line ls






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 18 at 19:18









Zanna

50.9k13137241




50.9k13137241










asked Jan 18 at 12:39









Mr BrownMr Brown

162




162




marked as duplicate by karel, Charles Green, Thomas, Eric Carvalho, guiverc Jan 28 at 1:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by karel, Charles Green, Thomas, Eric Carvalho, guiverc Jan 28 at 1:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2





    ... to find all the files with extension .png in the current directory and all its subdirectories, use find . -name '*.png' rather than trying to filter the output of ls -R

    – steeldriver
    Jan 18 at 13:25











  • @steeldriver Tranks you, this command run well.

    – Mr Brown
    Jan 18 at 13:35
















  • 2





    ... to find all the files with extension .png in the current directory and all its subdirectories, use find . -name '*.png' rather than trying to filter the output of ls -R

    – steeldriver
    Jan 18 at 13:25











  • @steeldriver Tranks you, this command run well.

    – Mr Brown
    Jan 18 at 13:35










2




2





... to find all the files with extension .png in the current directory and all its subdirectories, use find . -name '*.png' rather than trying to filter the output of ls -R

– steeldriver
Jan 18 at 13:25





... to find all the files with extension .png in the current directory and all its subdirectories, use find . -name '*.png' rather than trying to filter the output of ls -R

– steeldriver
Jan 18 at 13:25













@steeldriver Tranks you, this command run well.

– Mr Brown
Jan 18 at 13:35







@steeldriver Tranks you, this command run well.

– Mr Brown
Jan 18 at 13:35












3 Answers
3






active

oldest

votes


















-1














The correct use is



 ls -R | grep '.png$'


This command only works with normal file names with no space, new line or special characters. Use find as suggested by danzel or globstar as suggested by DoVo.






share|improve this answer


























  • @danzel thanks. Corrected.

    – VeeJay
    Jan 18 at 13:21






  • 3





    grep .png$ may appear to work, but will match any character before png (not just dot)

    – steeldriver
    Jan 18 at 13:28






  • 3





    Do not parse ls.

    – RoVo
    Jan 18 at 14:22













  • @steeldriver thanks.

    – VeeJay
    Jan 18 at 19:20











  • Agreed @RoVo thanks. Edited.

    – VeeJay
    Jan 18 at 20:11





















5














TL;DR



To find files matching a regular expression, use find with the -regex option:



find [startingPath] -type [fileType] -regex "[regularExpression]"


In your case, if you want to search for files (file type f) ending in .png, starting from the current directory:



find . -type f -regex ".*.pdf"


If you want to have an ls-like output, use the -ls action:



find . -type f -regex ".*.pdf" -ls


(the output has the same format as ls -dils).



If you want to execute a command for each file, use the -exec action, e.g.:



find . -type f -regex ".*.pdf" -exec file {} ;


... will print file type information for each matching file.



There are a lot more things you can do with find, just read the manual.





As @steeldriver said in the comment, there is no regular expression in your command. *.png is a shell glob and is expanded before ls is run. Imagine there are two files in the current directory:



picture1.png
picture2.png


...then ls -R *.png will be expanded to:



ls -R picture1.png picture2.png


In this case, the -R option is not particularly useful because there are no directories specified that ls could recurse into.



If the shell doesn't find any matching name, it passes the argument literally (depends on the shell, but bash does):



ls -R *.png


... and ls complains because there is no file called *.png.






share|improve this answer





















  • 1





    +1 as @steeldriver said. But please note that * and . have different meaning in -regex ".*.pdf" and -name "*.png".

    – PerlDuck
    Jan 18 at 14:00






  • 1





    @PerlDuck good point. -name takes a shell pattern (which is matched using the fnmatch function). Since the OP explicitly asked about regular expressions, I concentrated on that.

    – danzel
    Jan 18 at 14:37



















1














Another option instead of find would be the use of globstar:



shopt -s globstar
ls **/*.png


Optionally unset globstar afterwards:



shopt -u globstar




From bash manpage:



globstar
If set, the pattern ** used in a pathname expansion context will
match all files and zero or more directories and subdirectories.
If the pattern is followed by a /, only directories and
subdirectories match.





share|improve this answer






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    -1














    The correct use is



     ls -R | grep '.png$'


    This command only works with normal file names with no space, new line or special characters. Use find as suggested by danzel or globstar as suggested by DoVo.






    share|improve this answer


























    • @danzel thanks. Corrected.

      – VeeJay
      Jan 18 at 13:21






    • 3





      grep .png$ may appear to work, but will match any character before png (not just dot)

      – steeldriver
      Jan 18 at 13:28






    • 3





      Do not parse ls.

      – RoVo
      Jan 18 at 14:22













    • @steeldriver thanks.

      – VeeJay
      Jan 18 at 19:20











    • Agreed @RoVo thanks. Edited.

      – VeeJay
      Jan 18 at 20:11


















    -1














    The correct use is



     ls -R | grep '.png$'


    This command only works with normal file names with no space, new line or special characters. Use find as suggested by danzel or globstar as suggested by DoVo.






    share|improve this answer


























    • @danzel thanks. Corrected.

      – VeeJay
      Jan 18 at 13:21






    • 3





      grep .png$ may appear to work, but will match any character before png (not just dot)

      – steeldriver
      Jan 18 at 13:28






    • 3





      Do not parse ls.

      – RoVo
      Jan 18 at 14:22













    • @steeldriver thanks.

      – VeeJay
      Jan 18 at 19:20











    • Agreed @RoVo thanks. Edited.

      – VeeJay
      Jan 18 at 20:11
















    -1












    -1








    -1







    The correct use is



     ls -R | grep '.png$'


    This command only works with normal file names with no space, new line or special characters. Use find as suggested by danzel or globstar as suggested by DoVo.






    share|improve this answer















    The correct use is



     ls -R | grep '.png$'


    This command only works with normal file names with no space, new line or special characters. Use find as suggested by danzel or globstar as suggested by DoVo.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 18 at 20:09

























    answered Jan 18 at 12:50









    VeeJayVeeJay

    1,6871720




    1,6871720













    • @danzel thanks. Corrected.

      – VeeJay
      Jan 18 at 13:21






    • 3





      grep .png$ may appear to work, but will match any character before png (not just dot)

      – steeldriver
      Jan 18 at 13:28






    • 3





      Do not parse ls.

      – RoVo
      Jan 18 at 14:22













    • @steeldriver thanks.

      – VeeJay
      Jan 18 at 19:20











    • Agreed @RoVo thanks. Edited.

      – VeeJay
      Jan 18 at 20:11





















    • @danzel thanks. Corrected.

      – VeeJay
      Jan 18 at 13:21






    • 3





      grep .png$ may appear to work, but will match any character before png (not just dot)

      – steeldriver
      Jan 18 at 13:28






    • 3





      Do not parse ls.

      – RoVo
      Jan 18 at 14:22













    • @steeldriver thanks.

      – VeeJay
      Jan 18 at 19:20











    • Agreed @RoVo thanks. Edited.

      – VeeJay
      Jan 18 at 20:11



















    @danzel thanks. Corrected.

    – VeeJay
    Jan 18 at 13:21





    @danzel thanks. Corrected.

    – VeeJay
    Jan 18 at 13:21




    3




    3





    grep .png$ may appear to work, but will match any character before png (not just dot)

    – steeldriver
    Jan 18 at 13:28





    grep .png$ may appear to work, but will match any character before png (not just dot)

    – steeldriver
    Jan 18 at 13:28




    3




    3





    Do not parse ls.

    – RoVo
    Jan 18 at 14:22







    Do not parse ls.

    – RoVo
    Jan 18 at 14:22















    @steeldriver thanks.

    – VeeJay
    Jan 18 at 19:20





    @steeldriver thanks.

    – VeeJay
    Jan 18 at 19:20













    Agreed @RoVo thanks. Edited.

    – VeeJay
    Jan 18 at 20:11







    Agreed @RoVo thanks. Edited.

    – VeeJay
    Jan 18 at 20:11















    5














    TL;DR



    To find files matching a regular expression, use find with the -regex option:



    find [startingPath] -type [fileType] -regex "[regularExpression]"


    In your case, if you want to search for files (file type f) ending in .png, starting from the current directory:



    find . -type f -regex ".*.pdf"


    If you want to have an ls-like output, use the -ls action:



    find . -type f -regex ".*.pdf" -ls


    (the output has the same format as ls -dils).



    If you want to execute a command for each file, use the -exec action, e.g.:



    find . -type f -regex ".*.pdf" -exec file {} ;


    ... will print file type information for each matching file.



    There are a lot more things you can do with find, just read the manual.





    As @steeldriver said in the comment, there is no regular expression in your command. *.png is a shell glob and is expanded before ls is run. Imagine there are two files in the current directory:



    picture1.png
    picture2.png


    ...then ls -R *.png will be expanded to:



    ls -R picture1.png picture2.png


    In this case, the -R option is not particularly useful because there are no directories specified that ls could recurse into.



    If the shell doesn't find any matching name, it passes the argument literally (depends on the shell, but bash does):



    ls -R *.png


    ... and ls complains because there is no file called *.png.






    share|improve this answer





















    • 1





      +1 as @steeldriver said. But please note that * and . have different meaning in -regex ".*.pdf" and -name "*.png".

      – PerlDuck
      Jan 18 at 14:00






    • 1





      @PerlDuck good point. -name takes a shell pattern (which is matched using the fnmatch function). Since the OP explicitly asked about regular expressions, I concentrated on that.

      – danzel
      Jan 18 at 14:37
















    5














    TL;DR



    To find files matching a regular expression, use find with the -regex option:



    find [startingPath] -type [fileType] -regex "[regularExpression]"


    In your case, if you want to search for files (file type f) ending in .png, starting from the current directory:



    find . -type f -regex ".*.pdf"


    If you want to have an ls-like output, use the -ls action:



    find . -type f -regex ".*.pdf" -ls


    (the output has the same format as ls -dils).



    If you want to execute a command for each file, use the -exec action, e.g.:



    find . -type f -regex ".*.pdf" -exec file {} ;


    ... will print file type information for each matching file.



    There are a lot more things you can do with find, just read the manual.





    As @steeldriver said in the comment, there is no regular expression in your command. *.png is a shell glob and is expanded before ls is run. Imagine there are two files in the current directory:



    picture1.png
    picture2.png


    ...then ls -R *.png will be expanded to:



    ls -R picture1.png picture2.png


    In this case, the -R option is not particularly useful because there are no directories specified that ls could recurse into.



    If the shell doesn't find any matching name, it passes the argument literally (depends on the shell, but bash does):



    ls -R *.png


    ... and ls complains because there is no file called *.png.






    share|improve this answer





















    • 1





      +1 as @steeldriver said. But please note that * and . have different meaning in -regex ".*.pdf" and -name "*.png".

      – PerlDuck
      Jan 18 at 14:00






    • 1





      @PerlDuck good point. -name takes a shell pattern (which is matched using the fnmatch function). Since the OP explicitly asked about regular expressions, I concentrated on that.

      – danzel
      Jan 18 at 14:37














    5












    5








    5







    TL;DR



    To find files matching a regular expression, use find with the -regex option:



    find [startingPath] -type [fileType] -regex "[regularExpression]"


    In your case, if you want to search for files (file type f) ending in .png, starting from the current directory:



    find . -type f -regex ".*.pdf"


    If you want to have an ls-like output, use the -ls action:



    find . -type f -regex ".*.pdf" -ls


    (the output has the same format as ls -dils).



    If you want to execute a command for each file, use the -exec action, e.g.:



    find . -type f -regex ".*.pdf" -exec file {} ;


    ... will print file type information for each matching file.



    There are a lot more things you can do with find, just read the manual.





    As @steeldriver said in the comment, there is no regular expression in your command. *.png is a shell glob and is expanded before ls is run. Imagine there are two files in the current directory:



    picture1.png
    picture2.png


    ...then ls -R *.png will be expanded to:



    ls -R picture1.png picture2.png


    In this case, the -R option is not particularly useful because there are no directories specified that ls could recurse into.



    If the shell doesn't find any matching name, it passes the argument literally (depends on the shell, but bash does):



    ls -R *.png


    ... and ls complains because there is no file called *.png.






    share|improve this answer















    TL;DR



    To find files matching a regular expression, use find with the -regex option:



    find [startingPath] -type [fileType] -regex "[regularExpression]"


    In your case, if you want to search for files (file type f) ending in .png, starting from the current directory:



    find . -type f -regex ".*.pdf"


    If you want to have an ls-like output, use the -ls action:



    find . -type f -regex ".*.pdf" -ls


    (the output has the same format as ls -dils).



    If you want to execute a command for each file, use the -exec action, e.g.:



    find . -type f -regex ".*.pdf" -exec file {} ;


    ... will print file type information for each matching file.



    There are a lot more things you can do with find, just read the manual.





    As @steeldriver said in the comment, there is no regular expression in your command. *.png is a shell glob and is expanded before ls is run. Imagine there are two files in the current directory:



    picture1.png
    picture2.png


    ...then ls -R *.png will be expanded to:



    ls -R picture1.png picture2.png


    In this case, the -R option is not particularly useful because there are no directories specified that ls could recurse into.



    If the shell doesn't find any matching name, it passes the argument literally (depends on the shell, but bash does):



    ls -R *.png


    ... and ls complains because there is no file called *.png.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 18 at 13:55

























    answered Jan 18 at 13:43









    danzeldanzel

    2,087714




    2,087714








    • 1





      +1 as @steeldriver said. But please note that * and . have different meaning in -regex ".*.pdf" and -name "*.png".

      – PerlDuck
      Jan 18 at 14:00






    • 1





      @PerlDuck good point. -name takes a shell pattern (which is matched using the fnmatch function). Since the OP explicitly asked about regular expressions, I concentrated on that.

      – danzel
      Jan 18 at 14:37














    • 1





      +1 as @steeldriver said. But please note that * and . have different meaning in -regex ".*.pdf" and -name "*.png".

      – PerlDuck
      Jan 18 at 14:00






    • 1





      @PerlDuck good point. -name takes a shell pattern (which is matched using the fnmatch function). Since the OP explicitly asked about regular expressions, I concentrated on that.

      – danzel
      Jan 18 at 14:37








    1




    1





    +1 as @steeldriver said. But please note that * and . have different meaning in -regex ".*.pdf" and -name "*.png".

    – PerlDuck
    Jan 18 at 14:00





    +1 as @steeldriver said. But please note that * and . have different meaning in -regex ".*.pdf" and -name "*.png".

    – PerlDuck
    Jan 18 at 14:00




    1




    1





    @PerlDuck good point. -name takes a shell pattern (which is matched using the fnmatch function). Since the OP explicitly asked about regular expressions, I concentrated on that.

    – danzel
    Jan 18 at 14:37





    @PerlDuck good point. -name takes a shell pattern (which is matched using the fnmatch function). Since the OP explicitly asked about regular expressions, I concentrated on that.

    – danzel
    Jan 18 at 14:37











    1














    Another option instead of find would be the use of globstar:



    shopt -s globstar
    ls **/*.png


    Optionally unset globstar afterwards:



    shopt -u globstar




    From bash manpage:



    globstar
    If set, the pattern ** used in a pathname expansion context will
    match all files and zero or more directories and subdirectories.
    If the pattern is followed by a /, only directories and
    subdirectories match.





    share|improve this answer




























      1














      Another option instead of find would be the use of globstar:



      shopt -s globstar
      ls **/*.png


      Optionally unset globstar afterwards:



      shopt -u globstar




      From bash manpage:



      globstar
      If set, the pattern ** used in a pathname expansion context will
      match all files and zero or more directories and subdirectories.
      If the pattern is followed by a /, only directories and
      subdirectories match.





      share|improve this answer


























        1












        1








        1







        Another option instead of find would be the use of globstar:



        shopt -s globstar
        ls **/*.png


        Optionally unset globstar afterwards:



        shopt -u globstar




        From bash manpage:



        globstar
        If set, the pattern ** used in a pathname expansion context will
        match all files and zero or more directories and subdirectories.
        If the pattern is followed by a /, only directories and
        subdirectories match.





        share|improve this answer













        Another option instead of find would be the use of globstar:



        shopt -s globstar
        ls **/*.png


        Optionally unset globstar afterwards:



        shopt -u globstar




        From bash manpage:



        globstar
        If set, the pattern ** used in a pathname expansion context will
        match all files and zero or more directories and subdirectories.
        If the pattern is followed by a /, only directories and
        subdirectories match.






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 18 at 14:24









        RoVoRoVo

        7,7341843




        7,7341843















            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents