如何使用存储在变量中的部分名称重命名文件

时间:2015-05-27 17:33:28

标签: bash

大家好,我想知道如何使用存储在变量中的部分名称来重命名文件。为了更清楚,我将向您展示一个例子。 假设我必须遍历所有名为test _ * .txt的文件,因此test_1.txt,test_2.txt ... test_45.txt等等。 在循环中的某个点我需要根据test_1.txt文件中的数字重命名test-name1.txt,test-name2.txt中的文件test-name.txt

tv.setText(question.toString()+""); 

2 个答案:

答案 0 :(得分:2)

这应该有效:

for tst in test_*.txt
do
    nr=${tst#test_}  # Delete 'test_' from beginning of file name in tst
    nr=${nr%.txt}    # Delete '.txt' from end of file name in tst
    mv test_name.txt test_name_${nr}.txt
done

答案 1 :(得分:0)

你也可以在bash中使用模式匹配:

[[ $file =~ test_(.*).txt ]];
mv test_name.txt test_name${BASH_REMATCH[1]}.txt
相关问题