Computing.Net > Forums > Unix > Assign File modify time to a Var

Assign File modify time to a Var

Reply to Message Icon

Original Message
Name: Nish
Date: September 22, 2003 at 23:28:21 Pacific
Subject: Assign File modify time to a Var
OS: Solaris
CPU/Ram: 256
Comment:

Hi All,

I need to do develop a script in ksh to do the following.

Go to a particular directory (say /opt/alpha)
List files (using ls -ralt)
List the last updated file (ls -ralt|tail -1)
Assign the updated time (only hour) to a variable. (myvar)
eg:
owner group 512 10:22 test_file.log

$myvar should be 10


Appreciate your advise.

Thanks ,

Nish.



Report Offensive Message For Removal


Response Number 1
Name: David Perry
Date: September 23, 2003 at 05:31:34 Pacific
Subject: Assign File modify time to a Var
Reply: (edit)

Pipe your results through

| sed -e 's/:/ /' | awk '{ print $4 }'

myvar=`ls -ralt | tail -1 | sed -e 's/:/ /' | awk '{ print $4 }'`


Report Offensive Follow Up For Removal

Response Number 2
Name: WilliamRobertson
Date: September 24, 2003 at 00:42:59 Pacific
Subject: Assign File modify time to a Var
Reply: (edit)

Or just for fun, you could do it all with only ls and awk:

integer myvar=$(ls -ltr | awk '{timestamp=$8} END {split (timestamp,timevalues,":"); print timevalues[2]}')


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Assign File modify time to a Var

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software