4

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

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • Perhaps stat isn't installed ? It's normally part of the coreutils package. – steve Jun 01 '16 at 20:09
  • 1
    See here http://unix.stackexchange.com/questions/35183/how-do-i-identify-which-linux-distro-is-running – steve Jun 01 '16 at 20:16
  • If you are using Solaris, chances are you can accomplish what you want by using flag options with ls. For example ls -siv -@ -/ c -%all z displays file creation time, time stamps, access, etc. Solaris-specific ls is a powerful tool, and it essentially replaces stat, along with many other basic commands. –  Jun 01 '16 at 23:19

3 Answers3

6

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]'
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
1

Pretty sure your error is meant to indicate that your shell:

/usr/bin/ksh:

Can't find "stat":

stat: not found

Does your operating system provide the "stat" command? You'll need to provide more information on your operating system and it's version for further guidance.

You may also have a problem in your "PATH" environment variable.

0

It depends completely on your system, which you did not specify. An alternative to installing the binary provided by your package manager is to compile the program from source. To do so, you must first download the coreutils source (coreutils-8.0 at the time of writing).

If it is in an archive, unarchive it and then enter into the directory of the source. Run the configure script with the command ./configure, and then wait for it to finish. After configure has completed (with no major errors), then run make, with the command make.

The time it takes for make to complete can be anywhere from 30 seconds to literally days, depending on the size and complexity of the application. In this case however, it is highly unlikely for the make step to take over a few minutes.

Once make is done compiling the programs (without errors), install the programs with the command make install, or if you are not the root user sudo make install. This should only take a few seconds, although in some cases it can take longer. Once completed, you can exit the coreutils directory, and then proceed to delete it.

Notes

  • The only requirements for this to work (along with the proper libraries and dependencies*) are GCC or CC and the make utility.
  • To run all these steps with one command, you can enter ./configure && make && make install.
  • The complete list of dependencies for coreutils is: Bash, Binutils, Coreutils, Diffutils, GCC, Gettext, Glibc, Grep, Make, Perl, an Sed.

References

For more information, see these pages:

slm
  • 369,824