linux - 显示路径中目录的详细信息 - bash 脚本分配
问题描述
我有一个需要创建脚本的任务。我想要一些帮助。我一直在挣扎,此时我迷路了,不知道如何继续。我必须创建一个脚本,该脚本将确保路径中的所有项目都是目录,然后继续显示这样的目录详细信息。
如果./pathdisplay ~unx510/sample.dir1/testdir1
输入,则显示:
Owner Group Other Filename
----- ----- ----- --------
r w x r - x r - x /
r w x r - x r - x home
r w x - - x - - x unx510
r w x r - x r - x sample.dir1
r w x r - x r - x testdir1
Links: 4 Owner: unx510 Group: users Size: 229 Modified: Feb 22 2015
Valid commands: u(p) d(own) q(uit)
这是我到目前为止所拥有的:
if [ $# -ne 1 ]; then
echo "Usage: ./pathdisplay [ dir-name ]" >&2
exit 1
elif [ ! -d "$1" ]; then
echo "$1 is not a valid directory name" >&2
exit 1
else
allDirectories=$(tr '/.' '\n' < <(printf "%s" $1))
numOfDirectories=$(tr '/.' '\n' < <(printf "%s" $1) | wc -l)
SAVEIFS=$IFS
IFS=$'\n'
directories=($allDirectories)
IFS=$SAVEIFS
for (( i=0; i<${#directories[@]}; i++ ))
do
if [ $i -le 0 ]; then
$(ls ${directories[$i]})
else
tempIndex=$i
previousDirectory=${directories[i--]}
$(ls '$previousDirectory/'${directories[$i]})
fi
done
exit 0
fi
我不太明白如何遍历目录。因为我需要指定上一个目录。然后使用命令来显示屏幕,以显示权限和详细信息。任何帮助,将不胜感激。
https://ideone.com/Dhj7Sq - 到目前为止的 bash 脚本链接
http://czegel.com/seneca/unx510-dps918/assign1a/Assign1a.html - 分配规范
http://czegel.com/seneca/unx510-dps918/assign1a/Assign1b.html - 作业截图
解决方案
使用 'find' 命令列出你想要的所有目录,你可以使用 stat 生成输出
find . -maxdepth 1 -type d|xargs stat -c "%A %n, links:%h, owner:%U, group:%G, size:%B, modified:%y"
或者您可以只使用 find 命令循环迭代元素并生成更多格式化输出
推荐阅读
- angular - 具有多个日期选择的 Angular 6 日历
- server - Jupyter 服务器可从外部访问但拒绝本地主机连接
- android - 如何从 Presenter 访问 Fragment Manager(或 Hosting Activity)
- python - Jinja2检查字典列表中是否存在值
- amazon-web-services - AWS Kubernetes 暴露服务超时错误
- spring - 如何在spring boot中将数据填充到表onclick按钮中?
- java - 使用 Akka-http Java 加载静态 html 文件
- google-sheets - 从项目、任务和日期表中创建一列信息
- rx-java - 上游的 onComplete 如何传播到下游的平面图可观察对象?
- mongodb - 猫鼬连接和池大小不起作用