简体   繁体   English

在特殊边缘情况下使用 ls 命令时如何仅显示文件所有者

[英]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:备择方案:

  1. 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
  1. 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获取文件用户

  1. ls -hl | tr -s ' ' | cut -f 3 -d' '
  2. 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.

相关问题 如何在 Bash 中使用 ls 命令仅显示目录中的隐藏文件 - How to display only hidden files in directory using ls command in Bash 如何过滤ls命令的输出只显示二月份创建的文件? - How to filter the output of ls command to display only files created in February? 如何仅显示来自 aws s3 ls 命令的文件? - How to display only files from aws s3 ls command? Linux-使用ls或find命令查找所有者可执行的隐藏文件 - Linux - finding hidden files that are executable by the owner using ls or find command 如何忽略 ls 命令中没有这样的文件目录的错误? - How to ignore the error No such file directory in the ls command? 如何使用execvp在c中编写ls命令 - how to write ls command in c using execvp Bash:如何将ls -la的输出“ grep”匹配文件所有者和文件权限? - Bash: How do I “grep” output of ls -la matching file owner and file permissions? 在UNIX / Linux中如何显示'ls -l'命令的列标题? - How to display column headers for 'ls -l' command in unix/linux? 如何使用sort命令但不使用ls -lrt命令列出文件 - How to list the files using sort command but not ls -lrt command 如何在linux中使用ls和-R选项结合grep显示文件的完整路径 - How to display full path of file using ls in linux with -R option in conjunction with grep
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM