linux 循环显示所有的sh.*文件.,利用shell脚本遍历文件夹内所有的文件并作整理统计的方法...
时间: 2024-02-06 19:13:00 浏览: 29
好的,让我来回答你的问题。
显示所有的sh.*文件可以使用以下命令:
```sh
ls sh.*
```
这会列出当前目录下所有以 "sh." 开头的文件。
至于遍历文件夹内所有的文件并作整理统计的方法,可以使用以下 shell 脚本:
```sh
#!/bin/bash
# 定义变量
file_count=0
dir_count=0
# 定义函数
function count_files() {
for file in $1/*; do
if [ -d "$file" ]; then
# 如果是目录,则递归调用函数
((dir_count++))
count_files "$file"
elif [ -f "$file" ]; then
# 如果是文件,则统计文件个数
((file_count++))
fi
done
}
# 调用函数
count_files "/path/to/folder"
# 输出结果
echo "文件夹内共有 $dir_count 个子目录,$file_count 个文件。"
```
这个脚本会遍历 /path/to/folder 文件夹内的所有文件和子目录,并统计文件和子目录的数量。你只需要将 /path/to/folder 替换为你想要统计的文件夹路径即可。
相关问题
编写一个shell脚本,遍历当前文件夹下的所有文件。如果子文件夹下包含cmd.sh 就执行 sh cmd.sh
你可以使用以下 Shell 脚本来实现你的需求:
```bash
#!/bin/bash
# 遍历当前文件夹下所有文件和子文件夹
for file in $(find . -type f); do
# 如果子文件夹下包含 cmd.sh,则执行 sh cmd.sh
if [[ $file == */cmd.sh ]]; then
sh $file
fi
done
```
这个脚本会使用 `find` 命令遍历当前文件夹下的所有文件和子文件夹。对于每个找到的文件,如果它的路径包含 `/cmd.sh`,则执行 `sh cmd.sh` 命令。请注意,此脚本假定当前文件夹下没有空格或其他特殊字符的文件名。如果有这样的文件名,你需要修改脚本以正确处理它们。
编写一个shell脚本,遍历当前文件夹下的所有文件。如果子文件夹下包含cmd.sh 就执行 sh cmd.sh。 a文件夹和b文件夹优先执行
你可以使用以下 Shell 脚本来实现你的需求:
```bash
#!/bin/bash
# 遍历 a 文件夹下所有文件和子文件夹,执行 cmd.sh
for file in $(find ./a -type f); do
if [[ $file == */cmd.sh ]]; then
sh $file
fi
done
# 遍历 b 文件夹下所有文件和子文件夹,执行 cmd.sh
for file in $(find ./b -type f); do
if [[ $file == */cmd.sh ]]; then
sh $file
fi
done
# 遍历其他文件夹下所有文件和子文件夹,执行 cmd.sh
for file in $(find . -type f); do
if [[ $file == */cmd.sh && $file != */a/* && $file != */b/* ]]; then
sh $file
fi
done
```
这个脚本会先遍历 `a` 文件夹下的所有文件和子文件夹,对于每个找到的文件,如果它的路径包含 `/cmd.sh`,则执行 `sh cmd.sh` 命令。然后它会遍历 `b` 文件夹下的所有文件和子文件夹,执行同样的操作。最后,它会遍历其他文件夹下的所有文件和子文件夹,对于每个找到的文件,如果它的路径包含 `/cmd.sh` 且不在 `a` 或 `b` 文件夹中,就执行 `sh cmd.sh` 命令。请注意,这个脚本同样假定当前文件夹下没有空格或其他特殊字符的文件名。如果有这样的文件名,你需要修改脚本以正确处理它们。