LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-23-2007, 06:09 PM   #1
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Rep: Reputation: 30
question on how to rar multiple files into there own rar file


hey guys lets say i have a folder (on linux throw ssh/scp) and there's 10 files in there how do i add them to there own rar files?

the only way i know is

Code:
rar a -s -m5 file.rar *
but this put all files into 1 archive and i want each files to have it's own archive

any help would be very appreciated
 
Old 10-24-2007, 02:16 AM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 12,886

Rep: Reputation: 1881Reputation: 1881Reputation: 1881Reputation: 1881Reputation: 1881Reputation: 1881Reputation: 1881Reputation: 1881Reputation: 1881Reputation: 1881Reputation: 1881
If you want to rar each file in the specified directory you can use a script like this:
Code:
#!/bin/sh
for i in $(ls ssh/scp |cut -d"/" -f1);do
echo $i
rar a -s -m5 ssh/scp/$i.rar ssh/scp/$i
done
Regards
 
Old 10-24-2007, 02:34 AM   #3
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Original Poster
Rep: Reputation: 30
thanks so much for helping but that just gave lots of errors

also why did you include ssh/scp in the script i didn't mean that was a folder when i said it i was meaning well.. ssh/scp lol
 
Old 10-24-2007, 02:42 AM   #4
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Original Poster
Rep: Reputation: 30
wait a min this is working file after i edited it to

Code:
#!/bin/sh
for i in $(ls rar |cut -d"/" -f1);do
echo $i
rar a -s -m5 rar/$i.rar rar/$i
done
rar is the folder containing the files and it works now BUT only for files without spaces in them?
 
Old 10-24-2007, 02:46 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Say you have a directory with these files:
Code:
% ls
five  four  seven file.dat  six.png  three.txt  two.txt
You might do it like this:
Code:
% for f in *; do rar a -s -m5 "$f.rar" "$f"; done
[a lot of output]
% ls *.rar
five.rar  four.rar  seven file.dat.rar
six.png.rar  three.txt.rar  two.txt.rar
Note that there is a file called "seven file.dat" - note that the file has a space in the name. Because of this, you must quote the expensions of the f variable else the rar command will see separate strings for "seven" and "file.dat". It is generally good practise to quote your variable expansions for this reason.
 
Old 10-24-2007, 02:54 AM   #6
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by matthewg42 View Post
Say you have a directory with these files:
Code:
% ls
five  four  seven file.dat  six.png  three.txt  two.txt
You might do it like this:
Code:
% for f in *; do rar a -s -m5 "$f.rar" "$f"; done
[a lot of output]
% ls *.rar
five.rar  four.rar  seven file.dat.rar
six.png.rar  three.txt.rar  two.txt.rar
Note that there is a file called "seven file.dat" - note that the file has a space in the name. Because of this, you must quote the expensions of the f variable else the rar command will see separate strings for "seven" and "file.dat". It is generally good practise to quote your variable expansions for this reason.
thank you, thank you, thank you!!!!

any chance you know how to remove the original extension so file.txt becomes file.rar and not file.txt.rar ?
 
Old 10-24-2007, 03:24 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Yes. In bash you can use this syntax to remove a suffix from a variable's value: ${variablename%pattern} where pattern is a glob pattern to be removed. For example:
Code:
f=myfile.ext
echo "without extension ${f%.*}"
So the original command would change to:
Code:
% for f in *; do rar a -s -m5 "${f%.*}.rar" "$f"; done
To see other similar variable expansions, see the Parameter Expansion section of the bash manual page.
 
Old 10-24-2007, 03:32 AM   #8
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Original Poster
Rep: Reputation: 30
thank you so much one last thing if you know how to do it

lets say i have a folder with 10 random files in "/home/steve/rar" and i have a readme.txt file located at "/home/steve/readme.txt" is there anyway you can edit that command so that when every file that in the folder gets archived into it's own rar file it includes the readme.txt file as well?

i promise this is the last ting lol
 
Old 10-24-2007, 03:45 AM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Sure, just add that file after the "$f". The syntax of rar is helpful here as it does not include the path of the file by default. A happy co-incidence
Code:
% for f in *; do rar a -s -m5 "${f%.*}.rar" "$f" /home/steve/readme.txt; done
 
Old 10-24-2007, 03:47 AM   #10
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Original Poster
Rep: Reputation: 30
dude you've main my day so much easier thank you!!!
 
Old 10-24-2007, 03:51 AM   #11
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Original Poster
Rep: Reputation: 30
forget this post.. again thank you!

Last edited by steve51184; 10-24-2007 at 01:41 PM.
 
Old 10-24-2007, 01:53 PM   #12
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by matthewg42 View Post
Sure, just add that file after the "$f". The syntax of rar is helpful here as it does not include the path of the file by default. A happy co-incidence
Code:
% for f in *; do rar a -s -m5 "${f%.*}.rar" "$f" /home/steve/readme.txt; done
right that works fine but there's one thing

when i open the rar that is made i have the file.exe file (one of the random files) then i have a folder called home then in that folder i have a folder called steve then i have my readme.txt file so the rar is saving the path in the folder names but how do i stop this so it's just the 2 files?

nm got it:

Code:
for f in *; do rar a -dp -s -m5 "${f%.*}.rar" "$f" /home/steve/readme.txt; done

Last edited by steve51184; 10-24-2007 at 01:58 PM.
 
Old 10-24-2007, 03:00 PM   #13
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Oh, it didn't do that for me. What rar version/implementation are you using?
 
Old 10-24-2007, 04:23 PM   #14
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Original Poster
Rep: Reputation: 30
rar 3.60 but i fixed it with the -dp switch
 
  


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
how to deal with .rar files in linux now that rar is no longer part of linux Lleb_KCir Linux - Software 14 11-22-2008 07:01 AM
file-roller would read .rar files FC4 galen Fedora 5 07-04-2006 05:57 AM
How to open unrar? file.part1.rar - file.part2.rar Maje Linux - Software 4 05-29-2006 01:05 AM
combining multiple rar files jollyjoice Linux - Newbie 2 04-02-2005 02:49 PM
File Roller and RAR files TooTired Linux - Newbie 2 11-28-2003 02:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:33 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
Open Source Consulting | Domain Registration