bash脚本来分析目录和印刷目录汇总 [英] Bash script to analyze directory and print directory summary

查看:110
本文介绍了bash脚本来分析目录和印刷目录汇总的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个脚本,分析UNIX的目录,并会显示输出:
script.sh DIR1 DIR2 ... DIRN

I am working on a script that will analyze any dir in unix and will display the output: script.sh dir1 dir2 ... dirn

所需的输出:
目录包含XXX YY的文件和目录ZZ

Needed output: Directory xxx contains yy files and zz directories

#!/bin/bash
echo 'Enter Dir names'
read dirs
for input_source in $dirs; do 
ls -ld|wc -l; 
echo
# here goes a problem I am trying to figure out how to get this value printed by echo
# together with dirs and files quantity

请指教。

我无法弄清楚如何与code继续进行。请告知

I am not able to figure out how to proceed with the code. Please advise

推荐答案

注意:编辑照顾在文件/新行目录名称

不解析 LS 命令的输出。

要计算文件(不显示目录):

To count files (no dirs):

find "$DIR" -maxdepth 1 -type f -exec echo -n . \; | wc -c

要算迪尔斯:

find "$DIR" -maxdepth 1 -type d -exec echo -n . \; | wc -c

您完整的脚本:

#!/bin/bash
echo 'Enter Dir names'
read dirs
for DIR in "$dirs"; do 
    numFiles=$(find "$DIR" -maxdepth 1 -type f -exec echo -n . \; | wc -c)
    numDirs=$(find "$DIR" -maxdepth 1 -type d -exec echo -n . \; | wc -c)
    echo "Directory $DIR contains $numFiles files and $numDirs directories"
done

这篇关于bash脚本来分析目录和印刷目录汇总的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆