[Tutor] Move all files to top-level directory

Martin A. Brown martin at linux-ip.net
Tue Apr 13 09:33:58 CEST 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

 : 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

I would add a few different features to this 'find' to make it a bit 
more resistant to failure, although this sort of solution is always 
subject to the "somebody else is toying with my filesystem race 
condition".

  find "$srcdir" -depth -type f -print0 \
    | xargs --null --no-run-if-empty -- \
      mv --target-directory "$dstdir" --

The --target-directory option is only available in GNU mv (and cp), 
I believe.

I'll second the recommendation for 'shutil', although you can have 
some fun playing with recursion by building your tree flattener 
using os.walk(), os.path.split() and os.path.join().

- -Martin

- -- 
Martin A. Brown
http://linux-ip.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: pgf-0.72 (http://linux-ip.net/sw/pine-gpg-filter/)

iD8DBQFLxB5oHEoZD1iZ+YcRAvPUAKCmXcIhKVTUDx4IexWnAvnl64uQNACeOFf/
3tsR/sXGVe944dUMhxkPYkk=
=x0EG
-----END PGP SIGNATURE-----


More information about the Tutor mailing list