Using the -exec option with the Find command in Bash

I am trying to use the -exec option with the find command to find specific files in my huge panorama directory and move them to the specified location. The command below passes an error argument not found for -exec. Can someone point out my error while parsing the command? Or would I need to create some kind of pipe?

$ find -name ~ / path_to_directory_of_photos / specific_photo_names * -exec mv {} ~ / path_to_new_directory /

+4
source share
3 answers

You need to cancel the exec'ed command using the skeletal semicolon ( \; ).

+7
source

You must specify a name pattern, otherwise the shell will expand any wildcards in it before running find . You also need to have a semicolon (backslashed to avoid interpreting the shell as a command separator) to indicate the end of the mv command.

The correct command is:

 find ~/path_to_directory_of_photos -name "specific_photo_names*" -exec mv {} ~/path_to_new_directory \; 
+3
source

I know this post is old, but here is my answer in case it helps someone else. See the background of this post . If you end the command with + instead of \; you can run it much more efficiently. \; will cause mv to be executed once per file, and + will lead to mv with the maximum number of arguments. For instance.

 mv source1 destination/ mv source2 destination/ mv source3 destination/ 

vs.

 mv source1 source2 source3 destination/ 

The latter is much more effective. To use +, you must also use --target-directory. For instance.

 find ~/path_to_directory_of_photos -name "specific_photo_names*" -exec mv --target-directory="~/path_to_new_directory" {} + 
0
source

All Articles