LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Rename rar file based on filename contained in it (https://www.linuxquestions.org/questions/linux-newbie-8/rename-rar-file-based-on-filename-contained-in-it-4175475751/)

rahlquist 09-03-2013 02:23 PM

Rename rar file based on filename contained in it
 
Hello,

Have a question. I have multiple rar files with bad names say for example, "setup1.rar" and inside the rar is a better name like "Setup my folders.sh" got quite a few of these and would like to find a way to rename the rar file based on the filename inside so in the end I would have "Setup my folder.sh.rar"

Any suggestions?

John VV 09-03-2013 03:11 PM

you might want to use a descriptive name
the Microsoft default "setup" for EVERYTHING can be very annoying ,as in you have ZERO idea what it dose .

and not use blank spaces in the names of shell scripts
that is linux 101
just try to run this ???
Code:

sh ./Setup my folders.sh
-- there will be 3 errors

TB0ne 09-03-2013 03:25 PM

Quote:

Originally Posted by rahlquist (Post 5021095)
Hello,
Have a question. I have multiple rar files with bad names say for example, "setup1.rar" and inside the rar is a better name like "Setup my folders.sh" got quite a few of these and would like to find a way to rename the rar file based on the filename inside so in the end I would have "Setup my folder.sh.rar"
Any suggestions?

Easy enough to do. Just write a script to use the unrar command to list the contents of the .rar file. Once you've got the contents of the file (read the man page on the unrar command), you can just "mv <old file name> <new file name>".

rahlquist 09-03-2013 08:07 PM

This is what I cobbled together after a friend pointed me in the right direction with some help;

Code:

#!/bin/bash
for i in `ls *.rar`;
do f=`unrar lb $i`;
mv $i "`echo ${f%.}`.rar";
done;


grail 09-03-2013 09:18 PM

1. Do not use ls when simple globbing will not have issues with white space.
Code:

for i in *.rar
2. Are you positive that every rar file contains a single file? I ask as the 'lb' option will return all files inside and hence 'f' variable will now be a conglomeration of all those names with white space in between ... possibly not what you want

3. Redundant use of `echo` not required for variable substitution:
Code:

mv "$i" "${f%.}.rar"


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