Reputation: 77
I need to display file’s name, size, number of hard links, owner, and modify date (in that order).
#!/bin/csh
echo Enter file name
set filename = $<
if(-f $filename)then
if(-z $filename)then
rm $filename
else
clear
echo $filename
stat -c %s $filename
stat -c %h $filename
stat -c %U $filename
stat -c %y $filename
endif
endif
Everything works (I think) except it shows more than just the date. What command would I use to just display the date of last modification?
Upvotes: 1
Views: 1553
Reputation: 124734
It seems you are looking for this:
stat -c %y $filename
Or this:
date -r $filename
Or this (most portable):
fn=$filename perl -e 'print scalar localtime((stat("$ENV{fn}"))[9])'
On the other hand, stat -c %y
was already in your post, and you wrote:
Everything works (I think) except it shows more than just the date. What command would I use to just display the date of last modification?
Which makes me think that perhaps you want only the date part? One (lazy) solution for that is using shell commands like cut
, awk
, sed
to extract just what you need, for example:
$ stat -c %y sample.txt
2013-10-12 09:24:08.096820646 -0700
$ stat -c %y sample.txt | cut -f1 -d' '
2013-10-12
A better solution is to generate dates in the desired format, but that depends on the command you used. For example stat
cannot do this, but date
and perl
can, for example:
date +%Y-%m-%d -r $filename
Upvotes: 0
Reputation: 9413
See the man stat
.
You need to use a
, m
, c
or B
which gives the time file was last accessed or modified.
Upvotes: 1