我是新手来编写脚本并需要帮助:
我需要从目录中删除特定文件。我的目标是在每个子目录中找到一个名为" filename.A"的文件。并删除所有以" filename"开头的文件延伸B, 那就是:" filename01.B" ," filename02.B"等。
我试过了:
B_folders="$(find /someparentdirectory -type d -name "*.B" | sed 's# (.*\)/.*#\1#'|uniq)"
A_folders="$(find "$B_folders" -type f -name "*.A")"
for FILE in "$A_folders" ; do
A="${file%.A}"
find "$FILE" -name "$A*.B" -exec rm -f {}\;
done
当目录名称包含空格时,开始出现问题。
有关正确方法的建议吗?
编辑:
我的目标是在每个子目录中找到(名称中可能有空格),文件格式为:" filename.A"
如果存在此类文件:
检查" filename * .B"存在并删除它, 即:删除:" filename01.B" ," filename02.B"等。
答案 0 :(得分:2)
在bash
4中,它只是
shopt -s globstar nullglob
for f in some_parent_directory/**/filename.A; do
rm -f "${f%.A}"*.B
done
答案 1 :(得分:0)
如果空格是唯一的问题,您可以修改for内的find,如下所示:
find "$FILE" -name "$A*.B" -print0 | xargs -0 rm
男人找到节目:
-print0 True; print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corre- sponds to the -0 option of xargs.
和xarg的手册
-0 Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literal- ly). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode.