0

I have a set of files like this:

A1.xlsx
A2.xlsx
B1.xlsx
C1.xlsx
C2.xlsx

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?

3
  • 1
    What exactly is wrong with mycommand A*.xlsx, mv A*.xlsx tmp/, etc.? Commented 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) Commented 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.?) Commented Dec 15, 2020 at 19:02

2 Answers 2

1

Assuming you know and can hard-code the prefixes:

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

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"
done

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.