Matt
Matt

Reputation: 105

Run a bash script on an entire directory

I have a bash script that removes the first five lines and last nine lines on a user specified file. I need it to run on ~100 files a day, is there any way to run the script against the entire directory and then output every file to a separate directory with the same file name? Here is the script I am using.

read -e file
$file | sed '1,7d' | head -n -9 > ~/Documents/Databases/db3/$file
rm $file

I set it up to loop because of so many files but that is the core of the script.

Upvotes: 0

Views: 151

Answers (1)

heemayl
heemayl

Reputation: 42107

You can do:

for file in ~/Documents/Databases/db3/*; do
    [ -f "$file" ] && sed '1,7d' "$file" | head -n -9 > /out/dir/"${file##*/}"
done

Assuming the input directory is ~/Documents/Databases/db3/ and the output directory is /out/dir/.

Upvotes: 2

Related Questions