Register
It is currently Fri Oct 24, 2014 1:01 am

Moving files based on date


All times are UTC - 6 hours


Post new topic Reply to topic  [ 11 posts ] 
Author Message
 PostPosted: Sun Dec 13, 2009 2:36 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
I'm doing a very simple script to archive some files.

So far it creates a dir using current date in format dd-mm-yyyy.

All the files in root dir have the layout of picture dd-mm-yyyy hh-mm-ss.jpeg

What I need to be able to do is mv all the files with current date into the newly created dir

ie mv picture %d-%m-%y * /%date

Any help would be appriciated


Top
 Profile  
 PostPosted: Sun Dec 13, 2009 8:15 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
Code:
DATE=$(date +'%d-%m-%Y')
mv picture${DATE}*.jpeg $DATE/


Top
 Profile  
 PostPosted: Sun Jan 31, 2010 3:33 pm   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
That code throws up an error however if I do this command via CLI

mv picture\ 29-01-2010\ * 29-01-10/

it works fine how do I interperate that into script code?


Top
 Profile  
 PostPosted: Tue Feb 02, 2010 5:47 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
the code suggested is not proof vs spaces in the filenames. adding quotes like below may help

Code:
DATE=$(date +'%d-%m-%Y')
mv "picture${DATE}*.jpeg" "$DATE/"


Top
 Profile  
 PostPosted: Thu Apr 01, 2010 1:39 pm   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
DarthWavy wrote:
the code suggested is not proof vs spaces in the filenames. adding quotes like below may help

Code:
DATE=$(date +'%d-%m-%Y')
mv "picture${DATE}*.jpeg" "$DATE/"


Just been playing with this again and now i'm getting

mv: cannot stat `picture01-04-2010*.jpeg': No such file or directory

It appears it's not setup correctly for the space in between picture and the date


Top
 Profile  
 PostPosted: Sat Apr 03, 2010 11:16 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
OK got the space sorted but having problem with time bit, as all times are different I just need it to move the files based on the date so something like

mv picture (date) *.* /date ie ignore the time bit


Top
 Profile  
 PostPosted: Tue Jun 08, 2010 8:10 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
i've got it working so it ignores the time bits, i'm currently got it creating dirs using

Code:
mkdir $date


However i'd like to have a word before the date ie Folder $date but having problems with the space between the 2.

Any ideas?


Top
 Profile  
 PostPosted: Tue Jun 08, 2010 9:44 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
it is stated here and there, that, even it's possible, having spaces, and/or carriage returns (why not tabs) in {file,dir}names is a bad thing.
Code:
Date=$(date)
mkdir Directory\ $Date
mv "picture $Date "* "Directory $Date/"
still I wonder why you want to deal with those spaces :(


Top
 Profile  
 PostPosted: Tue Jun 08, 2010 1:19 pm   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
Yes I know and i'm beginning to agree with you =)) so i've changed it slightly

I seem to have balls up my script a bit as it's moving all files into $date not just the ones with $date in the filename?

Code:
mv "picture $date" * "$date/"


mv: cannot stat `picture 08-06-2010': No such file or directory
mv: cannot move `08-06-2010' to a subdirectory of itself, `08-06-2010/08-06-2010'


Top
 Profile  
 PostPosted: Tue Jun 08, 2010 2:03 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
the asterisk has to be next to closing quote.

it is double-quote picture space $date space double-quote asterisk

you might use rename, first:
Code:
rename 's/ /_/g' picture*


Top
 Profile  
 PostPosted: Tue Jun 08, 2010 2:16 pm   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
sussed it, excellent, thanks for your help =D> =D>


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 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:  


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