1

I want to execute a command which takes multi arguments that reside in different directories under same filename, e.g.

mycommand dir-001/myfile dir-002/myfile dir-003/myfile … dir-048/myfile

I am looking for some shell command that could put all files in subdirectories numbered starting from 1 up to 48 or whatever number as command arguments.

2
  • 2
    Could you clarify if the directory part is variable, or the file base name part? And if the number of directories is variable, what are their actual names and in what order should they be listed? Different people have understood your question differently. Commented May 21, 2017 at 22:45
  • Hi Gilles, directories are variable. mycommand takes multi-arguments (which is a file) and its located in directories named as 001 to 048 (total 48 directories) . mycommand {dir-1,dir-2,dir-3}/myfile this command works but I am lazy to type all directory names, because in some cases I have upto 96 directories.
    – Sufyan
    Commented May 24, 2017 at 9:40

2 Answers 2

2

You may use standard filename globbing, e.g. for all 3-digit numbers (in order):

$ mycommand dir-[0-9][0-9][0-9]/myfile

With basic globbing, you need one pattern for one-digit numbers, one for two-digit numbers, etc.

$ mycommand dir-[0-9]/myfile dir-[0-9][0-9]/myfile

Zsh (but not other shells) also supports mycommand dir-<->/myfile, or e.g. mycommand dir-<3-42>/myfile for the directories numbered 3 to 42.

Or if you prefer brace expansion (and your shell supports it):

$ mycommand dir-{1..3}/myfile

If your directory names contain leading zeros, put them in the braces, e.g. dir-{001..048}/myfile.

With brace expansion, you may also do (in ksh93 and zsh, but not in bash)

$ mycommand dir-{1..$n}/myfile

where $n expands to some integer.

0
-1
find . -type f -name myfile | xargs command
2
  • The question asks for "starting from 1 up to 3 or whatever number".
    – Sparhawk
    Commented May 21, 2017 at 22:40
  • This would list all files in all subdirectories, all subsubdirectories, etc., and doesn't give any guarantee as to their order. Commented May 24, 2017 at 11:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .