shell脚本——搜索某个目录下带指定前缀的文件
#!/bin/bash
# 参数检查
if [ $# -ne 2 ]; then
echo "用法: $0 <目录路径> <文件前缀>"
exit 1
fi
target_dir="$1"
file_prefix="$2"
# 验证目录存在性
if [ ! -d "$target_dir" ]; then
echo "错误: 目录 $target_dir 不存在"
exit 2
fi
# 执行查找并处理结果
find "$target_dir" -type f -name "${file_prefix}*" -print | while read -r file; do
echo "找到文件: $file"
done
echo "搜索完成"