2

我的目录有许多名为“20130101_temp.txt”、“20130102_temp.txt”等的文件。

如何删除所有这些文件名称中的“_temp”。即,将 20130101_temp.txt 重命名为 20130101.txt。

4

5 回答 5

7

使用 bash:

for x in *_temp.txt
do
    mv $x ${x%%_temp.txt}.txt
done

Perl(至少在 Ubuntu 上)还附带了一个实用程序,它使用rename正则表达式,因此您可以通过以下方式完成相同的操作:

rename -n 's/_temp\.txt$/.txt/' *_temp.txt

-n选项会启动“试运行”,它只会向您显示要重命名的内容。删除它以实际执行重命名。

于 2013-01-17T00:44:03.557 回答
3

使用for带有 glob 的 -loop 来查找文件并使用参数替换来删除_tempfor 移动:

for t in ????????_temp.txt; do 
    echo mv ${t} ${t/_temp/}
done

echo当您测试输出在您的系统上看起来正确时,请删除。

于 2013-01-17T00:44:30.423 回答
1

尝试这样的事情:

for FILENAME in *_temp.txt; do
    mv $FILENAME `echo $FILENAME | sed -e 's/_temp//'`
done

通常最好先mvecho.

于 2013-01-17T00:43:41.017 回答
0

这不是一个 bash 解决方案,但由于我经常遇到重命名任务,同时懒得考虑一个合理的 bash 解决方案,所以我刚刚得到了 pyRenamer,一个可以很好地完成类似事情的 GUI 工具。它通常可以从标准存储库安装。

dlundquist 解决方案虽然效果很好。

于 2013-01-17T00:54:27.690 回答
0

这对我有用:

find . -depth -name '*_temp*' -execdir bash -c 'for f; do mv -i "$f" "${f//_temp/ }"; done' bash {} +
于 2021-08-10T12:50:54.633 回答