找到文件并将其重命名为BASH

找到文件并将其重命名为BASH,bash,Bash,你知道为什么这样不行吗?我检查了很多链接,我不明白为什么我认为语法是正确的。 我想找到文件maplist.txt.old,然后将其重命名为同一文件夹中的maplist.txt。我没有错 find ~ -type f -name csgo/maplist.txt.old -execdir mv csgo/maplist.txt.old maplist.txt \; 有很多方法可以解决这个问题。由于您正在查找~/csgo,因此可以直接转到“查找”中的目录。-execdir选项将在目录中运行该命令。

你知道为什么这样不行吗?我检查了很多链接,我不明白为什么我认为语法是正确的。 我想找到文件maplist.txt.old,然后将其重命名为同一文件夹中的maplist.txt。我没有错

find ~ -type f -name csgo/maplist.txt.old -execdir mv csgo/maplist.txt.old maplist.txt \;

有很多方法可以解决这个问题。由于您正在查找~/csgo,因此可以直接转到“查找”中的目录。-execdir选项将在目录中运行该命令。因此,在不改变您的示例的情况下:

find ~/csgo -type f -name maplist.txt.old -execdir mv maplist.txt.old maplist.txt \;
要进一步自动化此操作,您可能需要使用bash for循环来处理此操作,例如:

for file in $( find ~/csgo -type f -name maplist.txt.old ) ; do
  mv $file $( echo $file | sed -e 's/\.old//' )
done

csgo/maplist.txt.old无效,您必须使用文件名,而不是匹配完整路径所需的路径,类似于
*csgo/maplist.txt.old
。我不明白循环的好处是什么,它与第一个命令的作用相同,但健壮性要差得多(带空格的文件名的分隔符,具有不带引号的扩展,可以简化为使用参数扩展而不是
echo | sed
,sed命令应转义
,并在字符串末尾锚定
old
)优点是您只需为文件命名一次,并且您可以在将来为其他文件更轻松地对其进行调整。您可以使用此选项:
-execdir sh-c'mv“$1”“${1%.old}”'{}\
是的,这将扩展到
mv filename.txt.old filename.txt
,其中
$1
filename.txt.old