首页 > 解决方案 > 列出所有用户目录并查找特定文件

问题描述

我正在编写一个脚本,它将检查具有主目录的所有用户的 ~/ 中的特定文件。

我尝试ls /home将用户 cd 放入他们的主目录,但它给出了太多参数错误。

username=$(ls /home)
cd /home/$username
cat file.json

我除了 json 文件的输出,但它不提供 json 文件的输出,即使用户有一个 json 文件。

编辑:

现在我需要使用file.json我尝试过的文件提取用户的用户名,grep但它没有用。

files=$(find /home -name tilde.json -print)
echo "$files" >> jsons.txt
cat jsons.txt | grep /*/

标签: linuxbashshellunix

解决方案


这将查找并列出目录file.json下调用的所有文件/home

find /home -name file.json -print 

/dev/null如果您无法访问所有用户的主目录,您可能希望将错误重定向到。

如果要打印所有这些文件的内容,请尝试:

find /home -name file.json -print -exec cat {} \;

要将搜索限制为仅下的目录/home(即不是/home其自身,并且用户主目录中没有子目录),请使用:

find /home -mindepth 2 -maxdepth 2 -type f -name file.json -print -exec cat {} \;

我还在-type那里添加了标志以将搜索限制为文件并排除可能发生共享名称的任何目录。


推荐阅读