如何查找除文件所有者名称中具有特定子字符串的文件之外的所有文件

How to find all files except those with particular substring in owner name of file

我正在尝试查找特定路径中的所有文件,但用户名中包含“developer”或“admin”的用户所拥有的文件除外。谁能帮我实现这个目标?

我正在使用 find 命令查找文件。尝试使用 -user 参数执行此操作但失败了。

find [pathname] -type f -not -user "*admin*"

我还负责查找所有文件的所有者名称代表整数的文件(所有者名称是一个字符串,但代表一个整数)。我知道 isdigit() returns 如果字符串表示正整数则为真。谁也知道如何实现这一目标?谢谢

我认为你不能直接用 find 来做,因为 -user 做的是直接相等比较,而不是通配符或正则表达式匹配。

完成任务的快速 perl 脚本(传递目录名称以在命令行上搜索):

#!/usr/bin/env perl
use strict;
use warnings;
use File::Find;
use File::stat;
use User::pwent;
use feature qw/say/;

my %uids; # Cache user information

sub wanted {
    my $st = stat($File::Find::name) or
        (warn "Couldn't stat $File::Find::name: $!\n" && return);
    return unless -f $st; # Only look at regular files
    my $user =
        exists $uids{$st->uid} ? $uids{$st->uid} : $uids{$st->uid} = getpwuid($st->uid);
    # Print filenames owed by uids that don't include developer
    # or admin in a username
    say $File::Find::name if !defined $user || $user->name !~ /developer|admin/;
    # Or defined $user && $user->name =~ /^\d+/ for filtering to usernames that are all digits
    # Or just !defined $user for files owned by uids that don't have /etc/passwd entries
}

find(\&wanted, @ARGV);

避免perl,嗯...

find pathname -type f -printf "%u7%p6" | awk -F"7" -v RS="6" ' !~ /developer|admin/ { print  }'

将查找除开发人员和管理员帐户拥有的文件之外的文件,但对于第二部分,您无法通过这种方法区分没有名称的用户 ID 和全数字的名称。