Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all the results of a "find" command [duplicate]

Tags:

bash

I have a directory containing several files ending with .pyc, which I'd like to all remove in one go. I've tried both

find . -name '*pyc' | rm

and

find . -name '*pyc' -exec rm

However, neither of these statements is in accordance with the usage (in the first case) or syntactically correct (in the second case, I get find: -exec: no terminating ";" or "+"). How can I 'pipe' the results to a 'remove' command?

like image 708
Kurt Peek Avatar asked Jul 11 '17 15:07

Kurt Peek


Video Answer


2 Answers

find . -name '*.pyc' -exec rm -- '{}' +

From man find:

  • -exec utility [argument ...] ; True if the program named utility returns a zero value as its exit status. Optional arguments may be passed to the utility. The expression must be terminated by a semicolon (;). If you invoke find from a shell you may need to quote the semicolon if the shell would otherwise treat it as a control operator. If the string {} appears anywhere in the utility name or the arguments it is replaced by the pathname of the current file. Utility will be executed from the directory from which find was executed. Utility and arguments are not subject to the further expansion of shell patterns and constructs.

  • -exec utility [argument ...] {} + Same as -exec, except that {} is replaced with as many pathnames as possible for each invocation of utility. This behaviour is similar to that of > xargs(1).

{} doesn't need to be quoted in bash, but this helps compatibility with some other extended shells.

like image 87
Charles Duffy Avatar answered Oct 06 '22 16:10

Charles Duffy


Following Why are the backslash and semicolon required with the find command's -exec option?, I used

find . -name '*pyc' -exec rm {} \;
like image 43
Kurt Peek Avatar answered Oct 06 '22 17:10

Kurt Peek