ubuntu find+sed implements string replacement for files in a directory

Hits: 0

find – search for files in a directory hierarchy

sed – [stream] editor for filtering and transforming text

find ./*  -exec  sed -i ‘s/apple/iphone/g’ {} \;

Replace apple in all files in the current directory (including subdirectories) with iPhone

find ./  -exec ** {} \;

{} stands for file

; Execute one command per file

  • execute a command in total

root@25902ace80e2:/home/test# find ./* -exec echo  arg: {} \+
arg: ./test.txt ./test2 ./test2/test2.txt
root@25902ace80e2:/home/test# find ./* -exec echo  arg: {} \;
arg: ./test.txt
arg: ./test2
arg: ./test2/test2.txt

sed -i ‘s/apple/iphone/g’

Replace apple in the file with iPhone

-i make file changes

Replace single quotes with double quotes

sed -i s#\’#\”#g test.txt

sed -i “s/’/\”/g” test.txt

You may also like...

Leave a Reply

Your email address will not be published.