I've renamed some files in a fairly large project and want to remove the .pyc files they've left behind. I tried the bash script:
rm -r *.pyc
But that doesn't recurse through the folders as I thought it would, what am I doing wrong?
Thanks
I've renamed some files in a fairly large project and want to remove the .pyc files they've left behind. I tried the bash script:
rm -r *.pyc
But that doesn't recurse through the folders as I thought it would, what am I doing wrong?
Thanks
rm -r
recurses into directories, but only the directories you give to rm
. It will also delete those directories. One solution is:
for i in $( find . -name *.pyc )
do
rm $i
done
find
will find all *.pyc files recursively in the current directory, and the for
loop will iterate through the list of files found, removing each one.
find . -name '*.pyc' -print0 | xargs -0 rm
The find recursively looks for *.pyc files. The xargs takes that list of names and sends it to rm. The -print0 and the -0 tell the two commands to seperate the filenames with null characters. This allows it to work correctly on files containing spaces, and even a file containing a new line.
The solution with -exec works, but it spins up a new copy of rm for every file. On a slow system or with a great many files, that'll take too long.
You could also add a couple more args:
find . -iname '*.pyc' -print0 | xargs -0 --no-run-if-empty rm
iname adds case insensitivity, like *.PYC . The no-run-if-empty keeps you from getting an error from rm if you have no such files.
$ which pycclean
pycclean is aliased to `find . -name "*.pyc" | xargs -I {} rm -v "{}"'
if you're using bash >=4.0 (or zsh)
rm **/*.pyc
(the globstar shell options must be enabled)
Just to throw another variant into the mix, you can also use backquotes like this:
rm `find . -name *.pyc`