一次重命名多个文件

时间:2019-02-28 00:28:30

标签: terminal rename filenames file-rename

我有一个很大的文件数据集,所有文件都这样命名:

proportional.rank_9.cycle_10157.ratio_9
proportional.rank_9.cycle_10158.ratio_0
proportional.rank_9.cycle_10159.ratio_130
proportional.rank_9.cycle_10160.ratio_7

但是当然,事后看来,这将使读取文件变得困难,因为我需要文件名,并且每个文件名的比率可能不同。有没有一种方法可以一次重命名所有文件,以使ratio_ *消失?

理想情况下,我希望他们采用以下形式:

proportional.rank_9.cycle_10157
proportional.rank_9.cycle_10158
proportional.rank_9.cycle_10159
proportional.rank_9.cycle_10160

1 个答案:

答案 0 :(得分:0)

//set X to all files of the form proportional.rank
X=$(ls proportional.rank*) 

//print out to check X is set
echo $X 

//loop over all files of X
for i in $X ; do
  before_stem=${i%.ratio_*} //what I want to keep
  after_stem=${i#*.ratio_}  //what I don't want
  new_end =$(printf ".out" $after_stem) //give new ending
  mv $i ${before_stem}${new_end} //concatenate the new ending and old beginning
done

%:采取所有遵循%的模式之前的

磅符号:紧跟#

之后的模式

printf:就像C printf

proportional.rank_9.cycle_10157.ratio_9
proportional.rank_9.cycle_10158.ratio_0
proportional.rank_9.cycle_10159.ratio_130
proportional.rank_9.cycle_10160.ratio_7

成为

proportional.rank_9.cycle_10157.out
proportional.rank_9.cycle_10158.out
proportional.rank_9.cycle_10159.out
proportional.rank_9.cycle_10160.out