1

I am learning searching and I'm confused this command.

find -name "*.swp" -exec rm {} ';'

Can you explain to me what this command means?

1

exec command executes a specific command for each file found. It treats it's arguments as a sub-process to execute. It is one of the most powerful and dangerous option provided by the find command.

When you execute the above command find search for the given pattern in the directories and sub directories and if found it returns the file name with the relative path from the current directory. for exapmle

  [guru@guru-Aspire~]$ touch temp1.swp temp2.swp temp3.swp   #(created 3 file)

  [guru@guru-Aspire~]$ find -name "*.swp" 
  ./temp2.swp
  ./temp3.swp
  ./temp1.swp

find takes all arguments after -exec as a part of the command until an argument consisting of ; is reached and replace {} with the file-name searched by the find command.

The file-name and ; is needed to be separated so that is can be protected from the expansion. So either we can escape it using \; or we can use or quoted like ';'

2

-exec is an option of find. What's happening here is all files matching *.swp are removed one by one. {} is the full path and file name, not sure about trailing ;. But the first term of find should be a directory or path to one. To learn more at the terminal type man find. man manuals can be searched using the apropos command.

1

Rather than giving you a fish, here's how to fish: Type this in the console:

 man find

Or this (doens't work on all systems):

info find

Also check this out: LDP: Bash for Beginners and learn to distinguish between a command and it's arguments ;)

0

That line simply finds files recursively (in each subdirectory of command issued) with swp extension and removes them. Here is a guide :http://www.ling.ohio-state.edu/~kyoon/tts/unix-help/unix-find-command-examples.htm#EX02 And this guide explains how to use -exec switch with find command http://www.softpanorama.org/Tools/Find/using_exec_option_and_xargs_in_find.shtml

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.