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


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Rename all Files in a UNIX Directory from one date format to another date format
# 1  
Old 10-25-2013
Question 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 .Xls or .XLS is also possible. Now i need to convert such a file to date format which ends with yyyymmdd.

For eg E-SSS_Reporting_for_Small_Groups_10012013.xls needs to be renamed as E-SSS_Reporting_for_Small_Groups_20131001.xls

How can i accomplish this?

Appreciate your Help on this.
# 2  
Old 10-25-2013
shell script

Code:
DIR=/your/directory
for FILE in $(ls $DIR)
do
 OLD_DT=$(echo ${FILE##*_} | cut -d. -f 1)
 NEW_DT=$(expr substr $OLD_DT 5 4)$(expr substr $OLD_DT 1 4)
 RENAMED_FILE=$(echo ${FILE%_*})"_"${NEW_DT}"."$(echo ${FILE##*.})
 echo mv $DIR/$FILE $DIR/$RENAMED_FILE
done

Replace the red marked /your/directory with the directory name where you have the files.

After reviewing the output you may remove the red echo and execute for renaming files.
This User Gave Thanks to krishmaths For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

2. Shell Programming and Scripting

Change date format in am/pm in csv files using UNIX

Hi All, I'm new to forum good to hear all. I stuck in converting date format in csv file using unix csv file contains as below ,750,0000000000000000GCJR, ,06/22/2016 14:48:44 I want to convert into as below ,750,0000000000000000GCJR, ,06/22/2016 02:48:44 PM Please reply asap..... (22 Replies)
Discussion started by: Raghureds
22 Replies

3. UNIX for Dummies Questions & Answers

Make directory with todays date format

Not sure why this is not working. Please advice: #!/bin/sh DIR=`date +"%m-%d-%y"` echo $DIR ] && mkdir $DIR (2 Replies)
Discussion started by: iaav
2 Replies

4. Shell Programming and Scripting

Date Format in UNIX

Hi Guys, Need some help. I want to use the date command in Unix and having this below format as an output. the month should be in FIRST 3 LETTERS & ALL CAPS. And the DAY should be in two digits format DESIRED OUTPUT: 09-JUN-11 I tried to use this command below but still it does... (2 Replies)
Discussion started by: pinpe
2 Replies

5. UNIX for Dummies Questions & Answers

Changing from Excel date format to MySQL date format

I have a list of dates in the following format: mm/dd/yyyy and want to change these to the MySQL standard format: yyyy-mm-dd. The dates in the original file may or may not be zero padded, so April is sometimes "04" and other times simply "4". This is what I use to change the format: sed -i '' -e... (2 Replies)
Discussion started by: figaro
2 Replies

6. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

7. Shell Programming and Scripting

Unix date Format (MON)

Hi , Can any one give me what is the date format for 3 character Month all caps (MON) when I do date +%b it will give me Sep But I want this to be SEP (all caps) . Thanks in advance (1 Reply)
Discussion started by: umathurumella
1 Replies

8. UNIX for Dummies Questions & Answers

Alternative of format date in HP unix

HI, Does anyone know how the alternative way to format date to 'Jul07' in UNIX-HP . I found HP Unix does not support date -d....:( --------------------------------------------------------------- trxStartYear=2007 mthKey0=07 trxDay=20 key=`date -d ${trxStartYear}${mthKey0}${trxDay}... (2 Replies)
Discussion started by: epall
2 Replies

9. Shell Programming and Scripting

convert mmddyy date format to ccyyddd format??

hi, for reading a cobol indexed file i need to convert "mmddyy" date format to "ccyyddd" format. i checked the datecalc and other scripts but couldnt modify them to cater to my need:(... The datecalc gives an output which i believe is the total days till that date, but i want to convert it... (2 Replies)
Discussion started by: Bhups
2 Replies

10. UNIX for Dummies Questions & Answers

Unix Date Format

I have an Foxbase application that saves dates in a character field using a Unix date format. Does anyone know what the formula is to convert back to a standard date, something that can be, I hesitate to say, understood in a Windows environment, such as Excel or Access. cheers peter (2 Replies)
Discussion started by: sfpoet
2 Replies
Login or Register to Ask a Question