6

What does

{} \;

string mean in bash?

I've seen it a couple of times, for example, to find and delete all files by name one must execute

find . -name "FILE-TO-FIND" -exec rm -rf {} \;

What does this string do?

heemayl
  • 90,425
  • 20
  • 200
  • 267
spiderface
  • 207
  • 3
  • 7

1 Answers1

13

Both {} and ; have special meanings in bash but in the context of find, it is find which actually interprets these two (actually we want find to interpret these two).

First of all, -exec is an action of find. {} and ; are special parameters for the -exec predicate of find.

The syntax for the -exec action is:

-exec command ;

or

-exec command {} + 

So we need to keep {} and ; from being interpreted by shell beforehand.

In the context of find .... -exec:

  • {} indicates (contains) the result(s) from the find expression i.e. find . -name "FILE-TO-FIND" in this case. Note that empty curly braces {} have no special meaning to shell so we can get away without escaping {}

  • As bash treats ; as end of a command, we need to escape this with \ i.e. \; so that it can be parsed by -exec not by bash itself.

heemayl
  • 90,425
  • 20
  • 200
  • 267