PDA

View Full Version : Bash script: stat use?



EvilMarshmallow
March 25th, 2008, 08:46 PM
I'm trying to write a script and I need to get a file's last modification time. I can't figure out how to construct the statement I need, which I think is some form of the stat command.

I want only mm/dd/yy hh:mm returned, in 24-hour format. I must have googled a hundred pages but I can't figure it out, and the man page doesn't explain how to construct the statement (at least not for a noob like me).

Can anyone help?

scragar
March 25th, 2008, 08:59 PM
try:


ls -l --time-style=full-iso filename | grep -o "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]" from a terminal. I'll convert this into a simple shell script that can just be run anywhere(I would use the {4} syntax to shorten the code, but for some reason grep for me was hopping between ereg and normal reg methods, which caused problems).

scragar
March 25th, 2008, 09:05 PM
ok, this code works fine from a terminal:

#!/bin/bash

ls -l --time-style=full-iso $1 | grep -o "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]"

sleep 5
and you can set it to a right click menu option, launching it using the command:

gnome-terminal -x PATH TO SCRIPT

WW
March 25th, 2008, 09:08 PM
stat can do what you want:


stat -t "%D %R" -f "%Sm" filename

(Obvious, right? :) )

EvilMarshmallow
March 25th, 2008, 09:35 PM
I tried the stat example by WW above... I get "cannot read file system informatio nfor '%D %R' and again for '$Sm', then I get the terse version of the stat command.

Does Ubuntu not use the standard implementation of stat?

WW
March 25th, 2008, 10:11 PM
Sorry. Lately I've been on a Mac more than Ubuntu. The command I gave above works on the Mac, but the stat command in Ubuntu has very different options. In the words of the immortal Emily Latella: "Oh. That's different. Never mind!"

nanotube
March 25th, 2008, 10:35 PM
well, on ubuntu, the following seems to produce the desired result, e.g.:

$ stat -c '%z' .Xauthority
2008-02-20 20:17:57.000000000 -0500

then all you need to do is cut it off where you want by any method you prefer. e.g.:

$ expr substr "$(stat -c '%z' .Xauthority)" 1 16
2008-02-20 20:17

(obviously substitute in the file you are interested in where you see ".Xauthority")

ghostdog74
March 26th, 2008, 06:13 AM
-z of stat command is time of last change. Use %y for last modification


# stat -c "%y" file | awk 'BEGIN{FS="[ .:]"}{gsub("-","/",$1);}{print $1,$2":"$3}'
2008/03/26 10:34

mssever
March 26th, 2008, 07:19 AM
I would use the {4} syntax to shorten the code, but for some reason grep for me was hopping between ereg and normal reg methods, which caused problems.
Use egrep instead of grep for anything but the most trivial regular expressions.

nanotube
March 26th, 2008, 07:07 PM
-z of stat command is time of last change. Use %y for last modification


# stat -c "%y" file | awk 'BEGIN{FS="[ .:]"}{gsub("-","/",$1);}{print $1,$2":"$3}'
2008/03/26 10:34


nice awk use. now... where i come from, modification is just a synonym for change. :) while i was man-ing stat, i saw the two options, but had no clue why there were two different ones so just picked one. so could you please enlighten me what the difference between the two is, in the context of stat?

ghostdog74
March 27th, 2008, 01:53 AM
nice awk use. now... where i come from, modification is just a synonym for change. :) while i was man-ing stat, i saw the two options, but had no clue why there were two different ones so just picked one. so could you please enlighten me what the difference between the two is, in the context of stat?

meta-data such as owner, group etc are stored in the inode. when you do chmod, chown etc on a file, you modify its last change time. when you change the actual data of the file itself, you changed its modification time.

nanotube
March 28th, 2008, 05:00 AM
meta-data such as owner, group etc are stored in the inode. when you do chmod, chown etc on a file, you modify its last change time. when you change the actual data of the file itself, you changed its modification time.

thanks, that was useful info! :)