【问题标题】:unix home directories without entries in /etc/passwd/etc/passwd 中没有条目的 unix 主目录
【发布时间】:2014-12-02 08:12:16
【问题描述】:

我能够获得两个列表( /etc/passwd 和 /home ),但是如何编写脚本,例如读取 /etc/passwd 的行,解析主目录,然后在 /home 中查找。如果不存在则抛出错误,如果存在则继续。

/etc/passwd 用户主目录列表

cut -d":" -f6 /etc/passwd | grep home | sort

来自 /home 的用户列表

ls -1 /home | (while read line; do echo "/home/"$line; done)

也许直接从第一个命令输出到一个文件,然后将每一行读入一个 find 命令,然后......或者,测试

if [ -d "$DIRECTORY" ]; then
echo "directory found for user that doesn't exist"
fi

现在如何把它们放在一起......

编辑:isedev 正是我所需要的。我的原始信息可能措辞有误……我们一直在清理用户,但没有清理他们的 /home 目录。所以我想知道还有哪些 /home 目录没有 /etc/passwd 条目。

这对 T 有用

for name in /home/*; do if [ -d "$name" ]; then cut -d':' -f6 /etc/passwd | egrep -q "^$name$" if [ $? -ne 0 ]; then echo "directory $name does not correspond to a valid user" fi fi done

从现在开始,我们将运行

userdel -r login

【问题讨论】:

  • 不完全可靠。帐户通常在 /home 中有它们的 homedir,但不是全部。例如考虑 root 帐户,它通常存在于/root
  • @MarcB 除非 OP 想知道 /home 下是否有与 /etc/passwd 中的条目不对应的目录。
  • 您是否担心/home 中的目录被/etc/passwd 中的多个条目声明?

标签: shell unix directory passwd


【解决方案1】:

这将报告来自/etc/passwd 的所有应该在/home 但不在的主目录:

cut -d":" -f6 /etc/passwd | grep home | sort | 
    while read dir; do [ -e "$dir" ] || echo Missing $dir; done

这个报告所有不存在的:

cut -d":" -f6 /etc/passwd | while read dir; do 
    [ -e "$dir" ] || echo Missing $dir
done

【讨论】:

    【解决方案2】:

    作为第一个近似值:

    perl -F: -lane 'next if m/^#/;print "$F[5] for user $F[0] missing\n" unless(-d $F[5])' /etc/passwd
    

    如果您想找出/etc/passwd/home 之间的区别

    comm <(find /home -type d -maxdepth 1 -mindepth 1 -print|sort) <(grep -v '^#' /etc/passwd  | cut -d: -f6| grep '/home' | sort)
    

    狭义的

    comm    <(
                find /home -type d -maxdepth 1 -mindepth 1 -print |sort
            ) <(
                grep -v '^#' /etc/passwd  |cut -d: -f6 |grep /home |sort
            )
    

    如果你会使用

    • comm ...(没有上述参数)将显示 3 列 1.) 仅在 /home 2.) 仅在 /etc/passwd 3.) 常见
    • comm -23 .... - 将显示仅在 /home 中的目录(而不是在 /etc/passwd 中)
    • comm -13 .... - 将显示仅在 /etc/passwd 中而不在 /home 中的目录
    • comm -12 .... - 将显示正确的目录(也存在于 /etc/passwd/home 中)

    我不确定 AIX 上的 -{max|min}depth..

    【讨论】:

    • 这是在 AIX...perl 上,带有 -E 在 AIX 7.1 上工作,但不是在 6.1 上。
    • @user2341048 现在 perl 应该可以工作了(使用 -lane
    【解决方案3】:

    所以,假设您想知道 /home 下是否有与现有用户不对应的目录:

    for name in /home/*; do
        if [ -d "$name" ]; then
            cut -d':' -f6 /etc/passwd | egrep -q "^$name$"
            if [ $? -ne 0 ]; then
                echo "directory $name does not correspond to a valid user"
            fi
        fi
    done
    

    再一次,这假设您没有使用诸如 LDAP 或 NIS 之类的名称服务,在这种情况下,将以 cut 开头的行更改为:

    getent passwd | cut -d':' -f6 | egrep -q "^$name$"
    

    【讨论】:

    • +1。作为旁注,您还可以简化为for name in /home/*; do [ -d "$name" ] &amp;&amp; cut -d':' -f6 /etc/passwd | grep q "^$name$" || echo "directory $name does not correspond to a valid user"; done。我也会使用简单的grep,这里不需要egrep,而且无论如何,这已经被弃用了(至少对于GNU grep),而现在有几年支持grep -E
    • @terdon 所有的优点(嗯,除了“弃用”部分——让我感觉老了)。
    • 告诉我吧:)。我已经养成了使用grep -E 的习惯,因为手册页建议了它。
    • POSIX 2008 标准化了 grep-E 选项;不再有单独的egrep(或fgrep)。
    • @JonathanLeffler 他们仍然存在于许多(所有?)Linux 系统上以实现向后兼容性。
    猜你喜欢
    • 1970-01-01
    • 2018-04-06
    • 2021-06-10
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 2021-04-10
    • 2015-05-01
    相关资源
    最近更新 更多