MadHatter
MadHatter

Reputation: 386

BASH; using find and -exec to remove .pyc files

Using the command line in Ubuntu 16.04.2 LTS. I'm getting towards the end of Zed Shaw's LPTHW, and on the video to ex46.py he exercises the following bash command to find and remove all .pyc byte code files:

find . -name "*.pyc" -exec rm {}

On the video this successfully removes all of Zed Shaw's .pyc files. However, upon typing in the exact same command I get the following error:

find: missing argument to `-exec'

I understand that there are many ways to delete .pyc files, however, since I'm following along with Zed Shaw I'd like to know how to do it using find and -exec. What am I doing wrong?

Upvotes: 1

Views: 738

Answers (2)

hiro protagonist
hiro protagonist

Reputation: 46859

you need to terminate the -exec command with \;

find . -name "*.pyc" -exec rm {} \;

have a look at find -exec in the man page.

as mentioned in the comments by Gordon Davisson it may be more efficient to terminate the command with + as rm is then invoked fewer times:

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

Upvotes: 4

Inian
Inian

Reputation: 85580

You could leverage using -delete over -exec rm as the former does not spawn a new process for each of the file instance to delete. Also you could chip in the -type f option to apply the operation for files only.

find . -type f -name "*.pyc" -delete

Upvotes: 3

Related Questions