Rename old files with last modification date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename old files with last modification date
# 1  
Old 11-03-2010
Rename old files with last modification date

Hi everyone,

I have files like file1_Mod.txt, file2_Mod.txt. I want to rename the old files with the last modification date. I write the below script to rename with current date, but I donīt know how to use "date -r" to get the last modification date with the same format I have below (dd-mm-yyyy_hhmmss).

The ouput I would like is:
file1_Old_dd-mm--yyyy_hhmmss.txt
file2_Old_dd-mm--yyyy_hhmmss.txt

Script what I have so far.
Code:
for each in *Mod.txt;
do
mv $each $(basename $each Mod.txt)Old_$(date +"%d-%b-%Y_%H%M%S").txt
done

May somebody help me with this issue.

Thanks in advance.
# 2  
Old 11-03-2010
Use:
Code:
$(date -r $each +"%d-%b-%Y_%H%M%S")

# 3  
Old 11-03-2010
Hi Chubler_XL,

I replace follow your suggestion and it works nice. Thank you for your help.

The only thing is I get errors like shown below when the file has spaces in the name.
The only way to fix this is replace spaces (" ") with underscores ("_") or is there other way using basename, each?

Code:
$ for each in *Mod.txt;
do
mv $each $(basename $each Mod.txt)Old_$(date -r $each +"%d-%b-%Y_%H%M%S").txt
done
basename: extra operand `21-09-2009_Mod.txt
Try `basename --help' for more information.
date: extra operand `21-09-2009_Mod.txt'
Try `date --help' for more information.
mv: target `Old_.txt' is not a directory

Thanks for your help so far.
# 4  
Old 11-03-2010
Code:
for each in *Mod.txt;
do
  NEWNAME=$(basename "$each" Mod.txt)Old_$(date -r "$each" +"%d-%b-%Y_%H%M%S")
  mv "$each" "$NEWNAME"
done

# 5  
Old 11-04-2010
Code:
for each in *Mod.txt;
do
  NEWNAME=${each%_*}_Old_$(date -r $each +"%d-%b-%Y_%H%M%S").txt
  mv "$each" "$NEWNAME"
done

# 6  
Old 11-04-2010
Hi Chubler_XL, thanks for your reply, it works now even if the file has spaces within its name, thank you.

Hi rdcwayx, thanks for your help too, I`ve tested your script and I`m not sure what is missing, I`ve made changes trying to fixit but is failing when the files has spaces in its name.

Thanks anyway.

Best regards
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort and move multiple files by modification date

Hi I have a problem, I have a large group of archive files in a folder some are later versions of the same archive, the only difference btween them is that the archiving program we use appends the name with a code for it to keep track of in its data base, and the modification date. I am starting... (6 Replies)
Discussion started by: Paul Walker
6 Replies

2. UNIX for Dummies Questions & Answers

Rename all Files in a UNIX Directory from one date format to another date format

Hi Unix Gurus, I would like to rename several files in a Unix Directory . The filenames can have more than 1 underscore ( _ ) and the last underscore is always followed by a date in the format mmddyyyy. The Extension of the files can be .txt or .pdf or .xls etc and is case insensitive ie... (1 Reply)
Discussion started by: pchegoor
1 Replies

3. Shell Programming and Scripting

Move files from one directory to another based on creation/modification date

Hi All, Really stuck up with a requirement where I need to move a file (Lets say date_Employee.txt--the date will have different date values like 20120612/20120613 etc) from one directory to another based on creation/modification dates. While visiting couple of posts, i could see we can... (3 Replies)
Discussion started by: dsfreddie
3 Replies

4. Shell Programming and Scripting

Help with script to copy/rename files, then delete by date

Hi All, I am new to scripting and am looking for some assistance setting up a script. Basically I need the script to scan a folder for the newest files and make a copy of those files, adding a month to the date stamp. I also need this script to delete the previously copied files to save space.... (4 Replies)
Discussion started by: Lucid13
4 Replies

5. Linux

rename files in a folder with date&time stamp

Hi, I want to rename all the files (more than 100 files) in a fodler to another folder with date&time stamp. foe eg, file1.dat file2.dat file3.dat .. to be renamed as file1100629_16_30_15.txt (yy-mon-dd_hh_mi_ss) file1100629_16_30_16.txt .. so on (2 Replies)
Discussion started by: feroz
2 Replies

6. UNIX for Advanced & Expert Users

Help with sorting files according to modification date

Hi, I was very surprised to not be able to find an answer to this question despite my best efforts in Google and elsewhere. Maybe it's a good thing as it forced me to finally become a member in this great forum that i use frequently. Ok my question: I want to be able to sort files inside a... (3 Replies)
Discussion started by: stavros
3 Replies

7. Shell Programming and Scripting

List the file or files with last modification date

hi. I need help my programing friends :p I need to list all the files with a certain name (for example FileName) by last modification date but only the one with the last date. If there are two files with the same name and same modification date it should print the both. For example in this set... (6 Replies)
Discussion started by: KitFisto
6 Replies

8. Shell Programming and Scripting

Copy files based on modification date

How to copy files from a location to a directory <YYMM> based on last modification date? This will need to run daily. I want to copy those file for May to 0905 and Jun to 0906. Appreciate your guidance.:) Thanks. -rw-rw-rw- 1 ttusr tgrp 4514 May 29 21:49 AB24279J.lot_a... (17 Replies)
Discussion started by: KhawHL
17 Replies

9. UNIX for Dummies Questions & Answers

script to rename files with current date and copy it.

I have few webservers logs like access.log. which would be growing everyday. what i do everyday is, take the backup of access.log as access.log_(currentdate) and nullify the access.log. So thought of writing a script... but stuck up in middle. My requirement: to take the backup and nullify... (6 Replies)
Discussion started by: logic0
6 Replies
Login or Register to Ask a Question