用于打印文件作者的Unix ls命令选项

用于打印文件作者的Unix ls命令选项,unix,options,ls,author,Unix,Options,Ls,Author,谁能告诉我用哪个ls选项打印文件的作者或所有者?我已经搜索了2个多小时,唯一找到的是连字符作者,它不起作用。我试过Unix.com、unixtural.com、Ubuntu.com和其他十几个网站。我用过谷歌,雅虎,必应,DuckDuckGo。我准备放弃一切。要获得作者,你需要将--author与-l结合起来(没有它就无法工作)。请记住,在大多数支持ls--author的unix中,author和owner是同一件事,我相信只有在GNU-Hurd中,它们是不同的概念。此外,并非所有unix都提供

谁能告诉我用哪个ls选项打印文件的作者或所有者?我已经搜索了2个多小时,唯一找到的是连字符作者,它不起作用。我试过Unix.com、unixtural.com、Ubuntu.com和其他十几个网站。我用过谷歌,雅虎,必应,DuckDuckGo。我准备放弃一切。

要获得作者,你需要将
--author
-l
结合起来(没有它就无法工作)。请记住,在大多数支持
ls--author
的unix中,author和owner是同一件事,我相信只有在GNU-Hurd中,它们是不同的概念。此外,并非所有unix都提供了
--author
选项

通过查看
ls-l
的输出可以得到当前所有者-它通常是行中的第三个参数(尽管这可能会根据一些情况而改变)。因此,简单地说,您可以使用:

ls -al myFileName | awk '{print $3}'
当然,解析
ls
的输出很少是个好主意。最好使用C程序调用文件上的
stat()
,并获取
st\u uid
字段以获取当前所有者:

#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

int Usage(char *errStr) {
    fprintf(stderr, "*** ERROR: %s\n", errStr);
    fprintf(stderr, "Usage: owner <file> [-n]\n");
    fprintf(stderr, "       '-n' forces numeric ID\n");
    return 1;
}

int main(int argc, char *argv[]) {
    if ((argc != 2) && (argc != 3))
        return Usage("Incorrect argument count");

    if ((argc == 3) && (strcmp(argv[2], "-n") != 0))
        return Usage("Final parameter must be '-n' if used");

    struct stat fileStat;
    int retStat = stat(argv[1], &fileStat);
    if (retStat != 0)
        return Usage(strerror(errno));

    struct passwd *pw = getpwuid (fileStat.st_uid);
    if ((argc == 3) || (pw == NULL)) {
        printf("%d\n", fileStat.st_uid);
        return 0;
    }

    puts(pw->pw_name);

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int用法(char*errStr){
fprintf(标准字符,***错误:%s\n”,errStr);
fprintf(stderr,“用法:所有者[-n]\n”);
fprintf(stderr,“-n”强制数字标识\n”);
返回1;
}
int main(int argc,char*argv[]){
如果((argc!=2)和(&(argc!=3))
返回用法(“参数计数不正确”);
如果((argc==3)和&(strcmp(argv[2],“-n”)!=0))
返回用法(“如果使用,则最终参数必须为“-n”);
struct stat fileStat;
int retStat=stat(argv[1],&fileStat);
如果(retStat!=0)
返回用法(strerror(errno));
结构passwd*pw=getpwuid(fileStat.st_-uid);
if((argc==3)| |(pw==NULL)){
printf(“%d\n”,fileStat.st\u uid);
返回0;
}
puts(pw->pw_名称);
返回0;
}
将其编译为
owner
,然后使用
owner myFileName
调用它以获取给定文件的所有者。它将尝试查找所有者的文本名称,但如果找不到文本名称,或者在调用结束时放置了
-n
标志,它将恢复为数字ID。

要获取作者,请将
--author
-l
组合(没有它无法工作)。请记住,在大多数支持
ls--author
的unix中,author和owner是同一件事,我相信只有在GNU-Hurd中,它们是不同的概念。此外,并非所有unix都提供了
--author
选项

通过查看
ls-l
的输出可以得到当前所有者-它通常是行中的第三个参数(尽管这可能会根据一些情况而改变)。因此,简单地说,您可以使用:

ls -al myFileName | awk '{print $3}'
当然,解析
ls
的输出很少是个好主意。最好使用C程序调用文件上的
stat()
,并获取
st\u uid
字段以获取当前所有者:

#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

int Usage(char *errStr) {
    fprintf(stderr, "*** ERROR: %s\n", errStr);
    fprintf(stderr, "Usage: owner <file> [-n]\n");
    fprintf(stderr, "       '-n' forces numeric ID\n");
    return 1;
}

int main(int argc, char *argv[]) {
    if ((argc != 2) && (argc != 3))
        return Usage("Incorrect argument count");

    if ((argc == 3) && (strcmp(argv[2], "-n") != 0))
        return Usage("Final parameter must be '-n' if used");

    struct stat fileStat;
    int retStat = stat(argv[1], &fileStat);
    if (retStat != 0)
        return Usage(strerror(errno));

    struct passwd *pw = getpwuid (fileStat.st_uid);
    if ((argc == 3) || (pw == NULL)) {
        printf("%d\n", fileStat.st_uid);
        return 0;
    }

    puts(pw->pw_name);

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int用法(char*errStr){
fprintf(标准字符,***错误:%s\n”,errStr);
fprintf(stderr,“用法:所有者[-n]\n”);
fprintf(stderr,“-n”强制数字标识\n”);
返回1;
}
int main(int argc,char*argv[]){
如果((argc!=2)和(&(argc!=3))
返回用法(“参数计数不正确”);
如果((argc==3)和&(strcmp(argv[2],“-n”)!=0))
返回用法(“如果使用,则最终参数必须为“-n”);
struct stat fileStat;
int retStat=stat(argv[1],&fileStat);
如果(retStat!=0)
返回用法(strerror(errno));
结构passwd*pw=getpwuid(fileStat.st_-uid);
if((argc==3)| |(pw==NULL)){
printf(“%d\n”,fileStat.st\u uid);
返回0;
}
puts(pw->pw_名称);
返回0;
}

将其编译为
owner
,然后使用
owner myFileName
调用它以获取给定文件的所有者。它将尝试查找所有者的文本名称,但如果找不到文本名称,或者在调用结束时添加了
-n
标志,它将恢复为数字ID。

我尝试了所有这些,但没有人使用上面的代码。我想可能是因为我使用的是ubuntu。对于linux系统,一个简单的ls命令,-然后author-l就可以了。厌倦了完整地编写author,不要使用-a,linux可能会将其转换为另一个命令。
ls-作者-l |文件名

这将打印作者姓名和版本。

我尝试了所有这些,但没有人使用上面的代码。我想可能是因为我使用的是ubuntu。对于linux系统,一个简单的ls命令,-author然后-l就可以了。厌倦了完整地编写作者,不要使用-a,linux可能会将其转换为另一个命令。
ls-作者-l |文件名

将打印作者姓名和版本。

--author
在任何BSD中都不可用。我不知道这是否是Linux独有的东西。谈到信息来源,我的Ubuntu 16.04的
ls
手册页明确指出,您应该使用
-l
-作者使用-l,打印每个文件的作者
-作者在任何BSD中都不可用。我不知道这是否是Linux独有的东西。谈到信息来源,我的Ubuntu 16.04的
ls
手册页明确指出,你应该使用
-l
-作者使用-l,打印每个文件的作者