I have a set of files like this:


I only know the prefixes (A, B, C) but the rest of the name is unknown beforehand, and so is the number of files. I need to call some commands passing them groups of those files like this:

mycommand A*.xlsx
mv A*.xlsx tmp/
mycommand B*.xlsx
mv B*.xlsx tmp/
mycommand C*.xlsx
mv C*.xlsx tmp/

I've tried different syntaxes for the loop like for fileGroup in A* B* C* but I've only managed to pass the filenames one by one to mycommand, or instead to call mycommand only once with all the files. Is there a syntax in bash to perform this?

  • 1
    What exactly is wrong with mycommand A*.xlsx, mv A*.xlsx tmp/, etc.?
    – roaima
    Dec 15, 2020 at 18:57
  • I want to put the mycommand, mv and a few other commands inside a loop (there are a lot of file patterns and a few lines of code for each one of them, all of them the same lines)
    – golimar
    Dec 15, 2020 at 19:00
  • 1
    Why do you want a loop? (Do you really want to call mycommand multiple times, or is that you don't know you can do mycommand A*.xlsx B*.xlsx C*.xlsx, etc.?)
    – roaima
    Dec 15, 2020 at 19:02

2 Answers 2


Assuming you know and can hard-code the prefixes:

for p in A B C D; do
    mycommand "$p"*.xlsx
    mv "$p"*.xlsx tmp/

Use a glob expression for all of them. For files starting with A or B or C.

<mycommand> [ABC]*.xlsx
mv -t tmp/ -- [ABC]*.xlsx

Here is a good read about the glob.

In case you need to execute commands that do not accept many file arguments, you would have to invoke separated processes for each argument, but still you can select them using the same glob:

for f in [ABC]*.xlsx; do
    <some_command> "$f"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.