问题
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?
回答1:
find . -type f -exec bash -c 'echo "append this" >> "{}"' \;
回答2:
Literally or figuratively...
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).
回答3:
Another way is to use a loop:
find . -type f | while read i; do
echo "apendthis" >> "$i"
done
来源:https://stackoverflow.com/questions/15604538/using-echo-append-file-recursively