Adding date and time to file name | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Adding date and time to file name

Shell Programming and Scripting


Tags
directory, multiple files, renaming a file

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-15-2010
cc_at_work cc_at_work is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 12 January 2011, 8:20 PM EST
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Adding date and time to file name

Hi All,

i wanted to add date and time to the file names in the same directory so lets say a file in the directory is test.txt then after running the shell script it should be test-15-11-2010.txt.

So I used the following script which works,

Code:
#!/bin/bash

thetime=`date +%Y-%m-%d--%H:%M:%S` #
for i in *.txt
do 
      mv $i ${i%.txt}$(date "+_%Y%m%d.txt")
done

But it does work only for .txt files I want to modify it so that it can take any file extension.

Thank you.

Last edited by Scott; 11-15-2010 at 01:49 AM.. Reason: Please use code tags
Sponsored Links
    #2  
Old 11-15-2010
michaelrozar17's Avatar
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 17 March 2014, 1:43 AM EDT
Location: IN
Posts: 892
Thanks: 14
Thanked 182 Times in 173 Posts
You could use (assuming this shell script is at the path where you need to add the date and time )
Code:
for i in *

or to be more precise
Code:
for i in *.*

As the former one would change the name of the sub-directory if its present in the current working dir.
Sponsored Links
    #3  
Old 11-15-2010
cc_at_work cc_at_work is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 12 January 2011, 8:20 PM EST
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
yea thankx for reply.. but yea that i could figure out but the main part is.

Code:
mv $i ${i%.txt}$(date "+_%Y%m%d.txt")"

here i have hard coded .txt.. that is what i want to change..

Last edited by Scott; 11-15-2010 at 01:50 AM.. Reason: Code tags
    #4  
Old 11-15-2010
michaelrozar17's Avatar
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 17 March 2014, 1:43 AM EDT
Location: IN
Posts: 892
Thanks: 14
Thanked 182 Times in 173 Posts
Store the file's extension in a variable before mv command and substitute in the variable later while using mv command.


Code:
#!/bin/bash

thetime=`date +%Y-%m-%d--%H:%M:%S` #
for i in *.*
do 
	extn=${i##*.} # save the extension of the file
	mv $i ${i%.*}$(date "+_%Y%m%d.${extn}")
done

The Following User Says Thank You to michaelrozar17 For This Useful Post:
cc_at_work (11-15-2010)
Sponsored Links
    #5  
Old 11-15-2010
cc_at_work cc_at_work is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 12 January 2011, 8:20 PM EST
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
awesome! thankx mate! it did work!

Sorry I am a beginner in shell. I wanted to handle files without extension as well... as of now it works for text.txt or anything which has file extension but if some file name is just "debug" it wont work for that.. So i tried to write another for loop with just * instead of*.* but ofcourse it dint work.. any ideas how should i proceed.
thank you,.
Sponsored Links
    #6  
Old 11-15-2010
frans's Avatar
frans frans is offline Forum Advisor  
Registered User
 
Join Date: Oct 2009
Last Activity: 4 December 2013, 9:39 AM EST
Location: France
Posts: 839
Thanks: 4
Thanked 54 Times in 53 Posts
Quote:
Originally Posted by michaelrozar17 View Post
Store the file's extension in a variable before mv command and substitute in the variable later while using mv command.
Why store it? (but it's preferable to store the date string to avoid calls to date in the iteration.

Code:
#!/bin/bash
D=$(date +%d-%m-%Y)
for F in *.*
do 
	mv $F ${F%.*}-$D.${F##*.}"
done


Last edited by frans; 11-15-2010 at 03:18 AM.. Reason: extra ')' removed
The Following User Says Thank You to frans For This Useful Post:
cc_at_work (11-15-2010)
Sponsored Links
    #7  
Old 11-15-2010
cc_at_work cc_at_work is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 12 January 2011, 8:20 PM EST
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by frans View Post
Why store it? (but it's preferable to store the date string to avoid calls to date in the iteration.

Code:
#!/bin/bash
D=$(date +%d-%m-%Y)
for F in *.*
do 
	mv $F ${F%.*}-$D.${F##*.}")
done

how about for files without any extension.. like lets say just "debug" and not "debug.txt" ? any idea?
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
compare file modification date/time to current date/time Bill Ma Shell Programming and Scripting 5 08-27-2009 12:02 PM
Adding Date & time stamps to filename manish.s UNIX for Dummies Questions & Answers 1 03-13-2009 04:00 AM
Adding date and time to a log file ltodd2 UNIX for Dummies Questions & Answers 2 12-15-2008 12:37 PM
problem with displaying date and adding time FuncMx Shell Programming and Scripting 1 10-10-2008 12:06 AM
Processing a log file based on date/time input and the date/time on the log file primp Shell Programming and Scripting 4 03-16-2008 11:23 AM



All times are GMT -4. The time now is 01:39 AM.