0

User-define action is a feature of Bash in linux. In Linux terminal if I write

root@localhost: find ~ -type f -name 'foo*' -ok ls -l '{}' ';'

then it checks if there is any filename starting with foo and shows the file details like this:

< ls ... /home/me/bin/foo > ? y
-rwxr-xr-x 1 me me 224 2011-10-29 18:44 /home/me/bin/foo
< ls ... /home/me/foo.txt > ? y
-rw-r--r-- 1 me me 0 2012-09-19 12:53 /home/me/foo.txt

As long as I press y it shows me those results.

My question is what is the special meaning of the '{}' ';' characters ?

I read somewhere that {} represents the current path and ';' ends the command, but in bash I never end a line with ';'.

Mastan
  • 215

2 Answers2

6

These are not part of bash; find is a standalone program and does not require bash or even a POSIX shell to run. For example, it works fine with fish, which is not POSIX compliant and does not follow all the same syntax rules as bash. You could, in fact, use it with no shell at all (e.g., in a programmatic context).

This is why (if you are using a POSIX shell, or one which has similar rules in this instance) you want to use quotes with certain arguments to find such as find . -name '*foo'. The string in single quotes is passed through without performing any substitution or expansion. The shell does nothing with it. If you use find . name *foo with bash and there happens to be files matching that glob in your current directory, what you intended to happen probably will not happen.

If you use a shell with different rules, you will have to take those into account when using find. For example, you actually don't need to quote the {} with bash, but if you do the same thing with fish, you will get find: missing argument to '-exec'.

Curly braces do have a meaning in bash -- they are used in parameter substitution/expansion and to group commands. They have a sort of similar purpose in find, but it is find that parses them, not bash.

goldilocks
  • 87,661
  • 30
  • 204
  • 262
4

Although both curly braces {,} and semicolons ; do have special meanings in bash, in this case it is the find command itself that is interpreting them, not the shell. The -ok command of find uses the same syntax as its -exec command, so you will find a complete description in that section of its manual page (man find):

   -exec command ;
          Execute  command;  true  if 0 status is returned.  All following
          arguments to find are taken to be arguments to the command until
          an  argument  consisting of `;' is encountered.  The string `{}'
          is replaced by the current file name being processed  everywhere
          it occurs in the arguments to the command, not just in arguments
          where it is alone, as in some versions of find.  Both  of  these
          constructions might need to be escaped (with a `\') or quoted to
          protect them from expansion by the shell.
steeldriver
  • 81,074