Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix上的文件修改时间(秒)_Unix_File_Time - Fatal编程技术网

Unix上的文件修改时间(秒)

Unix上的文件修改时间(秒),unix,file,time,Unix,File,Time,在Unix上,是否有显示文件修改时间的命令,精确到秒 在Linux上,这可以通过一个“stat-c%y”轻松完成,它返回类似于2009-11-27 11:36:06.000000000+0100。我在Unix上找不到类似的工具。根据我的Mac电脑(具有BSD标准版stat)上的手册页,您可以在几秒钟内通过以下工具获得修改的历元时间版本: stat -f %m /etc/passwd 或者,如果您想在小时:分钟:秒内打印出来,您可以这样做: perl -e "print scalar(local

在Unix上,是否有显示文件修改时间的命令,精确到秒


在Linux上,这可以通过一个“stat-c%y”轻松完成,它返回类似于
2009-11-27 11:36:06.000000000+0100
。我在Unix上找不到类似的工具。

根据我的Mac电脑(具有BSD标准版stat)上的手册页,您可以在几秒钟内通过以下工具获得修改的历元时间版本:

stat -f %m /etc/passwd
或者,如果您想在小时:分钟:秒内打印出来,您可以这样做:

perl -e "print scalar(localtime(`stat -f %m /etc/passwd`))"

对于任何面临同样问题的人,我都找不到解决方案(无论如何,在HP-UX 11i上)。 最终为我的需求编写了一个个性化的“ls-lh”。没那么难。。 打印如下内容:

 - 664 rw-/rw-/r--   1L expertNoob adm   8.37 kB 2010.08.24 12:11:15 findf1.c
 d 775 rwx/rwx/r-x   2L expertNoob adm     96 B  2010.08.24 15:17:37 tmp/
 - 775 rwx/rwx/r-x   1L expertNoob adm     16 kB 2010.08.24 12:35:30 findf1
 - 775 rwx/rwx/r-x   1L expertNoob adm     24 kB 2010.09.14 19:45:20 dir_info
 - 444 r--/r--/r--   1L expertNoob adm   9.01 kB 2010.09.01 11:23:41 getopt.c
 - 664 rw-/rw-/r--   1L expertNoob adm   6.86 kB 2010.09.01 11:24:47 getopt.o
 - 664 rw-/rw-/r--   1L expertNoob adm   6.93 kB 2010.09.14 19:37:44 findf1.o
 l 775 rwx/rwx/r-x   1L expertNoob adm      6 B  2010.10.06 17:09:01 test1 -> test.c
 - 664 rw-/rw-/r--   1L expertNoob adm    534 B  2009.03.26 15:34:23 > test.c
 d 755 rwx/r-x/r-x  25L expertNoob adm      8 kB 2009.05.20 15:36:23 zip30/
这是:

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <locale.h>
#include <langinfo.h>
#include <stdio.h>
//#include <stdint.h>
#include <limits.h> // PATH_MAX
#include <stdarg.h>
#include "getopt.h"

static short START_VSNBUFF=16;
// This is bformat from Better String library (bstrlib), customized
int strformat (char ** str, const char * fmt, ...) {

    va_list arglist;
    char * buff;
    int n, r;

    /* Since the length is not determinable beforehand, a search is
       performed using the truncating "vsnprintf" call (to avoid buffer
       overflows) on increasing potential sizes for the output result. */

    if ((n = (int) (2*strlen (fmt))) < START_VSNBUFF) n = START_VSNBUFF;
    if ( NULL == ( buff = (char *) malloc((n + 2)*sizeof(char)) ) ) {
        n = 1;
        if ( NULL == ( buff = (char *) malloc((n + 2)*sizeof(char)) ) ) {
            fprintf( stderr, "strformat: not enough memory to format string\n" );
            return -1;
        }
    }

    for (;;) {
        va_start (arglist, fmt);
        r = vsnprintf (buff, n + 1, fmt, arglist); // n+1 chars: buff[0]..buff[n], n chars from arglist: buff[n]='\0'
        va_end (arglist);

        buff[n] = (unsigned char) '\0'; // doesn't hurt, especially strlen!

        if ( strlen(buff) < n ) break;

        if (r > n) n = r; else n += n;

        if ( NULL == ( buff = (char *) realloc( buff, (n + 2)*sizeof(char) ) ) ) {
            free(buff);
            fprintf( stderr, "strformat: not enough memory to format string\n" );
            return -1;
        }
    }

    if( NULL != *str ) free(*str);
    *str = buff;
    return 0;
}

int printFSObjectInfo( const char * path, const char * name ) {

    struct stat    statbuf;

    struct passwd *pwd;
    struct group  *grp;
    struct tm     *tm;
    char           datestring[256];
    char           *type = "? ";
    char           *fbuf = NULL;

    double         size = 0;
    const char     *units[] = {"B ", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
    int            i = 0;

    char owner[] = "---", group[] = "---", others[] = "---";


    /* Get entry's information. */
    if ( -1 == lstat( path, &statbuf ) ) {
        fprintf( stderr, "printFSObjectInfo: error: can't stat %s\n", path );
        if( 0 == strformat( &fbuf, "lstat() said: %s", path ) ) { perror(fbuf); return -1; }
    }

    // File type
    if( S_ISREG(statbuf.st_mode) ) type = "-"; // regular file
    if( S_ISDIR(statbuf.st_mode) ) {         // directory
        type="d";
        if( S_ISCDF(statbuf.st_mode) ) type = "hd";  // hidden dir
    }
    if( S_ISBLK(statbuf.st_mode) ) type = "b"; // block special
    if( S_ISCHR(statbuf.st_mode) ) type = "c"; // character special
    if( S_ISFIFO(statbuf.st_mode) ) type = "f"; // pipe or FIFO
    if( S_ISLNK(statbuf.st_mode) ) type = "l"; // symbolic link
    if( S_ISSOCK(statbuf.st_mode) ) type = "s"; // socket
    if( S_ISNWK(statbuf.st_mode) ) type = "n"; // network special
    printf( "%2s ", type );

    /* Print out type, permissions, and number of links. */
    //printf("%10.10s", sperm (statbuf.st_mode));
    if( S_IRUSR & statbuf.st_mode ) owner[0] = 'r';
    if( S_IWUSR & statbuf.st_mode ) owner[1] = 'w';
    if( S_IXUSR & statbuf.st_mode ) owner[2] = 'x';

    if( S_IRGRP & statbuf.st_mode ) group[0] = 'r';
    if( S_IWGRP & statbuf.st_mode ) group[1] = 'w';
    if( S_IXGRP & statbuf.st_mode ) group[2] = 'x';

    if( S_IROTH & statbuf.st_mode ) others[0] = 'r';
    if( S_IWOTH & statbuf.st_mode ) others[1] = 'w';
    if( S_IXOTH & statbuf.st_mode ) others[2] = 'x';

    //printf( "\n%o\n", statbuf.st_mode );
    printf( "%3o %s/%s/%s ", 0777 & statbuf.st_mode, owner, group, others );

    printf("%4dL", statbuf.st_nlink);

    /* Print out owner's name if it is found using getpwuid(). */
    if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
        printf(" %-8.8s", pwd->pw_name);
    else
        printf(" %-8d", statbuf.st_uid);

    /* Print out group name if it is found using getgrgid(). */
    if ((grp = getgrgid(statbuf.st_gid)) != NULL)
        printf(" %-8.8s", grp->gr_name);
    else
        printf(" %-8d", statbuf.st_gid);

    /* Print size of file. */
    //printf(" %9d", (int)statbuf.st_size);
    i = 0;
    size = (double) statbuf.st_size;
    while (size >= 1024) {
        size /= 1024;
        i++;
    }
    if( 0 == (double)(size - (long) size) )
         printf( "%7d %-2s",  (long)size, units[i] );
    else printf( "%7.2f %-2s", size, units[i] );

    tm = localtime(&statbuf.st_mtime);

    /* Get localized date string. */
    strftime(datestring, sizeof(datestring), "%Y.%m.%d %T", tm); // nl_langinfo(D_T_FMT)

    if ( 0 == strcmp(name, "\n") )
         printf(" %s > %s", datestring, path);
    else {
        if( 0 == strcmp(type, "d") ) printf(" %s %s/", datestring, name);
        else                         printf(" %s %s", datestring, name);
    }

    if( 0 == strcmp(type, "l") ) {
        char buf[1+PATH_MAX];
        if( -1 == readlink( path, buf, (1+PATH_MAX) ) ) {
            fprintf( stderr, "printFSObjectInfo: error: can't read symbolic link %s\n", path);
            if( 0 == strformat( &fbuf, "readlink() said: %s:", path ) ) { perror(fbuf); return -2; }
        }
        else {
            lstat( buf, &statbuf ); // want errno, a symlink may point to non-existing object
            if(errno == ENOENT) printf(" -> %s [!no such file!]\n", buf );
            else {
                printf(" -> %s\n", buf );
                if ( 0 != strcmp(name, "\n") ) printFSObjectInfo( buf, "\n" );
            }
        }
    }
    else printf("\n");

    return 0;
}

int main(int argc, char **argv) {

    struct dirent *dp;
    struct stat    statbuf;

    char           *path = NULL; //[1+PATH_MAX];
    char           *fbuf = NULL;
    char           *pathArg = NULL;

    if( argc == 1 || 0 == strlen(argv[1]) ) pathArg = ".";
    else pathArg = argv[1];

    if ( lstat( pathArg, &statbuf ) == -1 ) {
        printf("%s: error: can't stat %s\n", argv[0], pathArg);
        if( 0 == strformat( &fbuf, "stat() said: %s", pathArg ) ) perror(fbuf);
        exit(2);
    }

if( S_ISDIR(statbuf.st_mode) ) {
    DIR *dir = opendir( pathArg );
    if( NULL == dir ) {
        fprintf( stderr, "%s: error: can't open %s\n", argv[0], pathArg );
        if( 0 != strformat( &fbuf, "opendir() said: %s", pathArg ) ) exit(5);
        perror(fbuf);
        exit(4);
    }

    /* Loop through directory entries. */
    while ( (dp = readdir(dir)) != NULL ) {

        if( 0!= strformat( &path, "%s/%s", pathArg, dp->d_name ) ) continue;

        printFSObjectInfo( path, dp->d_name );
    }
    closedir(dir);
} else  printFSObjectInfo( pathArg, pathArg );

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//#包括
#包括//PATH\u MAX
#包括
#包括“getopt.h”
静态短启动_VSNBUFF=16;
//这是来自更好的字符串库(bstrlib)的B格式,可定制
int strformat(字符**str,常量字符*fmt,…){
va_列表arglist;
字符*浅黄色;
int n,r;
/*由于长度事先无法确定,因此需要进行搜索
使用截断“vsnprintf”调用执行(以避免缓冲区
溢出)增加输出结果的潜在大小*/
如果((n=(int)(2*strlen(fmt))n)n=r;否则n+=n;
如果(NULL==(buff=(char*)realloc(buff,(n+2)*sizeof(char))){
免费(buff);
fprintf(stderr,“strformat:内存不足,无法格式化字符串\n”);
返回-1;
}
}
如果(空!=*str)空闲(*str);
*str=浅黄色;
返回0;
}
int printFSObjectInfo(常量字符*路径,常量字符*名称){
结构stat statbuf;
结构密码*pwd;
结构组*grp;
struct tm*tm;
字符日期字符串[256];
char*type=“?”;
char*fbuf=NULL;
双尺寸=0;
常量字符*单位[]={B”,“kB”,“MB”,“GB”,“TB”,“PB”,“EB”,“ZB”,“YB”};
int i=0;
字符所有者[]=“--”,组[]=“--”,其他[]=“--”;
/*获取条目的信息*/
如果(-1==lstat(路径和statbuf)){
fprintf(stderr,“printfobjectinfo:错误:无法统计%s\n”,路径);
如果(0==strformat(&fbuf,“lstat()表示:%s”,path)){perror(fbuf);return-1;}
}
//文件类型
if(S_ISREG(statbuf.st_mode))type=“-”;//常规文件
if(S_ISDIR(statbuf.st_mode)){//目录
type=“d”;
if(S_ISCDF(statbuf.st_mode))type=“hd”//隐藏目录
}
if(S_ISBLK(statbuf.st_mode))type=“b”//特殊块
if(S_ISCHR(statbuf.st_mode))type=“c”//特殊字符
if(S_ISFIFO(statbuf.st_mode))type=“f”//管道或FIFO
if(S_ISLNK(statbuf.st_mode))type=“l”//符号链接
if(S_ISSOCK(statbuf.st_mode))type=“S”//socket
if(S_ISNWK(statbuf.st_mode))type=“n”//网络专用
printf(“%2s”,类型);
/*打印输出类型、权限和链接数*/
//printf(“%10.10s”,精子(statbuf.st_模式);
如果(S_IRUSR&statbuf.st_模式)所有者[0]=“r”;
如果(S_IWUSR和statbuf.st_模式)所有者[1]=“w”;
如果(S_IXUSR&statbuf.st_模式)所有者[2]=“x”;
如果(S_IRGRP和statbuf.st_模式)组[0]=“r”;
如果(S_IWGRP&statbuf.st_模式)组[1]=“w”;
如果(S_IXGRP和statbuf.st_模式)组[2]=“x”;
如果(S_IROTH&statbuf.st_模式)其他[0]='r';
如果(S_IWOTH&statbuf.st_模式)其他[1]=“w”;
如果(S_IXOTH&statbuf.st_模式)其他[2]=“x”;
//printf(“\n%o\n”,statbuf.st\u模式);
printf(“%3o%s/%s/%s”,0777和statbuf.st_模式,所有者,组,其他);
printf(“%4dL”,statbuf.st_nlink);
/*如果使用getpwuid()找到所有者的名称,请打印它*/
如果((pwd=getpwuid(statbuf.st_uid))!=NULL)
printf(“%-8.8s”,pwd->pw_名称);
其他的
printf(“%-8d”,statbuf.st_uid);
/*如果使用getgrgid()找到组名,则打印出组名*/
if((grp=getgrgid(statbuf.st_gid))!=NULL)
printf(“%-8.8s”,grp->gr\u名称);
其他的
printf(“%-8d”,statbuf.st_gid);
/*打印文件的大小*/
//printf(“%9d”,(int)statbuf.st_size);
i=0;
大小=(双)statbuf.st_大小;
而(大小>=1024){
大小/=1024;
i++;
}
如果(0==(双精度)(大小-(长)大小))
printf(“%7d%-2s”)(长)尺寸,单位[i];
else printf(“%7.2f%-2s”,尺寸,单位[i]);
tm=本地时间(&statbuf.st_mtime);
/*获取本地化的日期字符串*/
strftime(datestring,sizeof(datestring),%Y.%m.%d%T”,tm);//nl_langinfo(d_T_FMT)
如果(0==strcmp(名称“\n”))
printf(“%s>%s”,日期字符串,路径);
否则{
如果(0==strcmp(类型“d”))printf(“%s%s/”,日期字符串,名称);
else printf(“%s%s”,日期字符串,名称);
}
如果(0==strcmp(类型为“l”)){
char buf[1+路径_MAX];
如果(-1==readlink(路径,buf,(1+path_MAX))){
fprintf(stderr,“printfobjectinfo:错误:无法读取符号链接%s\n”,路径);
如果(0==strformat(&fbuf,“readlink()说:%s:,path)){perror(fbuf);return-2;}
}
否则{
lstat(buf,&statbuf);//如果想要错误,符号链接可能会
machine:~/support> istat ../core 
Inode 30034 on device 32/3      File
Protection: rw-rw-r--   
Owner: 500(group)             Group: 500(user)
Link count:   1         Length 10787748 bytes

Last updated:   Wed Feb 22 13:54:28 2012
Last modified:  Wed Feb 22 13:54:28 2012
Last accessed:  Wed Feb 22 19:58:10 2012
find /etc/passwd -maxdepth 0 -printf "%TY/%Tm/%Td %TH:%TM:%.2TS\n"
2011/11/21 13:41:36
ls --time-style='+%d-%m-%Y %H:%M:%S' -l
root:~# ls --time-style='+%d-%m-%Y %H:%M:%S' -l
total 0
-rw-r--r-- 1 root root 0 16-04-2015 23:14:02 other-file.txt
-rw-r--r-- 1 root root 0 16-04-2015 23:13:58 test.txt
stat -c%Y <file>
prompt> ls -lT

total 0
-rw-r--r--  1 youruser  staff  0 Sep 24 10:28:30 2015 my_file_1.txt
-rw-r--r--  1 youruser  staff  0 Sep 24 10:28:35 2015 my_file_2.txt
perl -e '@d=localtime ((stat(shift))[9]); printf "%02d-%02d-%04d %02d:%02d:%02d\n", $d[3],$d[4]+1,$d[5]+1900,$d[2],$d[1],$d[0]' your_file_to_show_the_date_for.your_extension
date +%s -r /etc/passwd
date +%s.%N -r /etc/passwd