Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在bash中使用find和变量_Bash - Fatal编程技术网

在bash中使用find和变量

在bash中使用find和变量,bash,Bash,我是bash脚本新手,需要帮助: 我需要从目录中删除特定文件。我的目标是在每个子目录中找到一个名为“filename.a”的文件,并删除所有以扩展名为B的“filename”开头的文件, 即:“filename01.B”、“filename02.B”等 我试过: B_folders="$(find /someparentdirectory -type d -name "*.B" | sed 's# (.*\)/.*#\1#'|uniq)" A_folders="$(find "$B_folde

我是bash脚本新手,需要帮助:

我需要从目录中删除特定文件。我的目标是在每个子目录中找到一个名为“filename.a”的文件,并删除所有以扩展名为B的“filename”开头的文件, 即:“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”等。

如果空间是唯一的问题,您可以修改for中的查找,如下所示:

find "$FILE" -name "$A*.B" -print0 | xargs -0 rm
男子寻宝秀:

和xarg的手册


bash
4中,它只是

shopt -s globstar nullglob
for f in some_parent_directory/**/filename.A; do
    rm -f "${f%.A}"*.B
done

它可以工作,但如果没有“filename*.B”,它将尝试删除它,即使它不存在。如何避免这种情况?我添加了
-f
以在文件不存在时抑制错误。如果“some_parent_directory”实际上是一个变量中存储的2个或多个目录路径(find命令的输出:folders=“$(find/g-type d-name“jpegtest*”),它不会搜索所有目录的所有子目录。缺少什么?请为此打开一个新问题。
  -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.
shopt -s globstar nullglob
for f in some_parent_directory/**/filename.A; do
    rm -f "${f%.A}"*.B
done