[英]How to only display owner of file when using ls command with special edge case
My objective is to find all files in a directory recursively and display only the file owner name so I'm able to use uniq to count the # of files a user owns in a directory.我的目标是递归地查找目录中的所有文件并仅显示文件所有者名称,以便我能够使用 uniq 来计算用户在目录中拥有的文件数。 The command I am using is the following:
我正在使用的命令如下:
command = "find " + subdirectory.directoryPath + "/ -type f -exec ls -lh {} + | cut -f 3 -d' ' | sort | uniq -c | sort -n"
This command successfully displays only the owner of the file of each line, and allows me to count of the # of times the owner names is repeated, hence getting the # of files they own in a subdirectory.此命令成功地仅显示每行文件的所有者,并允许我计算所有者名称重复的次数,从而获得他们在子目录中拥有的文件的数量。 Cut uses ' ' as a delimiter and only keeps the 3rd column in ls, which is the owner of the file.
Cut 使用 ' ' 作为分隔符,只保留 ls 中的第 3 列,这是文件的所有者。
However, for my purpose there is this special edge case, where I'm not able to obtain the owner name if the following occurs.但是,出于我的目的,存在这种特殊的边缘情况,如果发生以下情况,我将无法获得所有者名称。
-rw-r----- 1 31122918 group 20169510233 Mar 17 06:02
-rw-r----- 1 user1 group 20165884490 Mar 25 11:11
-rw-r----- 1 user1 group 20201669165 Mar 31 04:17
-rwxr-x--- 1 user3 group 20257297418 Jun 2 13:25
-rw-r----- 1 user2 group 20048291543 Mar 4 22:04
-rw-r----- 1 14235912 group 20398346003 Mar 10 04:47
The special edge cases are the #s as the owner you see above.特殊的边缘情况是 #s 作为您在上面看到的所有者。 The current command Im using can detect user1,user2,and user3 perfectly, but because the numbers are placed all the way to the right, the command above doesn't detect the numbers, and simply displays nothing.
我使用的当前命令可以完美地检测到 user1、user2 和 user3,但是因为数字一直放在右边,所以上面的命令没有检测到数字,只是什么都不显示。 Example output is shown here:
此处显示示例 output:
1
1 user3
1 user2
1
2 user1
Can anyone help me parse the ls output so I'm able to detect these #'s when trying to only print the file owner column?任何人都可以帮我解析 ls output 这样我就可以在尝试只打印文件所有者列时检测到这些 #'s 吗?
cut -d' '
won't capture the third field when it contains leading spaces -- each space is treated as the separator of another field.当第三个字段包含前导空格时,
cut -d' '
不会捕获第三个字段——每个空格都被视为另一个字段的分隔符。
Alternatives:备择方案:
cut -c
123456789X123456789X123456789X123456789X123456789L0123456789X0123 -rw-r----- 1 31122918 group 20169510233 Mar 17 06:02 -rw-r----- 1 user1 group 20165884490 Mar 25 11:11
The data you seek is between characters 15 and 34 on each line, so you can say您要查找的数据在每行的第 15 到 34 个字符之间,因此您可以说
cut -c14-39
perl/awk: other tools are adept at extracting data out of a line. perl/awk:其他工具擅长从一行中提取数据。 Try one of
尝试其中之一
perl -lane 'print $F[2]' awk '{print $3}'
Don't try to parse the output of ls
.不要尝试解析
ls
的输出。 Use the stat
command.使用
stat
命令。
find dirname ! -user root -type f -exec stat --format=%U {} + | sort | uniq -c | sort -n
%U
prints the owner username. %U
打印所有者用户名。
Merging multiple spaces tr -s ' '
合并多个空格
tr -s ' '
Get file users获取文件用户
ls -hl | tr -s ' ' | cut -f 3 -d' '
ls -hl | awk '{print $3}'
sudo find ./ ! -user root -type f -exec ls -lh {} + | tr -s ' ' | cut -f 3 -d' ' | sort | uniq -c | sort -n
You can use the below command to display only the owner of a directory or a file.您可以使用以下命令仅显示目录或文件的所有者。
stat -c "%U" /path/of/the/file/or/directory
If you also want to print the group of a file or directory you can use %G as well.如果您还想打印文件或目录的组,您也可以使用 %G。
stat -c "%U %G" /path/of/the/file/or/directory
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.