Using “ echo append >> file” recursively

这一生的挚爱 提交于 2020-01-02 09:58:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!