Recursive directory scripting

William Park opengeometry at yahoo.ca
Tue Oct 15 06:24:18 CEST 2002


Will Stuyvesant <hwlgw at hotmail.com> wrote:
> Often I have to do something in the current directory and
> recursively below.  Like cleaning up.  Can I use python
> for this?  Yes I can, the scripts are below.  I tried to
> make it portable so it would also work with linux, but I
> gave up.  Maybe you can?
> 
> Improvements and other comments are welcome!

[sniped]

> With 'C:\bin' added to my %PATH% and walkcommand.bat and
> walkcommand.py there I can now clean my directory and any
> directories below like this: 
> C:\doc> walkcommand del *.aux *.toc *.log *.*~ *~ *.bak
> 
> Of course it is saver to put 'del *.aux *.toc *.log *.*~
> *~ *.bak' in a file called clean.bat in C:\bin and do:
> C:\doc> walkcommand clean

On Linux, you would just do
    find -type d | while read i; do
	(cd $i; rm *.aux *.toc *.log *.*~ *~ *.bak)
    done
or
    find -type f -name '*.aux' | xargs rm
    find -type f -name '*.toc' | xargs rm
    find -type f -name '*~'    | xargs rm
    find -type f -name '*.bak' | xargs rm

No need for convoluted script on script.

-- 
William Park, Open Geometry Consulting, <opengeometry at yahoo.ca>
Linux solution for data processing and management



More information about the Python-list mailing list