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?
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.
Following Why are the backslash and semicolon required with the find command's -exec option?, I used
find . -name '*pyc' -exec rm {} \;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With