|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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.
Last edited by vbe; 10-15-2012 at 11:17 AM.. |
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|