Cortando nomes de arquivos do final

3

Eu tenho uma pasta com o seguinte arquivo mp4 que precisa ser renomeado

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

Agora eu quero cortar as palavras aleatórias, começando de - até .mp4 , de modo que esse nome de arquivo seja convertido de The BioSand Water Filter-SlqAitfBbdQ.mp4 to The BioSand Water Filter.mp4

para isso eu obtive sucesso parcial como mostrado abaixo

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

Agora, quando eu adiciono a última peça, tudo se resume a nada?

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
    
por nightcrawler 24.01.2014 / 19:02

2 respostas

3

Como seus nomes de arquivos contêm espaços, você precisa citá-los antes de passá-los para mv , para evitar divisão de palavras :

 ... mv "$filename" "$Out" ...
    
por 24.01.2014 / 19:21
6

Parece que os nomes dos arquivos estão todos nesse formato:

filename-RandString.mp4

Nesse caso, você pode simplesmente excluir tudo do último - para .mp4 :

for f in /media/usama/CRUNCH/RO_Plant/RO_Videos/*-*mp4; do
    mv "$f"  "${f%-*mp4}.mp4"; 
done

O formato ${string%substring} exclui a correspondência mais curta de $substring da parte de trás de $string .

Você também pode fazer isso usando (Perl) rename :

rename 's/-[^-]*?mp4$/.mp4/' /media/usama/CRUNCH/RO_Plant/RO_Videos/*-*mp4
    
por 24.01.2014 / 19:14

Tags