查找文件并删除文件名中的字符

Find files and delete characters in the filenames

所以我有一个名为 testdir 的目录,我想从该目录中的文件中删除文件名中的下划线。

我尝试使用这个命令

find testdir -type f -exec ls {} \; | sed 's/_*//'

它会输出没有下划线的文件名,但不会永久删除下划线。谁能帮帮我?

谢谢!

如果您只是循环访问目录中的文件,请使用简单的循环:

while IFS= read -r file
do
   echo mv "$file" "${file//_/}"         #once you are sure it works, remove the echo!
done < <(find -type f -name "*_*")

这将为 while 循环提供 find 命令的输出。然后,使用 ${var//_/} 删除名称中的所有 _


为什么您的方法不起作用?

因为你在说

find ... -exec ls {} \; | sed '...'

也就是说,您正在查找某些内容,然后使用 sed 更改输出。也就是说,文件本身没有做任何事情。

这可能对你有帮助 找到 testdir -type f |重命名 's/_//'

此致