BashScripts.org
http://bashscripts.org/forum/

scripts which moves all files in certain DIR on dir up.
http://bashscripts.org/forum/viewtopic.php?f=8&t=1101
Page 1 of 1

Author:  Kiranos [ Sat May 15, 2010 10:57 am ]
Post subject:  scripts which moves all files in certain DIR on dir up.

Hi I'm trying to organise my data and currently I have a structure of my movies like this:

ls /home/adam/movies

Star.trek1
-CD1/Star.trek1-cd1.avi
-CD2/Star.trek1-cd2.avi
Star.trek2
-CD1/Star.trek2-cd1.avi
-CD2/Star.trek2-cd2.avi
Star.trek3
-CD1/Star.trek3-cd1.avi
-CD2/Star.trek3-cd2.avi

etc..

This has worked really well but now I'm starting to use XBMC and it likes to have all files of a movie in the same dir.

-Star.trek1
Star.trek1-cd1.avi
Star.trek1-cd2.avi


So I'm looking for a script that does this automatically:

./movefiles.sh /home/adam/movies/

This would go over all my movies and look for CD1 and CD2 directories and when it finds them, move all files in those directories one dir up to the movies "base" dir. If the movies doesnt have a CD1/CD2 dir, nothing should happen.

Also some of my movies are categories in subdirectories, like all star trek movies could all be inside a directory: /home/adam/movies/startrek/

So recursive search from base dir (./movefiles.sh /home/adam/movies/) would be great! :)

Maybe this is complicated, I'm new to scripting and programming in general but would love some help with this!! It would save me loads of time and I think it might help others aswell, as media centers only seem to grow and grow.

Thanks again, and if you have any questions please post here and I'll come back to you.

Author:  DarthWavy [ Sat May 15, 2010 7:53 pm ]
Post subject:  Re: scripts which moves all files in certain DIR on dir up.

I dont have time right now to write this but I do suggest that instead of actually moving the files you create symlinks to them.

Author:  Kiranos [ Sun May 16, 2010 3:10 am ]
Post subject:  Re: scripts which moves all files in certain DIR on dir up.

Hi, thanks for the reply but I'm looking for permanent movement. As this has to be compatible with samba and windows etc.., So I'd rather have them correctly placed physically. Again thanks for looking into it.

Author:  choroba [ Sun May 16, 2010 7:29 am ]
Post subject:  Re: scripts which moves all files in certain DIR on dir up.

Code:
find -type d -name 'CD[0-9]' -exec echo mv {}/* {}/.. \; | bash

Then, you will have to remove the empty CD[0-9] directories. But, be careful! Moving files is dangerous, make sure you do not overwrite anything (at least, run the command without the "| bash" part to see what it's really going to do.)

Author:  Kiranos [ Sun May 16, 2010 12:02 pm ]
Post subject:  Re: scripts which moves all files in certain DIR on dir up.

thanks!, I've changed CD1-9 to "CD?" so it takes everything from CD* but has to be 3 letters. could it be improved and make a rmdir CD? after the files has been moved?
if I use rmdir it only removes the dir if its empty so it should be relativly riskfree :)

Author:  choroba [ Sun May 16, 2010 4:34 pm ]
Post subject:  Re: scripts which moves all files in certain DIR on dir up.

Just use the same find, but instead of moving, remove:
Code:
find -type d -name 'CD?' -exec rmdir {} \;

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com