Bash

如何在bash腳本中一一重命名文件

  • December 13, 2014

我有很多看起來像這樣的文件:

data1.csv
data2.csv
.
.
data(n).csv

我的案例是,當我呼叫我的腳本時,它會更改data1.csv to data.csv並保持原樣,下次我第二次呼叫我的腳本時,它將移動data1.csv到已處理的文件夾並更改data2.csvdata.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

引用自:https://unix.stackexchange.com/questions/174049