Linux / UNIX Tech Support Forum
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 ...
|
|||||||
| Linux software General questions and discussion about Redhat/Fedora Core/Cent OS, Debian and Ubuntu Linux related to softwares should go here. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Sponsored Links | ||
|
|
|
||||
|
Both stat and ls -l are not suitable for this job. Use date command formatting.
Code:
echo $(date +"%b-%d-%y").log.txt Code:
NOW=$(date +"%b-%d-%y") cp log.txt "$NOW.log.txt" ls *.txt
__________________
Vivek Gite Linux Evangelist |
|
|||
|
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..... |
|
||||
|
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'
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
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"
Code:
chmod +x script.sh ./script.sh file.txt ./script.sh log.txt
__________________
Vivek Gite Linux Evangelist |
|
|||
|
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.... |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |