【问题标题】:How to find all files except those with particular substring in owner name of file如何查找除文件所有者名称中具有特定子字符串的文件之外的所有文件
【发布时间】:2021-07-29 23:45:09
【问题描述】:

我正在尝试从某个路径查找所有文件,但用户名中包含“开发者”或“管理员”的用户拥有的文件除外。谁能帮我实现这个目标?

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

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

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

【问题讨论】:

  • 路径名是否设置在其他地方?如果是这样,那么它应该是[$pathname]
  • -user 只做完全匹配,不做通配符。
  • 没有路径名在我的工作区中。出于一般目的,我只是这样输入它。是的,这就是我尝试上述命令时的假设

标签: python linux unix command-line find


【解决方案1】:

我不认为你可以直接用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 "%u\037%p\036" | awk -F"\037" -v RS="\036" '$1 !~ /developer|admin/ { print $2 }'

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

【讨论】:

  • 我对 awk 等不是很熟悉。有没有办法配置上述命令来查找所有不属于其名称中有 developer 或 admin 的用户的所有文件,而不是相反?我可以添加一个'!' -F 之后?
  • @CheeseMan 哎呀,我误读了你的问题。见编辑。
猜你喜欢
  • 1970-01-01
  • 2019-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
  • 2019-10-19
相关资源
最近更新 更多