Como faço para renomear arquivos em um diretório, um por um, em um loop, seguindo um padrão específico definido por mim?

Como faço para renomear arquivos em um diretório, um por um, em um loop, seguindo um padrão específico definido por mim?
#!/bin/bash

ls > ls_file.txt

#item="Variation of gm with constant Vgs and constant drain current bias-mod03lec05.mp4"
############## Above line is just a comment for testing purposes 


for item in ls_file.txt

do

    var1=$(grep -o "mod[0-9]\{2\}lec[0-9]\{2\}" <<< "$item")  # extract the mod02lec03 part
    echo $var1
    var2=$(grep -o "[a-zA-Z .-]*-mod" <<< "$item")   # extract the string before it
    echo $var2
    var2=${var2%"-mod"}  # by using bash tricks, remove the mod suffix from the var2 string
    echo $var2
    var3=$var1-$var2  # Now reorder the strings
    echo $var3
    mv "$var2-$var1.mp4" "$var3.mp4" # Now rename the strings and that should work
done


####################################################################
--------------------------------------------------------------------
####################################################################

Também anexei os nomes dos arquivos no diretório em que estou trabalhando. Quero que o mod03lec10 preceda o resto da descrição e então o .mp4 vem no nome do arquivo.

--------------------------------------------------------------------
####################################################################

Common source amplifier with drain feedback bias-mod03lec10.mp4
Constraint on the gate bias resistor-mod03lec11.mp4
Constraint on the input coupling capacitor.-mod03lec12.mp4
Constraint on the output coupling capacitor.-mod03lec13.mp4
Dependence of Id on Vds-mod03lec01.mp4
Effect of gds on a common source amplifier, Inherent gain limit of a Transistor-mod03lec03.mp4
Input and output resistances of the common source amplifier with constant VGS bias-mod03lec14.mp4
Intuitive explanation of low sensitivity with drain feedback-mod03lec09.mp4
ls_files.txt
ls_file.txt
mod03lec05-Variation of gm with constant Vgs and constant drain current bias
mod03lec05-Variation of gm with constant Vgs and constant drain current bias.mp4
Negative feedback control for constant drain current bias-mod03lec06.mp4
Sense at the drain and feedback to the gate-Drain feedback-mod03lec08.mp4
Small signal output conductance of a MOS TRANSISTOR-mod03lec02.mp4
test_script
Types of feedback for constant drain current bias-mod03lec07.mp4
Variation of gm with transistors parameters-mod03lec04.mp4

Responder1

Aqui está a resposta que funciona. Eu testei.
#!/bin/bash

find . -type f -name "* *" -exec bash -c 'mv "$0" "${0// /_}"' {} \;
for item in `ls | grep mod`
do
echo $item
var1=$(grep -o "mod[0-9]\{2\}lec[0-9]\{2\}" <<< "$item")  # extract the mod02lec03 part
echo "var1 is" $var1
var2=$(grep -o "[a-zA-Z -\_]*-mod" <<< "$item") # extract the string before it
#echo $var2
var2=${var2%"-mod"} #by using bash tricks,remove the mod suffix from the var2 string
echo "var2 is" $var2
var3=$var1-$var2  # Now reorder the strings
echo "var3 is var1-var2" $var3
cp_item=$item
mv "$cp_item" "$var3.mp4" # Now rename the strings and that should work
done
### Of course, this substitutes ' ' in the filename by '_' but then we can do       the reverse to get the original filenames with the spaces.

informação relacionada