Register
It is currently Fri Oct 31, 2014 3:53 am

Script to move contents of a folder


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Fri Mar 06, 2009 8:56 am   

Joined: Fri Mar 06, 2009 8:42 am
Posts: 5
Hi guys, I need to write a script to move any new files (it will be in cron executing every few minutes) from a list of directories to another similar list of directories. I tried to think of something on my own, but it seems my scripting skills are too basic for this, so I will need some help :]
The structure of the source and destination folders is kind of complicated, but I will try to explain it:

--==Source==--

Main Directory
-Location1
........-Type1
...............-file1
...............-file2
........-Type2
........-Type3
-Location2
........-Type1
........-Type2
...............-file3
........-Type3
-Location3
........-Type1
........-Type2
...............-file4
........-Type3
...............-file5



--==Destination==--

Main Directory
-Type1
........-Location1
..................-file1
..................-file2
........-Location2
........-Location3
-Type2
........-Location1
........-Location2
.................-file3
........-Location3
.................-file4
-Type3
........-Location1
........-Location2
........-Location3
.................-file5


The "Location" and "Type" fields are directories, their places are switched in the destination folder, but the files still have to be in the right combination of location/type. I tried geting the files with find(all the files are .jpg) but I dont know how to send them in the proper directory after that.
Any help is wellcome :]

Thanks

Al


Top
 Profile  
 PostPosted: Mon Mar 09, 2009 8:01 am   

Joined: Fri Mar 06, 2009 8:42 am
Posts: 5
I kinda did it on my own, and amazingly it worked :]
Just posting here if someone else needs it:

Code:
#!/bin/bash
source="/home/ftp/upload"
dest="/home/files/images"
for i in `ls "$source"`; do
for j in `ls "$source/$i"`; do
  for k in `ls "$source/$i/$j"`; do
  mv "$source"/"$i"/"$j"/$k "$dest/$j/$i/"
done
done
done


Top
 Profile  
 PostPosted: Mon Mar 09, 2009 1:52 pm   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Hi, couldn't help myself.

There is an easier way then using 3 for-loops to do this :)

You know that the directory depth is minimum 3 right?

Code:
#!/bin/bash
# You know what this is :)
source="/home/ftp/upload"
dest="/home/files/images"
# Just to make sure incase any directory contains a whitespace character, you can remove this safely if these doesn't exist
ifs="
"
# The magic find :) -mindepth is the trick
for i in $(find $source -mindepth 3 -type d -iname \*); do
    # create the new path
    tmp=$(echo $i | replace "$source" "$dest")
    # move the files from $i -> $tmp which is the new dest
    mv "$i" "$tmp"
done


I haven't tested this, but something like that should work. You can also specify -maxdepth with find incase you wish to lock yourself in a directory depth.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Wed Mar 11, 2009 4:53 am   

Joined: Fri Mar 06, 2009 8:42 am
Posts: 5
Thanks Frederik, I will definitely try this, looks a lot easier that way :)

Al


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP