1

What does \; mean in the following command? I’m also not sure what {} means. Is \; really necessary?

find ./ -name file_1 -exec cp {} ./ \;
3

3 Answers 3

7

In your example \; is part of the find command. (see man 1 find). The same is true for {}.

This precise command states:

  • Find everything in the current directory (the dot or in your case ./)
  • Where the file or directory name is file_1
  • And then execute the following command: cp {} ./
    where the {} will replace the found file or directory.

The result is that all files and directories called file_1 will be copied to your current directory. (and they are likely to get overwritten if there are multiple files or dirs with that one name)


So much for the general explanation. Now the nitty-pick details:

The command which will run for every item that find finds is: cp full_path_to_item ./
This is the content between the -exec and the ;

However the semicolon is a special character in bash. If we do not do anything special with it then the shell will eat it. So we escape it with a backslash. This tells the shell to leave it alone and pass it on to find.


Note that you will need to do this for all special characters. E.g. you can not simply use an asterix in the filename (so no find . -type f -name *.mp3 ....)

0
3

The reason the semi-colon needs to be escaped is that it will be interpreted by the shell instead of by find if it's not escaped. find expects to see this on its command line, so the backslash tells the shell to ignore it and pass it on to find. It's essential that find see the semi-colon or it will give you an error.

{} is also interpreted by find. This is a placeholder you put in the command you want to run, and for every file find finds, it will replace those two brackets with the filename and run your command.

In your example, if it found file_1, the following command will be run:

cp file_1 ./
2

The {} \; has not so much to do with shell, but more with the arguments that the find command expects to an -exec subcommand. The {} is replaced with the current filename, e.g. this results in find executing the command ls -d FILENAME with FILENAME replaced by each file it found. The \; serves as a terminator of the -exec argument. See the manual page of find, e.g. type man find on a linux shell, and look for the string -exec there to find the description.

Not the answer you're looking for? Browse other questions tagged .