nixCraft Linux Forum

nixCraft

Linux 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

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 03-29-2006, 02:52 PM
Member
User
 
Join Date: Jun 2005
Posts: 45
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
Sponsored Links
  #2 (permalink)  
Old 03-29-2006, 03:20 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,061
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 | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #3 (permalink)  
Old 03-29-2006, 04:29 PM
Member
User
 
Join Date: Jun 2005
Posts: 45
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 03-29-2006, 04:51 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,061
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 | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #5 (permalink)  
Old 03-30-2006, 12:04 PM
Member
User
 
Join Date: Jun 2005
Posts: 45
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 03-30-2006, 01:11 PM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
My distro: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 581
Rep Power: 7
rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough
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.
You may have my body & soul, but you will never touch my pride!

If you have knowledge, let others light their candles at it.

Certified to work on HP-UX / Sun Solaris / RedHat
Reply With Quote
Reply

Bookmarks


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 On

Similar Threads

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


All times are GMT +5.5. The time now is 12:22 AM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

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