List all the files in ending with several file extensions?

29,361

Solution 1

Use this: ls -l /users/jenna/bio/*.{fas,pep}

Or this: find /users/jenna/bio -iname '*.pep' -or -iname '*.fas'

Solution 2

The thing that's most likely to work out of the box is brace expansion. It works in bash, zsh and ksh.

ls -l /users/jenna/bio/*.{fas,pep}

(No spaces anywhere around the braces.)

In bash and ksh, this displays an error if one of the extensions isn't matched, but lists the files matching the other extension anyway. The reason for this is that brace expansion isn't a wildcard matching feature, but a textual replacement: *.{fas,pep} is converted to *.fas *.pep before the shell starts looking up files that match the patterns. In zsh, you'll get an error and the command won't run, but there's a better way: an “or” glob pattern. This one won't error out unless none of the extensions match.

ls -l /users/jenna/bio/*.(fas|pep)

Or patterns are also available with a different syntax: @(…|…); in bash, this syntax needs to be activated with shopt -s extglob, and in zsh, this syntax needs to be activated with setopt ksh_glob.

ls -l /users/jenna/bio/*.@(fas|pep)

(Some of this behavior can be configured. All my statements apply to the shells' default configuration unless explicitly specified.)

Solution 3

When you want to scan recursively without the annoying errors you usually get from find or ls (like: "Permission denied", "Not a directory") which can brake scripts or xargs commands, also the tree command wasn't mentioned anywhere here:

tree -fai / | grep -e ".fas$" -e ".pep$"

above I used the root /, but can be any path:

tree -fai /users/jenna/bio/ |  grep -e ".fas$" -e ".pep$""

BTW

Share:
29,361

Related videos on Youtube

Jenna Maiz
Author by

Jenna Maiz

Updated on September 18, 2022

Comments

  • Jenna Maiz
    Jenna Maiz over 1 year

    If I want files ending with .fas I know I can do the following with ls command :

    ls -l /users/jenna/bio/*.fas
    

    and this lists out all the files ending with .fas. But how can I get files ending with either .fas or .pep ?

    ls -l /users/jenna/bio/(*.fas | *.pep) doesn't seem to work ? I know there might be other commands that can do this, but is there a way to make it work with ls ?

    Thanks for any help !

  • Mikhail Krutov
    Mikhail Krutov about 8 years
    first my depend on shell actually, not sure it would work in busybox for example. in bash it would.