1.示例主脚本 delete_all_folders.sh
它会遍历指定目录下的所有子目录,并调用 delete_files.sh 脚本删除每个子目录中的文件和空目录
#!/bin/bash# 检查是否指定了根目录路径
if [ -z "$1" ]; thenecho "Usage: $0 <root_directory>"exit 1
fiROOT_DIRECTORY="$1"# 检查指定的根目录是否存在
if [ ! -d "$ROOT_DIRECTORY" ]; thenecho "Error: Root directory '$ROOT_DIRECTORY' does not exist."exit 1
fi# 遍历根目录下的所有子目录
for SUBDIRECTORY in "$ROOT_DIRECTORY"/*/; doif [ -d "$SUBDIRECTORY" ]; thenecho "Processing directory: $SUBDIRECTORY"# 调用 delete_files.sh 脚本./delete_files.sh "$SUBDIRECTORY"echo "All subdirectories in '$SUBDIRECTORY' have been processed."fi
doneecho "All subdirectories in '$ROOT_DIRECTORY' have been processed."
2.示例子脚本 delete_files.sh
#!/bin/bash# 检查是否指定了目录路径
if [ -z "$1" ]; thenecho "Usage: $0 <directory>"exit 1
fiDIRECTORY="$1"# 检查指定的目录是否存在
if [ ! -d "$DIRECTORY" ]; thenecho "Error: Directory '$DIRECTORY' does not exist."exit 1
fi# 递归删除目录下的所有文件
find "$DIRECTORY" -type f -print0 | xargs -0 -n 1000 -P 40 rm -f# 删除空目录
find "$DIRECTORY" -type d -empty -deleteecho "All files and empty directories in '$DIRECTORY' have been deleted."
3.赋予两个脚本权限
chmod +x delete_all_folders.sh
chmod +x delete_files.sh
4.后台运行脚本
nohup ./delete_all_folders.sh /path/to/root_directory > all_output.log 2>&1 &
5.查看 all_output.log 文件来检查主脚本的输出和错误信息
[root@localhost storage]# tail -f all_output.log
Processing directory: data//1001/
All files and empty directories in 'data//1001/' have been deleted.
All subdirectories in 'data//1001/' have been processed.
Processing directory: data//1002/
All files and empty directories in 'data//1002/' have been deleted.
All subdirectories in 'data//1002/' have been processed.
Processing directory: data//1003/