Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Assume that I have two sets of files and Each set contains (a known) number of files. Now, I want to process corresponding pairs using some tool (ImageMagick in my case), that is I want to run some_command, where is the result/output when running the tool on the pair and for some fixed i.

My question is how can I batch this process? Some python, bash script?

Clarification: By set of files I mean a list of files, all start with the same initial, then some UID and having the same extension. This hold for both input sets and the output.

share|improve this question
When you say "sets of files" do you mean text files containing lists of files? So does "" then mean "the ith line in file"? If that's the case you can use the unix "paste" command to merge the two files side-by-side. And if that's not what you mean, I don't understand it. – Paul Richter Jan 19 '12 at 15:22
By "set of files" I mean that I have two sets, each contains the same number of files. Each element of either of the sets is a file - .png in my case. – Dror Jan 19 '12 at 15:35
How long are those files? Will both lists of names easily fit into RAM? Are there filenames containing blanks and other, unusual characters like tab, line feed, page feed and so on? – user unknown Jan 19 '12 at 16:25
The files in my case are binaries... – Dror Jan 19 '12 at 19:03
I was confused because it's not very clear from 'foo bar' style names you chose whether '' was a text file containing file names (which I assume it is) or, for example, a directory containing the files. It's much clearer to use a name like 'file_list_A.txt' for a "set" and 'image_A_01.png" for a target file. – Paul Richter Jan 20 '12 at 1:21

2 Answers 2

up vote 1 down vote accepted

Here's a possible solution, which works, although I don't know if it's a good one.



if [ $# -ne $EXPECTED_ARGS ]
    echo Not the right number of arguments
    exit $E_BADARGS

for i in $( ls $1* ); do
    TMP=$(echo $2$(echo $i | cut -d'_' -f2))
    TMP2=$(echo $3$(echo $i | cut -d'_' -f2))
    convert $i $TMP +append $TMP2

Running it should be something like:

script foo_A_ foo_B_ out_
share|improve this answer
count=$(wc -l
for line in $(seq 1 ${count/ */})
    aline=$(sed -n $line"p"
    bline=$(sed -n $line"p"
    yourcommand "$aline" "$bline"

Assuming both files are sorted, the count-line get's the number of lines in file a. Then a loop runs over all values from 1..count. The count-variable contains the name of the file, which is cut off with ${count/ */}. With sed, you get the same line from both files. -n means 'no printing by default', $line"p" will then just print line $line.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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