LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell command to rename multiple archives (https://www.linuxquestions.org/questions/linux-general-1/shell-command-to-rename-multiple-archives-946302/)

lghizoni 05-22-2012 08:53 AM

Shell command to rename multiple archives
 
Hello all,

I'm a Debian user. I do have 1148 txt archives with different names, and I want to change their names. More specificaly, I just want to delete 1 letter from the archives. The names are:

WFI_FM1_DB_OD_B13_G100_I25_20120227_R_M_1_002.txt
WFI_FM1_DB_OE_B14_G100_I75_20120227_R_M_1_002.txt
WFI_FM1_DB_OD_B15_G159_I50_20120227_R_R_5_006.txt
WFI_FM1_DB_OD_B15_G159_I50_20120227_R_R_4_005.txt
WFI_FM1_DB_OD_B13_G100_I25_20120227_R_M_2_003.txt
WFI_FM1_DB_OE_B14_G100_I75_20120227_R_M_2_003.txt

etc...and I want to delete that R.

Does anyone know how to do it in shell with one command??

Thanks!

Snark1994 05-22-2012 09:57 AM

Should be fairly easy. The "rename" command may or may not do what you want it to do - I've found it to vary wildly between different distros. See what "man rename" says. However, otherwise you should be able to run this:

Code:

for i in WFI_FM1*.txt; do
    mv $i $(echo $i | sed 's/_R\(_[^_]\+_[^_]\+_[^_]\+.txt\)$/\1/')
done;

I don't know exactly what defines the archives' names, but these are the assumptions I've made:
  • They all begin with WFI_FM1 and end in .txt
  • The _R that you want to remove is followed by "_<some characters>_<some characters>_<some characters>.txt".

If these two are true, then the code should do what you want it to do. Hope this helps,

lghizoni 05-22-2012 12:35 PM

It works great!! Thank you!

Cheers.

Quote:

Originally Posted by Snark1994 (Post 4684819)
Should be fairly easy. The "rename" command may or may not do what you want it to do - I've found it to vary wildly between different distros. See what "man rename" says. However, otherwise you should be able to run this:

Code:

for i in WFI_FM1*.txt; do
    mv $i $(echo $i | sed 's/_R\(_[^_]\+_[^_]\+_[^_]\+.txt\)$/\1/')
done;

I don't know exactly what defines the archives' names, but these are the assumptions I've made:
  • They all begin with WFI_FM1 and end in .txt
  • The _R that you want to remove is followed by "_<some characters>_<some characters>_<some characters>.txt".

If these two are true, then the code should do what you want it to do. Hope this helps,



All times are GMT -5. The time now is 09:20 PM.