Arash Saidi
Arash Saidi

Reputation: 2238

Execute Script to Run on Multiple Files

I have a script that I need to run on a large number of files.

This is the script and how it is run:

./tag-lbk.sh test.txt > output.txt

It takes a file as input and creates an output file. I need to run this on several input files, and I want a different output file for each input file.

How would I go about doing this? Can I make a script (I have not much experience writing bash scripts).

[edits]:

@fedorqui asked: Where are the names of the input files and output files stored?

There are several thousand files, each with a unique name. I was thinking maybe there is a way to recursively iterate through all the files (they are all .txt files). The output files should have names that are generated recursively, but in a random fashion.

Upvotes: 1

Views: 277

Answers (2)

Ram
Ram

Reputation: 1115

The below script will find the files with extension .txt and redirect the output of the tag-1bk script to the randomly generated log file log.123 ..

#!/bin/bash
declare -a ar
# Find the files and store it in an array
# This way you don't iterate over the output files
# generated by this script
ar=($(find . -iname "*.txt"))
#Now iterate over the files and run your script
for i in "${ar[@]}"
do
    #Create a random file in the format log.123,log.345
    tmp_f=$(mktemp log.XXX)
    #Redirect your output to the log file
   ./tag-lbk.sh "$i" > $tmp_f
done

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328604

Simple solution: Use two folders.

for input in /path/to/folder/*.txt ; do
    name=$(basename "$input")
    ./tag-lbk.sh "$input" > "/path/to/output-folder/$name"
done

or, if you want everything in the same folder:

for input in *.txt ; do
    if [[ "$input" = *-tagged.txt ]]; then
        continue # skip output
    fi
    name=$(basename "$input" .txt)-tagged.txt
    ./tag-lbk.sh "$input" > "$name"
done

Try this with a small set of inputs somewhere where it doesn't matter when files get deleted, corrupted and overwritten.

Upvotes: 1

Related Questions