How to list a subset of files in several directories?

2

1

I'm starting to pick up Linux. I have one directory called class03 with five directories in it:

_html
html
dothtml
images
123

I need a one-line command that lists all files with -01, -02 and -03 in the file name in those directories.

I can get all the files to list with the command:

ls _html/ html/ dothtml/ images/

When i try to put the restrictions it still lists without the restriction and errors with the *-0[123]

I tried:

ls *-0[123] * _html/ html/ dothtml/ images/

and:

ls _html/ html/ dothtml/ images/ * -0[123] *

So what do I need to do to make the code work in one command?

brian fox

Posted 2012-09-07T23:03:32.870

Reputation: 21

By the way, if you want to list all the files in, say, the _html and html directories, it's good enough to say ls _htlm html -- you don't need to type / at the end of each directory name. – Scott – 2012-09-08T00:09:21.927

Answers

2

If there are no other directories, you can use just

ls */*-0[123]*

If there are more directories you want to exclude, you can use the brace expansion:

ls {{,_,dot}html,images,123}/*-0[123]*

choroba

Posted 2012-09-07T23:03:32.870

Reputation: 14 741

now if i wanted to move those files would i just use that same command replace ls to mv – brian fox – 2012-09-07T23:53:49.960

@brianfox: Sure. But be careful if files with the same name exist in different source directories, you will get mv: will not overwrite just-created. – choroba – 2012-09-08T00:18:12.167