Thursday 3 October 2013 — This is 11 years old. Be careful.
Recently I was debugging one of those “it can’t happen” kinds of problems, and wanted to make sure I didn’t have any stale .pyc files lying around. I figured the “find” command could find pairs of files whose dates compared incorrectly, but I didn’t know how to do it.
I asked in the #bash IRC channel, and they gave me this:
find . -name '*.pyc' -exec bash -c 'test "$1" -ot "${1%c}"' -- {} \; -print #stalepyc
It’s one of those Unix-isms I won’t be able to remember (yet?), so I’ll leave it here to find again when I need it later.
Notice I’ve added a bashtag to it so I can search for it in my command history. (I wish I had come up that name!).
I’m sure there are other ways to find stale files, maybe even better ones?
Comments
I normally just do a "find . -name '*.pyc' | xargs rm" in the root of my project since it's easy to remember & I don't care about deleting valid pyc files along with stale ones.
BTW: find has -delete, so you can use: find . -name '*.pyc' -delete
To save others the time:
bash -c : from the man page
-c string If the -c option is present, then commands are read from
string. If there are arguments after the string, they are
assigned to the positional parameters, starting with $0.
Therefore the command going to bash is:
test "$1" -ot "${15c}
the -- is used to signal the end of command line options and the {} \; is the results of the find command and the exec terminator.
${1} is the first command line arg passed (the found file name + path == path to a *.pyc file)
$(1%c} is the first arg with a "c" stripped off the end ( == path to a *.py file)
the test -ot asks bash to determine if the pyc file is "older than" the py file
I don't want to guess how much more bash I need to do every day before I would have thought that up on my own ;-)
Stale pyc files cause trouble when the original .py is gone.
find . -name '*.pyc' -exec bash -c 'test ! -e "${1%c}"' -- {} \; -print
Useful mostly for development systems.
http://stackoverflow.com/questions/2528283/automatically-deleting-pyc-files-when-corresponding-py-is-moved-mercurial
Add a comment: