Sunday, May 5, 2024
73
rated 0 times [  73] [ 0]  / answers: 1 / hits: 58646  / 3 Years ago, fri, july 16, 2021, 5:50:51

Sometimes I see the following command:



find . -name  * -exec ls -a {} ;


I was asked to execute this.



What does {} ; mean here?


More From » command-line

 Answers
0

If you run find with exec, {} expands to the filename of each file or directory found with find (so that ls in your example gets every found filename as an argument - note that it calls ls or whatever other command you specify once for each file found).



Semicolon ; ends the command executed by exec. It needs to be escaped with so that the shell you run find inside does not treat it as its own special character, but rather passes it to find.



See this article for some more details.






Also, find provides some optimization with exec 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.



find ~ -iname '*.jpg' -exec ls {} ;
# vs
find ~ -iname '*.jpg' -exec ls {} +


Assuming you have some jpg files (with short enough paths), the result is one line per file in first case and standard ls behavior of displaying files in columns for the latter.


[#29703] Friday, July 16, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rryble

Total Points: 489
Total Questions: 121
Total Answers: 119

Location: Marshall Islands
Member since Mon, May 31, 2021
3 Years ago
;