【问题标题】:Rename all the files in the folder with increasing numbers将文件夹中的所有文件重命名为递增的数字
【发布时间】:2011-12-23 17:19:45
【问题描述】:

我有一个文件夹,在该文件夹中我有 10-15 个任意名称的文件。 文件名中可能包含空格。例如:wWw.page.com __ (576)_002。 在终端中,当我按w 然后按tab 时,文件名显示如下:wWw.page.com\ \ __\ \(576\)_0.txt

我想要一些脚本来重命名我的所有文件,例如0.txt1.txt2.txt 等等。

我的问题是:wWw.page.com __ (576)_002.txt file not found

index=0;
for i in $(ls *.txt)
do
    cp "${i}"  $index".txt" 
done

【问题讨论】:

  • perl -e'rename $_, $i++.q(.txt) while(<*.txt>)'

标签: linux bash shell terminal


【解决方案1】:

而不是ls 尝试glob

index=0;
for name in *.txt
do
    cp "${name}" "${index}.txt"
    index=$((index+1))
done

【讨论】:

  • 而且你不需要内部的$ (index=$((index+1)))
  • @MichaelKrelin-hacker 谢谢! :-)
  • 是的,为什么会这样:在命令替换时,bash 会根据IFS 破坏命令输出——对于 glob 则不然。
  • @MichaelKrelin-hacker 我很少写脚本。有没有更好的方法来增加索引?
  • @cnicutar,首先,就像我在括号中所说的那样。或者,你可以直接((++index))
猜你喜欢
  • 2013-04-24
  • 1970-01-01
  • 2016-06-28
  • 1970-01-01
  • 2016-07-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多