Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 553  / 2 Years ago, wed, november 24, 2021, 7:14:16

I found the following awesome little script at link




$ find /home/bruno/old-friends -type f -exec sed -i
's/ugly/beautiful/g' {} ;




But I really do not understand the final bit




{} ;




Could someone please explain??



Thanks!


More From » bash

 Answers
7

This is not exactly a sed syntax problem, it's a find syntax problem.



The {} is a container that will replace the name of the file being processed by the sed command.



If you use find like this:



$ find . -type f -name *.py -exec head {} ;


This will find Python scripts and show you the head of each file. If you have three scripts in the current directory:




  • 1.py

  • 2.py

  • 3.py



The {} will contain each file name and execute the head command on each file.



The -exec argument needs a ; at the end so it can recognize the end of the command arguments (sed in this case) but, in order to protect ; from expansion you need to escape it with the .



Remember that you can execute several commands at a time if you use the ;:



$ ls; pwd;


This will list directories (ls) and print your working directory (pwd). The -exec argument also needs the ;. But since you don't want to confuse the ; that separates commands with the ; that is part of the -exec argument of find, you need to escape it with the .



Alternatively, you can use quotes around the ;:



$ find /home/bruno/old-friends -type f -exec sed -i 's/ugly/beautiful/g' {} ";"


I hope I made myself clear


[#36810] Thursday, November 25, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
chilgirlguid

Total Points: 123
Total Questions: 114
Total Answers: 121

Location: Macau
Member since Mon, Nov 16, 2020
4 Years ago
;