Sometimes I see the following command:
find . -name * -exec ls -a {} \;
I was asked to execute this.
What does {} \;
mean here?
command linefind
Sometimes I see the following command:
find . -name * -exec ls -a {} \;
I was asked to execute this.
What does {} \;
mean here?
Best Answer
If you run
find
withexec
,{}
expands to the filename of each file or directory found withfind
(so thatls
in your example gets every found filename as an argument - note that it callsls
or whatever other command you specify once for each file found).Semicolon
;
ends the command executed byexec
. It needs to be escaped with\
so that the shell you runfind
inside does not treat it as its own special character, but rather passes it tofind
.See this article for some more details.
Also,
find
provides some optimization withexec cmd {} +
- when run like that,find
appends found files to the end of the command rather than invoking it once per file (so that the command is run only once, if possible).The difference in behavior (if not in efficiency) is easily noticeable if run with
ls
, e.g.Assuming you have some
jpg
files (with short enough paths), the result is one line per file in first case and standardls
behavior of displaying files in columns for the latter.