LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-03-2013, 03:23 PM   #1
rahlquist
LQ Newbie
 
Registered: Nov 2012
Posts: 14

Rep: Reputation: Disabled
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?
 
Old 09-03-2013, 04:11 PM   #2
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,225

Rep: Reputation: 2521Reputation: 2521Reputation: 2521Reputation: 2521Reputation: 2521Reputation: 2521Reputation: 2521Reputation: 2521Reputation: 2521Reputation: 2521Reputation: 2521
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

Last edited by John VV; 09-03-2013 at 04:13 PM.
 
Old 09-03-2013, 04:25 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,267

Rep: Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440
Quote:
Originally Posted by rahlquist View Post
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>".
 
1 members found this post helpful.
Old 09-03-2013, 09:07 PM   #4
rahlquist
LQ Newbie
 
Registered: Nov 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
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;

Last edited by rahlquist; 09-03-2013 at 09:09 PM.
 
Old 09-03-2013, 10:18 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
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"
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File Rename Based on Text Within File in a Particular Location Nargousias Linux - Newbie 3 06-11-2013 03:50 PM
[SOLVED] how to rename a file based on it creation date limdel Linux - Newbie 11 09-23-2009 07:41 AM
Create folder from .rar or .r00 file with filename excluding the extension (usinBash) Techno Guy Linux - Newbie 7 01-04-2009 01:13 AM
Rename file based on its own date/time stamp airman99 Linux - General 19 09-05-2006 08:52 AM
auto file rename based on time wx_jason Linux - Newbie 10 07-10-2003 12:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration