编写一个shell程序count.sh统计当前目录中子目录以及文件的数量
时间: 2024-05-06 19:17:22 浏览: 48
可以使用以下命令实现:
```
#!/bin/bash
echo "Number of directories: $(find . -type d | wc -l)"
echo "Number of files: $(find . -type f | wc -l)"
```
将以上代码保存为 count.sh,并给予执行权限:
```
chmod +x count.sh
```
然后在终端中执行:
```
./count.sh
```
即可统计当前目录中子目录以及文件的数量。
相关问题
编写一个Shell程序countfile,能统计出当前目录中子目录、文件的数量
好的,以下是实现 `countfile` 的 Shell 脚本程序:
```bash
#!/bin/bash
echo "子目录数量: $(find . -maxdepth 1 -type d | wc -l)"
echo "文件数量: $(find . -maxdepth 1 -type f | wc -l)"
```
使用 `find` 命令可以遍历当前目录下的所有子目录和文件,通过 `wc -l` 命令可以计算出数量。其中,`-maxdepth 1` 表示只遍历当前目录,不会递归到子目录中。
使用方法:
1. 打开终端
2. 进入要统计的目录
3. 输入 `sh countfile.sh` (假设脚本文件名为 countfile.sh)
编写一个Shell程序countfile,能统计出当前目录中子目录、文件的数量。
下面是一个简单的Shell程序countfile,可以统计当前目录中子目录、文件的数量:
```bash
#!/bin/bash
# 统计子目录数量
dircount=$(find . -type d | wc -l)
# 统计文件数量
filecount=$(find . -type f | wc -l)
echo "当前目录中子目录数量为:$dircount"
echo "当前目录中文件数量为:$filecount"
```
该程序首先使用find命令查找当前目录中的所有子目录,然后使用wc命令统计行数,即子目录数量。同样地,该程序也可以统计当前目录中的文件数量。