linux - 列出所有用户目录并查找特定文件
问题描述
我正在编写一个脚本,它将检查具有主目录的所有用户的 ~/ 中的特定文件。
我尝试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 /*/
解决方案
这将查找并列出目录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
那里添加了标志以将搜索限制为文件并排除可能发生共享名称的任何目录。
推荐阅读
- javascript - highcharts 不读取数据,使用假数据
- xml - 在 unix shell 脚本中删除 xml 标记之间的新行
- c++ - VisualStudio 跨平台 (Linux) 项目中的预编译头文件
- html - 使用 Umbraco amd MVC 在 html 中登录失败时显示 div
- laravel - 如何获取从 Laravel 控制器中的复选框输入中获取的数组值?
- android - 如何在android Q中获取相邻单元格信息?
- c# - 如何映射两个不同的接口,以便一个接口值自动更改,另一个应该得到反映
- c++ - Oracle 即时客户端头文件丢失
- docker - 如何强制子域与 NGINX 完全匹配?
- c - 使用文本表进行类别线性转换的 Compu 方法