16
6
I have a set of files in a directory numbered as follows:
file1.txt
file2.txt
...
file30.txt
...
Now, I'd like to run a command on a specific range of files, lets say 18 through 31.
So far I have used the following ways,
three arguments
command file1[8-9].txt file2[0-9].txt file3[0-1].txt
Now suppose I want every other number,
loop
for i in `jot - 18 31 2`; do echo file${i}.txt; done | xargs command
this seems like it's a more reliable loop (spaces work)
for i in `jot - 18 31 2`; do echo "\"file${i}.txt\" "; done | xargs command
but it seems like there should be a simpler way to do this. I am guessing the best solution is to create a bash function that will return the set of files to the command line. Then I could do something like,
command `filerange file 18 31`
Are there better ways or suggestions on doing this efficiently?
I apologize in advance if I missed this same question answered somewhere else
on the web or on superuser
.
I updated my question to include a solution using a
– milkypostman – 2011-01-22T17:08:24.990for
loop that allows for spaces in filenames using xargs. It seems to work if you don't have bash 4. If you have bash 4, then definitely use brace expansion! See the selected answer. Shameless plug to upgrade Snow Leopard bash