テキストファイルからファイルリストの名前を変更する

テキストファイルからファイルリストの名前を変更する

これらのファイルのリストを含むフォルダがあります。

lesson1.mp4
lesson2.mp4
lesson3.mp4
lesson4.mp4

"rename.txt"の内容に基づいてこのファイルの名前を変更しようとしています。

 1. Introduction to the React Ecosystem Video 
 2. Video Babel, Webpack, and React 
 3. Solution - Props 
 4. Solution - .map and .filter 

このスクリプトを実行しています。

for file in *.mp4; 
do read line;  
mv -v "${file}" "${line}";  
done < rename.txt

これは私に望ましくない結果を与える

'lesson1.mp4' -> '1. Introduction to the React Ecosystem Video '
'lesson10.mp4' -> '2. Video Babel, Webpack, and React '
'lesson11.mp4' -> '3. Solution - Props '
'lesson12.mp4' -> '4. Solution - .map and .filter '
'lesson13.mp4' -> '5. Video Validating Components with PropTypes'

望ましい結果。

'lesson1.mp4' -> '1. Introduction to the React Ecosystem Video.mp4'
'lesson2.mp4' -> '2. Video Babel, Webpack, and React.mp4'
'lesson3.mp4' -> '3. Solution - Props.mp4'
'lesson4.mp4' -> '4. Solution - .map and .filter.mp4'
'lesson5.mp4' -> '5. Video Validating Components with PropTypes.mp4'

ベストアンサー1

ワイルドカードの代わりにシェル拡張を使用できます。

for file in lesson{1..10}.mp4;do
       read line
       mv -v "${file}" "${line}"
done < rename.txt

エラーが発生しやすいように見えるかもしれませんが、これを行う必要があるファイルが多い場合は、ファイル名の数字が名前が変更されたファイルの行の先頭にある数字と一致することを確認できます。それは次のとおりです。

for file in *.mp4;do
       num=$(echo "${file}" | sed -E 's/^lesson([0-9]+).mp4$/\1/')
       line=$(grep -E "^ *${num}\." rename.txt)
       mv -v "${file}" "${line}"
done

このように、ファイルの順序は重要ではなく、rename.txtシェルグローバルファイル名の順序も重要ではありません。

おすすめ記事