And a bit of soy. And a bit of soy. - 3 days ago 5
Bash Question

Pasting a line within dat files in multiple directories except for dat files with a certain name

Pasting a line within dat files in multiple directories except for dat files with a certain name.

Here's the code I've got so far.

for file in */*; do
if [ $file != "NoEdit.dat" ]
then
echo 'Configuration' >> $file
fi
done


Any ideas on how to exclude files that go by the name of "NoEdit.dat"?
I've been trying to implement !(NoEdit.dat) and "if" arguments.
I am a complete newbie to programming in general, any help would be appreciated!

EDIT:
Using the find command worked.

find */* ! -name 'NoEdit.dat' -exec sh -c 'echo "Configuration" >> {}' \;

Answer

This code achieves what you want:

#!/bin/bash
directory='/folder/'
mkdir -p /tmp/
find "$directory" -maxdepth 1 -type f -printf '%f\n' | grep -v "NoEdit.dat" > /tmp/holder && directoryEscaped="$(echo "$directory" | sed 's/\//\\\//g')" && sed -i -e "s/^/$directoryEscaped/" /tmp/holder
while read -r line; do
filename="$line"
echo 'Configuration' >> "$filename"
done < /tmp/holder
exit 0

All you need to do is to provide the variable "directory" with the folder path to be searched. Basically, the code creates a temporary file with the file paths of all files that don't match the name you provided, and then it echos the string on each one of them (at the end of each file).

Comments