用于循环GNU Linux Synology NAS NAS

发布于 2025-01-25 20:16:54 字数 555 浏览 5 评论 0原文

我正在使用一个简短的脚本来搜索此奇数字符并删除字符的NAS上的大量文件夹。我正在运行Linux的Synology NAS。这就是我到目前为止的。

#!/bin/bash

for file in "$(find "/volume1/PLNAS/" -depth -type d -name '**')";
do
  echo "$file";
  mv "$file" "$(echo $file | sed s/// )";
done

当前的问题是内核似乎并未分别传递每个MV命令。我收到一条长的错误消息,该消息似乎在一个命令中列出了每个文件,下面的截断错误消息。我的文件路径中有空间,这就是为什么我试图引用每个变量的原因。

mv: failed to access '/volume1/PLNAS/... UT Thickness Review ': File name too long

I am working on a short script to search a large number of folders on a NAS for this odd character and delete the character. I am on a Synology NAS running Linux. This is what I have so far.

#!/bin/bash

for file in "$(find "/volume1/PLNAS/" -depth -type d -name '**')";
do
  echo "$file";
  mv "$file" "$(echo $file | sed s/// )";
done

Current problem is that the Kernel does not appear to be passing each MV command separately. I get a long error message that appears to list every file in one command, truncated error message below. There are spaces in my file path and that it why I have tried to quote every variable.

mv: failed to access '/volume1/PLNAS/... UT Thickness Review ': File name too long

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我纯我任性 2025-02-01 20:16:54

几个问题。最重要的是,对于“ $(find ...)”中的文件,仅用file设置为搜索的完整结果。这就是双引号的目的:防止单词分裂。

但是对于$(find ...)中的文件是不安全的:如果某些文件名包含空格,它们将被拆分...

假设字符是Unicode 0xf028( )尝试以下内容:

while IFS= read -r -d '' file; do
  new_file="${file//

如果情况看起来正确,请输入 mv 行。

由于您的文件名不寻常,我们使用 -D'''读取分隔符和 print0 查找选项。这将使用NUL字符(ASCII代码零)作为文件名之间的分离器,而不是默认的newline字符。 NUL字符是您唯一在完整文件名中找不到的字符。

我们还使用bash $'...''扩展来代表其Unicode十六进制代码的不良字符,它比复制涂层字形更安全。新名称是用bash模式替换( $ {var //} )计算的。

注意:不要使用异常字符串使用 echo ,尤其是在不引用字符串的情况下(例如您的 echo $ file | ... )。优先 printf 或引用此处的字符串( sed ...<<<<“ $ file” )。

\uf028'}" printf 'mv %s %s\n' "$file" "$new_file" # mv "$file" "$new_file" done < <(find "/volume1/PLNAS/" -depth -type d -name

如果情况看起来正确,请输入 mv 行。

由于您的文件名不寻常,我们使用 -D'''读取分隔符和 print0 查找选项。这将使用NUL字符(ASCII代码零)作为文件名之间的分离器,而不是默认的newline字符。 NUL字符是您唯一在完整文件名中找不到的字符。

我们还使用bash $'...''扩展来代表其Unicode十六进制代码的不良字符,它比复制涂层字形更安全。新名称是用bash模式替换( $ {var //} )计算的。

注意:不要使用异常字符串使用 echo ,尤其是在不引用字符串的情况下(例如您的 echo $ file | ... )。优先 printf 或引用此处的字符串( sed ...&lt;&lt;&lt;&lt;“ $ file” )。

*\uf028*' -print0)

如果情况看起来正确,请输入mv行。

由于您的文件名不寻常,我们使用-D'''读取分隔符和print0查找选项。这将使用NUL字符(ASCII代码零)作为文件名之间的分离器,而不是默认的newline字符。 NUL字符是您唯一在完整文件名中找不到的字符。

我们还使用bash $'...''扩展来代表其Unicode十六进制代码的不良字符,它比复制涂层字形更安全。新名称是用bash模式替换($ {var //})计算的。

注意:不要使用异常字符串使用echo,尤其是在不引用字符串的情况下(例如您的echo $ file | ...)。优先printf或引用此处的字符串(sed ...&lt;&lt;&lt;&lt;“ $ file”)。

Several issues. The most important is probably that for file in "$(find...)" iterates only once with file set to the full result of your search. This is what the double quotes are for: prevent word splitting.

But for file in $(find...) is not safe: if some file names contain spaces they will be split...

Assuming the character is unicode 0xf028 (  ) try the following:

while IFS= read -r -d '' file; do
  new_file="${file//

Uncomment the mv line if things look correct.

As your file names are unusual we use the -d '' read separator and the print0 find option. This will use the NUL character (ASCII code zero) as separator between the file names instead of the default newline characters. The NUL character is the only one that you cannot find in a full file name.

We also use the bash $'...' expansion to represent the unwanted character by its unicode hexadecimal code, it is safer than copy-pasting the glyph. The new name is computed with the bash pattern substitution (${var//}).

Note: do not use echo with unusual strings, especially without quoting the strings (e.g. your echo $file | ...). Prefer printf or quoted here strings (sed ... <<< "$file").

\uf028'}" printf 'mv %s %s\n' "$file" "$new_file" # mv "$file" "$new_file" done < <(find "/volume1/PLNAS/" -depth -type d -name

Uncomment the mv line if things look correct.

As your file names are unusual we use the -d '' read separator and the print0 find option. This will use the NUL character (ASCII code zero) as separator between the file names instead of the default newline characters. The NUL character is the only one that you cannot find in a full file name.

We also use the bash $'...' expansion to represent the unwanted character by its unicode hexadecimal code, it is safer than copy-pasting the glyph. The new name is computed with the bash pattern substitution (${var//}).

Note: do not use echo with unusual strings, especially without quoting the strings (e.g. your echo $file | ...). Prefer printf or quoted here strings (sed ... <<< "$file").

*\uf028*' -print0)

Uncomment the mv line if things look correct.

As your file names are unusual we use the -d '' read separator and the print0 find option. This will use the NUL character (ASCII code zero) as separator between the file names instead of the default newline characters. The NUL character is the only one that you cannot find in a full file name.

We also use the bash $'...' expansion to represent the unwanted character by its unicode hexadecimal code, it is safer than copy-pasting the glyph. The new name is computed with the bash pattern substitution (${var//}).

Note: do not use echo with unusual strings, especially without quoting the strings (e.g. your echo $file | ...). Prefer printf or quoted here strings (sed ... <<< "$file").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文