Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need a script to rar my files

usage will be like this

r1 archivename fileordirtocompress

with following attributes

rar a -m0 -v1024000 -r 

How whould this bash script look like?

#!/bin/bash

for file in "$*"
do

rar a -v10000k archive.rar $file

done

I found something like that but I wish I could change the archive name

examples:

r1 MyPhotos Myphotos/ = MyPhotos.rar (Myphotos/ inside with subfolders)

r1 Nicemusic *.mp3 = Nicemusic.rar (all .mp3 inside)

r1 Familyvideo movie1.avi = Familyvideo (movie1.avi file inside)

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You can use the variable $file to name the archive as well.

#!/bin/bash

for file in "$*"
do

    rar a -m0 -v1024000 -r ${file}.rar $file

done

Note that I changed the options to rar to what you said you wanted. The for loop stores every file it finds in * (i.e. every file in the current directory) in the variable $file, one by one, as it goes through the loop. You need the {} around the name of the variable before .rar to make sure bash doesn't think you're trying to find a variable called $file.rar.

share|improve this answer
    
It's not working with folders with space r1 Phot My/ Photo\ giving: WARNING: No files –  John Oct 28 '13 at 8:04
    
Try enclosing the $file in quotes, like so: "$file" –  darthbith Oct 28 '13 at 12:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.