Script moving files based on date


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script moving files based on date
# 1  
Old 10-11-2012
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.
# 2  
Old 10-11-2012
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

# 3  
Old 10-12-2012
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
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

# 5  
Old 10-12-2012
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:
Mod Comment Please use code tags next time for your code and data.

Last edited by vbe; 10-15-2012 at 12:17 PM..
# 6  
Old 10-15-2012
Here's something else:

Code:
#!/bin/bash

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

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

mkdir "${MONTH}"

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

# 7  
Old 10-15-2012
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving old files based on pattern

Hi all I am trying to loop through a directory of files using a given search pattern. some of the files will be duplicated due to the pattern, but of the duplicate files i wanted to move the older files to another location. Is there any straightforward way of doing this ? One of ways I... (1 Reply)
Discussion started by: sthapa
1 Replies

2. Shell Programming and Scripting

Moving files based on file name

Hi All, I have multiple files in the folder, I want to move those files into the other folder on based of name File names: Template_server1_01==> Template_server1_02==>To one directory /Server1 Template_server1_03==> Template_server2_01==> Template_server2_02==>To one... (9 Replies)
Discussion started by: sharsour
9 Replies

3. Shell Programming and Scripting

Moving files from one directory to another based on 2 date variables

Hi All, I am currently coding for a requirement(LINUX OS) where I am supposed to move a file (Lets Call it Employee.txt) from Directory A to Directory B based on 2 date fields as below, Date_Current = 20120620 Date_Previous = 20120610 Source Directory : /iis_data/source Target... (11 Replies)
Discussion started by: dsfreddie
11 Replies

4. UNIX for Dummies Questions & Answers

moving files based on condition

hi i have to move files and send an email and attached the bad files to inform the developer about that. #!/bin/ksh BASE_DIR=/data/SrcFiles cd $BASE_DIR ## finding the files from work directory which are changed in 1 day find -type f -name "*.csv" –ctime 0 > /home/mydir/flist.txt ##... (14 Replies)
Discussion started by: awais290
14 Replies

5. Shell Programming and Scripting

Moving log files based on month - help with ksh shell script

Hello, I'm trying to move the log files from the parent directory to respective monthly folder and I would be running this script on a weekly basis through cron. I'm new to this scripting and here is what i could come up and it runs without really doing anything. I even tried placing echo... (2 Replies)
Discussion started by: jusblues
2 Replies

6. UNIX for Dummies Questions & Answers

Moving Directories Based on Modified date

Hi, How can I move directories (and all sub directories/files) from one directory to another based on the modified date of the directory? Currently the existing structure looks like this: /public_html/media/videos/tmb/34947/image1.jpg /public_html/media/videos/tmb/34947/image2.jpg ... (0 Replies)
Discussion started by: lbargers
0 Replies

7. Shell Programming and Scripting

script to search context from 2 files based on date and name

Please Close thread- Thank You. (1 Reply)
Discussion started by: ezmethod
1 Replies

8. Shell Programming and Scripting

Sorting Files by date and moving files in date order

I need to build a k shell script that will sort files in a directory where files appear like this "XXXX_2008021213.DAT. I need to sort by date in the filename and then move files by individual date to a working folder. concatenate the files in the working folder then start a process once... (2 Replies)
Discussion started by: rebel64
2 Replies

9. Shell Programming and Scripting

script to view files based on date

I have a UNIX box where in files are created every 5 minutes. I need to write a shell script which will take in the date as parameter and return me all files created on that day. Also I will like it to show me only files which have a size greater than 0 i.e. non-empty files. how do I go abt this? I... (6 Replies)
Discussion started by: krahuliyer
6 Replies

10. UNIX for Dummies Questions & Answers

Moving files based on creation date

Howdy, I'm trying to figure out how to move multiple files based on their creation date. If anyone can enlighten me it would be most appreciated!! Thanks! :D (1 Reply)
Discussion started by: dgoyea
1 Replies
Login or Register to Ask a Question