用于循环GNU Linux Synology NAS NAS
我正在使用一个简短的脚本来搜索此奇数字符并删除字符的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几个问题。最重要的是,对于“ $(find ...)”中的文件,仅用
file
设置为搜索的完整结果。这就是双引号的目的:防止单词分裂。但是
对于$(find ...)中的文件
是不安全的:如果某些文件名包含空格,它们将被拆分...假设字符是Unicode
0xf028
( )尝试以下内容:如果情况看起来正确,请输入
mv
行。由于您的文件名不寻常,我们使用
-D'''
读取分隔符和print0
查找选项。这将使用NUL字符(ASCII代码零)作为文件名之间的分离器,而不是默认的newline字符。 NUL字符是您唯一在完整文件名中找不到的字符。我们还使用bash
$'...''
扩展来代表其Unicode十六进制代码的不良字符,它比复制涂层字形更安全。新名称是用bash模式替换($ {var //}
)计算的。注意:不要使用异常字符串使用
echo
,尤其是在不引用字符串的情况下(例如您的echo $ file | ...
)。优先printf
或引用此处的字符串(sed ...<<<<“ $ file”
)。Several issues. The most important is probably that
for file in "$(find...)"
iterates only once withfile
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:Uncomment the
mv
line if things look correct.As your file names are unusual we use the
-d ''
read separator and theprint0
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. yourecho $file | ...
). Preferprintf
or quoted here strings (sed ... <<< "$file"
).