Operating System - HP-UX
1833847 Members
2223 Online
110063 Solutions
New Discussion

Re: how can do this date conversion?

 
SOLVED
Go to solution
gary_35
Advisor

how can do this date conversion?

i wanna make a shell to check core and if there is one core ,it will rename the core automatic, for example:
-rw-rw-rw- 1 pin pin 1 Mar 5 08:54 core

the core should be rename core.200303050854

i have no idea how can i do it in some simple way
4 REPLIES 4
Sridhar Bhaskarla
Honored Contributor
Solution

Re: how can do this date conversion?

Hi Gary,

I tried to write a script on the fly. Create dummy core files in subdirectories any test directory and run this against the directory. Make sure it works as you need before running it on the system.

-Sri


#!/usr/bin/ksh
if [ $# -ne 1 ]
then
echo "Usage:$0 Directory"
exit 1
fi

DIR=$1
chart="
Jan-01
Feb-02
Mar-03
Apr-04
May-05
Jun-06
Jul-07
Aug-08
Sep-09
Oct-10
Nov-11
Dev-12
"

move()
{
file=$1
Month=$(ls -al $file |awk '{print $6}')
DAY=$(ls -al $file |awk '{print $7}')
TimeYear=$(ls -al $file |awk '{print $8}')

for i in $chart
do
echo $i |grep $Month > /dev/null 2>&1
if [ $? = 0 ]
then
MON=$(echo $i |awk '{FS="-";print $2}')
fi
done

echo $TimeYear |grep ":" > /dev/null 2>&1
if [ $? = 0 ]
then
HR=$(echo $TimeYear|awk '{FS=":";print $1}')
MIN=$(echo $TimeYear|awk '{FS=":";print $2}')
YEAR=$(date |awk '{print $6}')
else
HR=00
MIN=00
YEAR=$TimeYear
fi

mv $file $file.${YEAR}${MON}${DAY}${HR}${MIN}
}

for FILE in $(find $DIR -name core)
do
move $FILE
done
You may be disappointed if you fail, but you are doomed if you don't try
Michael Steele_2
Honored Contributor

Re: how can do this date conversion?


For this format: filename.yyyymmddhhmm

DATE=$(date +%Y +%b +%d +%H +%M)

filename.$DATE
Support Fatherhood - Stop Family Law
monasingh_1
Trusted Contributor

Re: how can do this date conversion?

I will agree with jsut one liner date substituion.
Do a man on date and it will tell you more for varios output format...

Sridhar Bhaskarla
Honored Contributor

Re: how can do this date conversion?

Guys - Read his message. He is not trying to create a file. He is renaming a file with the it's (not current)datestamp as the extension.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try