结合查找和 ls

Combining find and ls

我正在尝试在 LS 输出上使用 find 或 grep。

目前,ls -l 正在打印大量信息。但我只希望用户关联到一个文件名。并且文件名可能被 greped

使用 find-printf 标志:

find . -name "a*" -printf "%u %f\n"
find . -name "M*" -printf "%u %f\n"

来自man find

-printf format

%u File's user name, or numeric user ID if the user has no name

%f File's name with any leading directories removed (only the last element).

考虑像这样添加一个 perl oneliner:

ll | grep user_u | perl -lane 'print "$F[2] $F[8]"'

-lane 允许使用 space 作为分隔符对输出进行切片,$F[2] 和 $F[8] 是感兴趣的切片(第一个切片是 $F[0])

我自己找到了答案,但我的方法似乎更棘手。 我正在使用:

tr -s ' ' | cut -d ' ' -f3,9 | grep " .*"  

不过你的看起来不错。问题是

find . -name "M*" -printf "%u %f\n"

也显示 .git 个文件。估计题目已经解决了,感谢您的考虑!

大多数系统都提供 stat 命令,可以轻松生成您想要的有关文件(或文件列表)的任何信息。不幸的是,stat 命令没有标准化,选项集也有很大差异。有关详细信息,请在您的系统上阅读 man 1 stat

在 Linux 上,通过 GNU stat,您可以使用

stat -c%U file...

在 BSD 上(包括 Mac OS X),你应该可以使用

stat -f%Su file ...

(如果您想要 uid 而不是用户名,您可以分别使用 -c%u-f%u。)