question on how to rar multiple files into there own rar file
Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
% 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.
% 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 ?
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.
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?
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.