List files with certain extensions with ls and grep

后端 未结 12 617
悲&欢浪女
悲&欢浪女 2020-12-02 04:09

I just want to get the files from the current dir and only output .mp4 .mp3 .exe files nothing else. So I thought I could just do this:

ls | grep \\.mp4$ | g         


        
相关标签:
12条回答
  • 2020-12-02 04:52

    Why not:

    ls *.{mp3,exe,mp4}
    

    I'm not sure where I learned it - but I've been using this.

    0 讨论(0)
  • 2020-12-02 04:52

    ls -R | findstr ".mp3"

    ls -R => lists subdirectories recursively

    0 讨论(0)
  • 2020-12-02 04:54

    egrep -- extended grep -- will help here

    ls | egrep '\.mp4$|\.mp3$|\.exe$'
    

    should do the job.

    0 讨论(0)
  • 2020-12-02 04:57

    In case you are still looking for an alternate solution:

    ls | grep -i -e '\\.tcl$' -e '\\.exe$' -e '\\.mp4$'
    

    Feel free to add more -e flags if needed.

    0 讨论(0)
  • 2020-12-02 04:59

    the easiest way is to just use ls

    ls *.mp4 *.mp3 *.exe
    
    0 讨论(0)
  • 2020-12-02 05:00

    For OSX users:

    If you use ls *.{mp3,exe,mp4}, it will throw an error if one of those extensions has no results.

    Using ls *.(mp3|exe|mp4) will return all files matching those extensions, even if one of the extensions had 0 results.

    0 讨论(0)
提交回复
热议问题