TZJ
TZJ

Reputation: 159

How do I only list specific files with 'ls' in bash?

I was wondering how I can list files with ls in bash that will only list a specific subset of files?

For example, I have a folder with 10000 files, some of which are named: temp_cc1_covmat and temp_cc1_slurm, but the values of 1 range from 1-1000.

So how would I list only say, temp_cc400_slurm-temp_cc_499_slurm?

I want to do this as I would like to queue files on a supercomputer that only ends with slurm. I could do sbatch *_slurm but there are also a lot of other files in the folder that ends with _slurm.

Upvotes: 4

Views: 2544

Answers (2)

James Brown
James Brown

Reputation: 37404

Using the ? wildcard:

$ ls temp_cc4??_slurm

man 7 glob:

Wildcard matching
   A  string  is  a  wildcard pattern if it contains one of the characters
   '?', '*' or '['.  Globbing is the operation  that  expands  a  wildcard
   pattern  into  the list of pathnames matching the pattern.  Matching is
   defined by:

   A '?' (not between brackets) matches any single character.

The argument list too long error applies using the ? also. I tested with ls test????? and it worked but with ls test[12]????? I got the error. (Yes, you could ls temp_cc4[0-9][0-9]_slurm also.)

Upvotes: 1

anubhava
anubhava

Reputation: 785146

You can use this Brace Expansion in bash:

temp_cc{400..499}_slurm

To list these file use:

echo temp_cc{400..499}_slurm

or:

printf "%s\n" temp_cc{400..499}_slurm

or even ls:

ls temp_cc{400..499}_slurm

Upvotes: 3

Related Questions