[Tutor] Move all files to top-level directory

Alan Gauld alan.gauld at btinternet.com
Tue Apr 13 02:13:48 CEST 2010

"Dotan Cohen" <dotancohen at gmail.com> wrote

>I use this one-liner for moving photos nested a single folder deep
> into the top-level folder:
> find * -name "*.jpg" | awk  -F/ '{print "mv "$0,$1"-"$2}' | sh

You could miss out the awk and use the exec option of find...

Or miss out the shell and use the system() function of awk.

> I need. I have googled file handling in Python but I simply cannot get
> something to replicate the current functionality of that lovely
> one-liner. What fine manual should I be reading? I am not asking for
> code, rather just a link to the right documentation.

You won't easily get a one liner to do the same in Python but you
can do the same things using the glob, subprocess, shutil, os and 
path modules. In particular look at the os.walk and the shutil.move 

In addition to the module documentation you will find examples and 
description of both in the Using the OS topic of my tutorial.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list