Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform a command on multiple files using command line

Say I have the following files:

myfile1_1.dat
myfile1_2.dat
myfile2_1.dat
myfile2_2.dat
...

and I want to use the same command on each of them (whilst ignoring other files in the directory that do not begin with myfile). Is there an easy way of doing this?

So far, I have been toying with wildcards:

mycommand myfile*.dat

EDIT: I am using both Linux and MinGW

like image 492
atomh33ls Avatar asked Aug 29 '26 23:08

atomh33ls


1 Answers

mycommand myfile*.dat will only work if mycommand supports multiple files. If it doesn't, you can use xargs to pass a single file to mycommand as shown below:

echo myfile*.dat | xargs -n 1 mycommand

Alternatively, use a loop:

for file in myfile*.dat; do
    mycommand "$file"
done
like image 106
dogbane Avatar answered Aug 31 '26 15:08

dogbane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!