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'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

1 Answer 1

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

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.