简体   繁体   English

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

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

I'm trying to find all files from a certain path except the files owned by users who have "developer" or "admin" within their user name.我试图从某个路径中查找所有文件,但用户名中包含“开发人员”或“管理员”的用户拥有的文件除外。 Can anyone help me achieve this?任何人都可以帮助我实现这一目标吗?

I am using the find command to find files.我正在使用 find 命令来查找文件。 Tried doing this using the -user argument but it failed.尝试使用 -user 参数执行此操作,但失败了。

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

I am also tasked with finding all files where the owner name of the file represents an integer (The owner name is a string, but represents an integer).我还负责查找所有文件的所有者名称代表整数的文件(所有者名称是一个字符串,但代表一个整数)。 I know isdigit() returns true if a string represents a positive integer.我知道如果字符串表示正整数,则 isdigit() 返回 true。 Would anyone know how to achieve this as well?有谁知道如何实现这一目标? Thanks.谢谢。

I don't think you can do it directly with find , because -user does a straight up equality comparison, not wildcard or regular expression matching.我不认为你可以直接用find来做,因为-user直接进行相等比较,而不是通配符或正则表达式匹配。

A quick perl script that does the job (Pass directory names to search on the command line):完成这项工作的快速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);

Avoiding perl , hmm...避免perl ,嗯...

find pathname -type f -printf "%u\037%p\036" | awk -F"\037" -v RS="\036" '$1 !~ /developer|admin/ { print $2 }'

will find files except ones owned by the developer and admin accounts, but for the second part, you can't tell a user id that doesn't have a name apart from a name that's all digits with this approach.将查找除开发人员和管理员帐户拥有的文件之外的文件,但对于第二部分,除了使用这种方法的全数字名称之外,您无法告诉没有名称的用户 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM