Stat command not found

kshsolarisstat

I want to execute stat command in my unix shell /usr/bin/ksh:

Input:

/bin/date +%Y%m%d%H%M%S -d "$(/usr/bin/stat -c %x find.txt)"

And the output:

/usr/bin/ksh: stat:  not found

My system:
SunOS 5.10 Generic_150400-23 sun4v sparc sun4v

Best Answer

The stat command is not standard. There's one on Linux, a more restricted one on embedded Linux, one with completely different options on FreeBSD and OSX, and none on most other Unix variants such as Solaris, AIX, and HP-UX. Your syntax looks like it's intended for Linux's stat.

You're apparently running a system without stat. You probably don't have date -d either then.

The only portable way to list a file's access time is with ls.

ls -log -u find.txt

This gives less precise output that what you need, in a cumbersome format.

If you can install GNU coreutils, do so and use its stat and date commands. Many modern Unix variants have an easy way to install GNU utilities.

Alternatively, use Perl, which is very often installed on Unix systems. Call stat to read the file's timestamp and localtime to break the timestamp into date and time parts.

perl -e '@stat = stat($ARGV[0]); @time = localtime($stat[9]); printf "%04d%02d%02d%02d%02d%02d\n", $time[5]+1900, @time[4,3,2,1,0]'
Related Question