1

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)

1 Answer 1

1

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.

2
  • It's not working with folders with space r1 Phot My/ Photo\ giving: WARNING: No files
    – John
    Oct 28, 2013 at 8:04
  • Try enclosing the $file in quotes, like so: "$file"
    – darthbith
    Oct 28, 2013 at 12:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .