miglesias
miglesias

Reputation: 11

List multiple directories contents and its path

I want to write a linux script or command that will:

Look into multiple specific directories and list its contents

For example

/test/dir1/abc/version1/program_name/

/test/dir1/abc/version2/program_name/

/test/dir1/abc/version3/program_name/

/test/dir1/bca/version1/program_name/

/test/dir1/bca/version2/program_name/

/test/dir1/bca/version3/program_name/

/test/dir1/cab/version1/program_name/

/test/dir1/cab/version2/program_name/

/test/dir1/cab/version3/program_name/

I can do a

ls -al /test/dir1/*/ 

and see its contents. But I just want to see what it inside version2 and version3.

for example

ls -al /test/dir1/*/<version2 or version3>/*

and get a list like:

/test/dir1/abc/version2/program_name/

/test/dir1/abc/version3/program_name/

/test/dir1/bca/version2/program_name/

/test/dir1/bca/version3/program_name/

/test/dir1/cab/version2/program_name/

/test/dir1/cab/version3/program_name/

Not including version1. There is more directories than version1, version2, and version3. Thats why just excluding version1 doesnt work.

Any help really appreciated!

Upvotes: 0

Views: 154

Answers (2)

Kevin
Kevin

Reputation: 2182

You want to use two glob expansions for this search. Try this:

ls -al /test/dir1/*/version[23]/*

It will search through all of the /test/dir1/* directories, and then look for subdirectories matching either 'version2' or 'version3'.

Upvotes: 1

anubhava
anubhava

Reputation: 785058

You can use list feature (glob) in BASH:

ls -al /test/dir1/*/{version2,version3}/*

Upvotes: 0

Related Questions