1
#!/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


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

Also I have attached the names of the files in the directory I am working in. I want the mod03lec10 to precede the rest of the description then the .mp4 comes in the filename.

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

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
4
  • This is not about Ubuntu. Try unix.stackexchange.com
    – heynnema
    Mar 15, 2020 at 20:17
  • I don't understand what your second code block is supposed to be telling us, but it sounds like you want something like this rename -n 's/(.*?)-(mod\d\dlec\d\d)/$2-$1/' *.mp4 using the Perl rename implementation (-n included for testing) Mar 15, 2020 at 21:11
  • @steeldriver- I gave the file listing of my directory in my 2nd code block. So, these are the files I want to operate on. Thanks for the code. I will try this out. Mar 16, 2020 at 8:57
  • @steeldriver- Yeah, you are right ! Your solution does exactly what I want in fewer lines of code. Mar 16, 2020 at 9:05

1 Answer 1

1
Here is the answer that works. I have tested it.
#!/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.
2
  • Hello, why you are still using ls | grep mod instead of $(find . -type f -name "*mod*")? :)
    – pa4080
    Mar 16, 2020 at 9:42
  • Yes the find command would be better but ls seems quick and dirty although it doesn't screen for files directly. Mar 16, 2020 at 10:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .