12

I run this command:

~/shell_temp$ find . -type f -name "IMAG1806.jpg" -exec rm -f {}\

i got output below:

> IMAG1806.jpg

Error:
find: missing argument to `-exec'

what is exact command for find any file from current directory and remove with -exec?

  • 1
    Can't you just to rm "IMAG1806.jpg"? It's in the same directory, so you know what it's called. – TheWanderer Apr 18 '15 at 19:36
  • @Zacharee1 - would assume the OP is doing a "dry run". I would consider using a -i with rm just in case your find "finds" more files then intended or at least running without the -exec and reviewing the file list – Panther Apr 18 '15 at 19:43
  • 1
    @Zacharee1: The find command will delete all files called with this name in the current directory and all of its subdirectories (at any depth). – Dennis Apr 18 '15 at 21:07
  • 3
    Or instead of using -exec rm just use -delete directly. – Braiam Apr 19 '15 at 0:59
29

You missed the a ; at the end (and a space too between {} and ;). The correct command is:

find . -type f -name "IMAG1806.jpg" -exec rm -f {} \;

; indicates the end of -exec predicate of find.

Also note that we have used \; i.e. \ in front of ; to escape the interpretation of ; by shell, otherwise shell will treat ; as end of the whole find command and find will throw the same error. You can also use ';' instead of \;.

You were using \ at the end, this indicates your shell will continue to take input via PS2 (indicated by >), you typed IMAG1806.jpg again, so the whole command becomes:

find . -type f -name "IMAG1806.jpg" -exec rm -f {}IMAG1806.jpg

As you can see this is not a valid command at all with IMAG1806.jpg at the end, no closing of -exec predicate and without a space between {} and \;.

  • 1
    "You missed the a ; at the end " Thanks for this, spent 10 minutes troubleshooting before I realized I had accidentally typed : instead of ;, on these modern HD screens its easy to miss... – user5359531 Dec 30 '16 at 17:49
7

You can simply

find . -type f -name 'IMAGE1806.jpg' -delete

From the man page:

Delete files; true if removal succeeded.  If the removal failed,
an  error message is issued.  If -delete fails, find's exit sta‐
tus will be nonzero (when it eventually exits).  Use of  -delete
automatically turns on the -depth option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.