Reputation: 1459
I want to append a line to the end of every file in a folder. I know can use
echo apendthis >> file
to append the string to a single file. But what is the best way to do this recursively?
Upvotes: 3
Views: 5503
Reputation: 146073
Do you mean recusively to be taken literally or figuratively? If you are really in search of a specifically recursive solution, you can do that like so:
operate () {
for i in *; do
if [ -f "$i" ]; then
echo operating on "$PWD/$i"
echo apendthis >> "$i"
elif [ -d "$i" ]; then
(cd "$i" && operate)
fi
done
}
operate
Otherwise, like others have said, it's a lot easier with find(1).
Upvotes: 2
Reputation: 33327
Another way is to use a loop:
find . -type f | while read i; do
echo "apendthis" >> "$i"
done
Upvotes: 1