Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got this script I called it "r1":

for f in "$@"
do
fpath=`dirname "$f"`
file=`basename "$f"`
arc=$fpath"/"$file".rar"
rar a -v1024000 -r -m0 -ep1 -o- "$arc" "$f"
done

It works well but I am not able to change archive name I wish I could do this so the outcome of:

r1 Myphoto Photos/

whould be an archive called Myphoto.rar containing Photos/ dir

How should I modify the script?

I think that this line:

file=`basename "$f"`

should be different, so it will allow me to choose archive name on my own

share|improve this question

The following script will do exactly what you are asking for.

 #!/bin/sh

 if [ ! $# -eq 2 ]
    then
    echo " Two arguments needed, a file name for the rar file "
    echo " and a directory/file name to be compressed. "
    echo " Both can be relative or absolute addresses "
    exit 1
 fi

 if [ -e $1.rar ] 
    then
    echo " File $1.rar already exists. Quitting "
    exit 2
 fi

 if [ ! -d $2 -a ! -e $2 ]
    then
    echo " $2 is not a directory/file to be compressed. Quitting "
    exit 3
 fi

 file=$1
 dir=$2
 rar a -v1024000 -r -m0 -ep1 "$file".rar  $dir
 exit

I have added an absolute minimum of error checking, and a line reminding you that you may use this script to compress both files and folders.

share|improve this answer
    
Not working with directory with long names and spaces – John Oct 28 '13 at 8:04
    
@John Not true: the problem is how you pass arguments from the command line. If you have a directory called "a b" (without apostrophes), you must issue the command as follows: r1 filename a\ b It will work then. – MariusMatutiae Oct 28 '13 at 9:10

You must log in to answer this question.

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