linux - 查找命令不适用于名称中的冒号

标签 linux bash shell find

我毫无问题地将许多包含 mp3/m4a 文件的文件夹复制到我的 NAS 音乐目录 (ext3)。一些文件夹名称包含“:”(冒号)、多个点 (...) 等。如何使用查找命令通过删除冒号和其他不需要的字符来重命名文件夹?每个简单的查找命令都会给我一个错误:

cd /nas3/Musik
find . -type d -exec grep -H ':' '{}' \+
find: "./Eis_Am_Stiel_Volume_3:_Liebeleien--The_Shirelles": Datei oder Verzeichnis nicht gefunden
find: "./Kicking_Television:_Live_In_Chicago--Wilco": Datei oder Verzeichnis nicht gefunden
find: "./Respect:_A_Century_Of_Women_In_Music--Mary_Wells": Datei oder Verzeichnis nicht gefunden

...

德语术语“Datei oder Verzeichnis nicht gefunden”的意思是“找不到文件或文件夹”。

我该如何解决这个问题? 非常感谢!

最佳答案

命令:

find . -regextype egrep -iregex '.*\:.*'

替代方案:

find . -iname "*:*"

结果:

./Eis_Am_Stiel_Volume_3:_Liebeleien--The_Shirelles
./Kicking_Television:_Live_In_Chicago--Wilco
./Respect:_A_Century_Of_Women_In_Music--Mary_Wells

评论:

  • 在使用 find(或在管道周围抛出的任何文件名)时,使用 " 的做法<---quotes--->" 围绕任何标识符/变量/反向引用/字符串通常会很有帮助。

  • 如果出于某种原因您确实需要对从 find 发出的文件名进行操作,使用空分隔符会更安全:

示例:

find . -iname "*:*" -print0 | while IFS= read -rd '' file ; do echo "$file" ; done

将与 :

一样工作
find . -iname "*:*" -print | while read -r file ; do echo "$file" ; done

但是第一个例子要安全得多:

  • 使用 -print0 会导致 find 输出 null 分隔文件,而不是 line 分隔文件。你会惊讶于一些奇怪命名的文件(尤其是那些从下载中提取/转换不当/否则)包含换行转义,当非战略性地放置在管道中时,它们可能真的非常糟糕。

  • while IFS=(但没有命令分隔符),设置该命令的 IFS。因此,如果此环境变量发生更改,您不会感到意外

  • 虽然 IFS= read -rd '' var 本质上是对 print0 的“读取”补充——如果您输出空分隔符,那么您也有读取空分隔符。

关于linux - 查找命令不适用于名称中的冒号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40202412/

相关文章:

shell - 二郎 shell pretty-print 深度

c - X11 改变屏幕方向 Debian

sql - 如何将变量从shell脚本传递给sqlplus

linux - 为什么 perl 编译后的代码需要更多的内存?

Linux 命令行界面 : Extract variable number of lines from text file based on search

linux - set $(wc -l $title_file) 在 shell 中是什么意思?

xml - 在 bash 脚本中提取 XML 值

linux - Shell (Bash) 脚本的测试与分析?

linux - 如何检测我在 .bash_profile 中使用的服务器并相应地应用设置

linux - 制作一个可以循环的计算器 - Bash