Script moving files based on date | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Script moving files based on date

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-11-2012
rockingvj rockingvj is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 16 October 2014, 6:28 PM EDT
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Script moving files based on date

Hi,

I need a script that moves files based on date to a folder. The folder should be created based on file date. Example is :

Date file name
----- --------
Oct 08 07:39 10112012_073952.xls
Oct 09 07:39 10112012_073952.xls
Oct 10 07:39 10112012_073952.xls
Oct 11 07:39 10112012_073952.xls

and it should create like below :

Folder names should be like:

08Oct2012
09Oct2012
10Oct2012
11Oct2012

Files should be moved to respective folders based on date like :

08Oct2012 -->Oct 08 07:39 10112012_073952.xls
09Oct2012 -->Oct 09 07:39 10112012_073952.xls
10Oct2012 -->Oct 10 07:39 10112012_073952.xls
11Oct2012 -->Oct 11 07:39 10112012_073952.xls

Let me know the script how to achieve this.
Sponsored Links
    #2  
Old 10-11-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 23 September 2014, 12:09 PM EDT
Location: Houston, Texas, USA
Posts: 675
Thanks: 0
Thanked 203 Times in 195 Posts

Code:
#!/bin/ksh
typeset -R4 yy
for i in *.xls
do
  a=($(echo $i))
  y=${a[3]}
  y=${y##* }
  y=${y%%_*}
  yy=$y
  [[ -d ${a[1]}${a[0]}$yy ]] || mkdir ${a[1]}${a[0]}$yy
  mv -f "$i" ${a[1]}${a[0]}$yy
done

Sponsored Links
    #3  
Old 10-12-2012
rockingvj rockingvj is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 16 October 2014, 6:28 PM EDT
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Hi rdrtx1,

I'm getting error at line number 5 as :

./movefiles.sh[3]: syntax error at line 5 : `(' unexpected

I figured it out that
a=($(echo $i))

it should be like :

a=$(echo $i)
But I'm not getting desired output, Please check and tell me.
    #4  
Old 10-12-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 23 September 2014, 12:09 PM EDT
Location: Houston, Texas, USA
Posts: 675
Thanks: 0
Thanked 203 Times in 195 Posts

Code:
#!/bin/ksh
typeset -R4 yy
for i in *.xls
do
  echo $i | read m d y
  y=${y##* }
  y=${y%%_*}
  yy=$y
  dir=$d$m$yy
  [[ -d $dir ]] || mkdir $dir
  mv -f "$i" $dir
done

Sponsored Links
    #5  
Old 10-12-2012
rockingvj rockingvj is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 16 October 2014, 6:28 PM EDT
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Its still not working :

$ ./movefiles.sh
mkdir: cannot create 10112012_073952.xls
10112012_073952.xls: File exists
mv: 10112012_073952.xls.xls and 10112012_073952.xls are identical.

Just small question , the code given by you has :

#!/bin/ksh typeset -R4 yy for i in *.xls do echo $i | read m d y y=${y##* } y=${y%%_*} yy=$y dir=$d$m$yy [[ -d $dir ]] || mkdir $dir mv -f "$i" $dir done
echo $i | read m d y y=${y##* } ----------------> I think it should be m y=${y%%_*} ----------------> it should be d yy=$y
Let me know your comments.

Moderator's Comments:
Please use code tags next time for your code and data.

Last edited by vbe; 10-15-2012 at 11:17 AM..
Sponsored Links
    #6  
Old 10-14-2012
hedkandi's Avatar
hedkandi hedkandi is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 10 December 2012, 5:52 AM EST
Location: Doha, Qatar
Posts: 187
Thanks: 27
Thanked 9 Times in 8 Posts
Here's something else:


Code:
#!/bin/bash

MONTH="$(date +'%d''%b''%Y')"

FILES="$(date +'%d''%m''%Y')"

mkdir "${MONTH}"

mv ${FILES}_*.xls ${MONTH}

Sponsored Links
    #7  
Old 10-15-2012
rockingvj rockingvj is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 16 October 2014, 6:28 PM EDT
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Hi hedkandi,

According to you it will create the folder with current date. I want folder to be created based on file date and then file should move to the folder for which date it belongs.

Any help much appreciated.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Moving files from one directory to another based on 2 date variables dsfreddie Shell Programming and Scripting 11 06-21-2012 06:45 PM
Moving log files based on month - help with ksh shell script jusblues Shell Programming and Scripting 2 02-03-2012 01:59 PM
Moving Directories Based on Modified date lbargers UNIX for Dummies Questions & Answers 0 09-11-2010 01:12 PM
script to view files based on date krahuliyer Shell Programming and Scripting 6 10-05-2005 04:51 AM
Moving files based on creation date dgoyea UNIX for Dummies Questions & Answers 1 06-28-2001 05:43 PM



All times are GMT -4. The time now is 02:24 AM.