Jesse
Jesse

Reputation: 1459

Using " echo append >> file" recursively

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

Answers (3)

DigitalRoss
DigitalRoss

Reputation: 146073

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).

Upvotes: 2

user000001
user000001

Reputation: 33327

Another way is to use a loop:

find . -type f | while read i; do
    echo "apendthis" >> "$i"
done

Upvotes: 1

uselpa
uselpa

Reputation: 18917

find . -type f -exec bash -c 'echo "append this" >> "{}"' \;

Upvotes: 8

Related Questions