nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

How can I rename a file by its date?

This is a discussion on How can I rename a file by its date? within the Linux software forums, part of the Linux Getting Started category; I have here a simple question. I have a file named "log.txt"and the date modified is Mar 29, 2006 16:55 ...


Go Back   nixCraft Linux Forum > Linux Getting Started > Linux software

Linux answers from nixCraft.


Linux software General questions and discussion about Redhat/Fedora Core/Cent OS, Debian and Ubuntu Linux related to softwares should go here.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 29-03-2006, 01:52 PM
Member
User
 
Join Date: Jun 2005
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
warren
Default How can I rename a file by its date?

I have here a simple question.

I have a file named "log.txt"and the date modified is Mar 29, 2006 16:55
I am planning to rename/create link of "log.txt" to "Mar 29, 2006 1655.txt"
The problem is how can I get that string.

Sol. 1
-bash-3.00# stat --format=%y log.txt
2006-03-29 16:55:41.571766000 +0800
But the problem is how can I convert that to string "Mar 29, 2006 1655"

Sol 2.
-bash-3.00# ls -l --time-style="+%b %e, %Y %k%M" log.txt
-rw-rw-rw- 1 root root 0 Mar 29, 2006 1655 log.txt
I have that string but the problem is how can I filter that string.

Thanks in advance for any help
warren
Reply With Quote
  #2 (permalink)  
Old 29-03-2006, 02:20 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Both stat and ls -l are not suitable for this job. Use date command formatting.

Code:
echo $(date +"%b-%d-%y").log.txt
More examples
Code:
NOW=$(date +"%b-%d-%y")
cp log.txt "$NOW.log.txt"
ls *.txt
See our article Creating report/log file names with date in filename for more info http://www.cyberciti.biz/nixcraft/vi...-reportlog.php
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 29-03-2006, 03:29 PM
Member
User
 
Join Date: Jun 2005
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
warren
Default

thanks for this great idea.
but sad to say that it might not really fit into what i'm planning
the "date" command will get the current time and append it to the filename prior to creating and in my case the files are already created.

There is a third party software that will create log1.txt, log2.txt, and so on and so forth and in no way I can change that and if I change that, the application will break.
I'm planning to create a link between that log files to another file where its filename equal to its modified date. In this case it much easier to tell which log I want to look.

Anyway you have given me a good idea. I'll look into the application if I can put this as an arguement. By the time the application creates the log file at the same time another log file is created but with the date as its prefix.
thanks.....
Reply With Quote
  #4 (permalink)  
Old 29-03-2006, 03:51 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

My bad I did not read it your request correctly. Any ways here is the solution

Code:
ls -l --time-style="+%b %e, %Y %k%M" log.txt  | awk '{ print $6"-"$7"-"$8 }' | sed -e 's/,//g'
And here is what you need to do at a shell prompt:
Code:
NOW=$(ls -l --time-style="+%b %e, %Y %k%M" log.txt  | awk '{ print $6"-"$7"-"$8 }' | sed -e 's/,//g')
cp log.txt $NOW.log.txt
ls *.txt
Or better put in script (vi script.sh) :
Code:
#!/bin/bash
FILE=$1
NOW=$(ls -l --time-style="+%b %e, %Y %k%M" $FILE  | awk '{ print $6"-"$7"-"$8 }' | sed -e 's/,//g')
cp $FILE "$NOW.$FILE"
echo "File $FILE == $NOW.$FILE"
Run script as follows:
Code:
chmod +x script.sh
./script.sh file.txt
./script.sh log.txt
Enjoy!
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #5 (permalink)  
Old 30-03-2006, 11:04 AM
Member
User
 
Join Date: Jun 2005
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
warren
Default

You got it! thank you so much.
I should have study awk. After some basic courses of awk, i've arrived to something like this:

ln -s log0.txt "`ls -ld --time-style="+%b %e, %Y %k%M%S" log0.txt | awk '{ print $6" "$7" "$8" "$9}'`"

the output would be:
Mar 30, 2006 122104

thanks....
Reply With Quote
  #6 (permalink)  
Old 30-03-2006, 12:11 PM
rockdalinux's Avatar
Is that all you got?
User
 
Join Date: May 2005
Location: Planet Vegeta
OS: Redhat
Posts: 708
Thanks: 15
Thanked 19 Times in 18 Posts
Rep Power: 10
rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light
Default

LOL @ nixcraft, you always miss the bus first time...

Awk and sed both are real nifty tools. In addition, one can only master them by observing code like this

PS: nixCraft. Btw your Cyborg rocks
__________________
Rocky Jr.
What's wrong? I hope I am not making you uncomfortable...

Never send a boy to do a mans job.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Shell date command for manipulation and formatting date newbie4 Shell scripting 4 28-10-2008 07:33 PM
How to rename a file in Linux raj Linux software 0 22-04-2008 02:00 AM
How to calculate date minus 72 hours? svinopas Shell scripting 1 09-10-2007 09:37 PM
how to rename eth0 to eth1 deltamails Networking, Firewalls and Security 1 24-09-2007 07:53 PM
script to ping + date dendi_rm Shell scripting 3 11-09-2007 10:42 AM


All times are GMT +5.5. The time now is 09:39 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38