3

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
  • It deletes your swap files with the extension .swp. In my system using: $ locate *.swp tells me I have one swap file: /etc/systemd/system/apt-daily.timer.d/..#override.confc1f37c0bd02ea65e.swp This is different than the swap partition. Better answers can be provided if you include the link to what you were reading. Oct 9, 2020 at 5:41

4 Answers 4

2

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

When you execute the above command, find will search for the given pattern in the directories and sub-directories. If found, it returns the filename with the relative path from the current directory. For example:

  [guru@guru-Aspire~]$ touch temp1.swp temp2.swp temp3.swp   # Create 3 files.

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

find takes all arguments after -exec as part of the command until the argument ; is reached. {} is replaced with the filename searched by the find command.

The filename and ; is needed to be separated so that it 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

You must log in to answer this question.

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