我正在编写一个 bash 脚本,它将当前目录中文件名中的空格更改为下划线。这是脚本:
#!/bin/bash
for fil in "$(pwd)"/*; do
basefil=$(basename "$fil") #get name of file
name=${basefil// /_}
if [[ ! -f "$name" ]]; then
echo "$fil" "$name"
mv "$fil" "$name"
fi
done
问题是我得到这样的东西:mv: cannot move '/home/gustavolozada/Downloads/prueba' to a subdirectory of itself, 'prueba/prueba'
我只用目录得到这个。我添加了 echo "$fil" "$name"
获得更多信息,这就是它所显示的:/home/gustavolozada/Downloads/prueba prueba
我不明白是什么问题,因为命令基本上是:mv /home/gustavolozada/Downloads/prueba prueba
对我来说没有任何问题(除了它们是同一个文件,但我已经检查了目录中是否存在具有该名称的文件 if [[ ! -f "$name" ]]; then
最佳答案
只是一个奇怪的解释:自从name
在您的错误情况下,值为 prueba/prueba
, 但已通过 basename
计算,它不应包含任何斜杠。难道是Downloads/prueba
是符号链接(symbolic link)吗?
关于linux - 用于从文件名中删除空格的 Bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64311066/