Bash
如何在bash腳本中一一重命名文件
我有很多看起來像這樣的文件:
data1.csv data2.csv . . data(n).csv
我的案例是,當我呼叫我的腳本時,它會更改
data1.csv to data.csv
並保持原樣,下次我第二次呼叫我的腳本時,它將移動data1.csv
到已處理的文件夾並更改data2.csv
為data.csv
依此類推。
以下
data.csv
用作到所請求文件的連結以保持迭代之間的狀態。# check to see if an argument is given if [ "$#" -ne 1 ]; then echo "Illegal number of parameters" exit fi # check if ran before if so move that to processed/ directory if [ -h "data.csv" ]; then prev=`readlink data.csv` echo "Found previous run $prev" rm -f data.csv # remove link mv -f $prev processed/ # move previous file to processed directory echo "Moved to processed/$prev" fi # check to see if file exists if [ -e data$1.csv ]; then ln -s data$1.csv data.csv # link data.csv to the requested file echo "Linked data.csv -> data$1.csv" else echo "No such file data$1.csv" fi