Bash – Trimming Filenames from the end

bashrename

I have a folder with following mp4 file which are needed to be renamed

for filename in /media/usama/CRUNCH/RO_Plant/RO_Videos/*; do
    File1=$filename
    echo $File1
done
/media/usama/CRUNCH/RO_Plant/RO_Videos/Desal RO 16' Desalination Plants-JdN_Yx_5HzI.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/How Desalination Works-_H8EDLFNDtI.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/how membrane filter works with water-M3mpJysa6zQ.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/Hydranautics - Spiral Wound Reverse Osmosis Elements-YlMGZWmh_Mw.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/Industrial Reverse Osmosis Startup Part 1 of 3-6xD9FhbAIvY.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/Industrial Reverse Osmosis Startup Part 2 of 3-Dt23yzCXaVA.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/Industrial Reverse Osmosis Startup Part 3 of 3-VXyK20P6HD4.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/MED Desalination Process-5nDcxhkq8Js.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/Reverse Osmosis Membrane Replacement Procedure-zNXYkH3uj-I.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/Reverse Osmosis or RO System-BeXHKpuHVZg.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/Reverse Osmosis Pressure Vessel-BeXHKpuHVZg.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/RO Process Equipments by R S Engineering, Mumbai-ijJ6QyaQ-Bk.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/The BioSand Water Filter-SlqAitfBbdQ.mp4

Now I want to trim the random words starting from till .mp4 such that this filename is converted from
The BioSand Water Filter-SlqAitfBbdQ.mp4 to The BioSand Water Filter.mp4

for this I got partial success as is shown below

for filename in /media/usama/CRUNCH/RO_Plant/RO_Videos/*; do
    File1=$filename
    Out=${File1:0:${#File1}-16}".mp4"
    echo $Out
done
/media/usama/CRUNCH/RO_Plant/RO_Videos/Desal RO 16' Desalination Plants.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/How Desalination Works.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/how membrane filter works with water.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/Hydranautics - Spiral Wound Reverse Osmosis Elements.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/Industrial Reverse Osmosis Startup Part 1 of 3.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/Industrial Reverse Osmosis Startup Part 2 of 3.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/Industrial Reverse Osmosis Startup Part 3 of 3.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/MED Desalination Process.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/Reverse Osmosis Membrane Replacement Procedure.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/Reverse Osmosis or RO System.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/Reverse Osmosis Pressure Vessel.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/RO Process Equipments by R S Engineering, Mumbai.mp4
/media/usama/CRUNCH/RO_Plant/RO_Videos/The BioSand Water Filter.mp4

Now when I add the last piece, it boils down to nothing?

for filename in /media/usama/CRUNCH/RO_Plant/RO_Videos/*; do
    File1=$filename
    Out=${File1:0:${#File1}-16}".mp4"
    echo $Out
    mv $filename $Out
done
/media/usama/CRUNCH/RO_Plant/RO_Videos/Desal RO 16' Desalination Plants.mp4
mv: target ‘Plants.mp4’ is not a directory
/media/usama/CRUNCH/RO_Plant/RO_Videos/How Desalination Works.mp4
mv: target ‘Works.mp4’ is not a directory
/media/usama/CRUNCH/RO_Plant/RO_Videos/how membrane filter works with water.mp4
mv: target ‘water.mp4’ is not a directory
/media/usama/CRUNCH/RO_Plant/RO_Videos/Hydranautics - Spiral Wound Reverse Osmosis Elements.mp4
mv: target ‘Elements.mp4’ is not a directory
/media/usama/CRUNCH/RO_Plant/RO_Videos/Industrial Reverse Osmosis Startup Part 1 of 3.mp4
mv: target ‘3.mp4’ is not a directory
/media/usama/CRUNCH/RO_Plant/RO_Videos/Industrial Reverse Osmosis Startup Part 2 of 3.mp4
mv: target ‘3.mp4’ is not a directory
/media/usama/CRUNCH/RO_Plant/RO_Videos/Industrial Reverse Osmosis Startup Part 3 of 3.mp4
mv: target ‘3.mp4’ is not a directory
/media/usama/CRUNCH/RO_Plant/RO_Videos/MED Desalination Process.mp4
mv: target ‘Process.mp4’ is not a directory
/media/usama/CRUNCH/RO_Plant/RO_Videos/Reverse Osmosis Membrane Replacement Procedure.mp4
mv: target ‘Procedure.mp4’ is not a directory
/media/usama/CRUNCH/RO_Plant/RO_Videos/Reverse Osmosis or RO System.mp4
mv: target ‘System.mp4’ is not a directory
/media/usama/CRUNCH/RO_Plant/RO_Videos/Reverse Osmosis Pressure Vessel.mp4
mv: target ‘Vessel.mp4’ is not a directory
/media/usama/CRUNCH/RO_Plant/RO_Videos/RO Process Equipments by R S Engineering, Mumbai.mp4
mv: target ‘Mumbai.mp4’ is not a directory
/media/usama/CRUNCH/RO_Plant/RO_Videos/The BioSand Water Filter.mp4
mv: target ‘Filter.mp4’ is not a directory

Best Answer

Since your file names contain spaces, you need to quote them before passing them to mv in order to avoid word splitting:

 ... mv "$filename" "$Out" ...
Related Question