Bash Script Not allowing special symbols in zipping a file

64 views Asked by At

I am actually creating a zip file based on a pdf file that I have downloaded through java code, so the problem statement is like this.

I have a pdf file with special symbols = (wallbay)+1-50 m+ ipad+i pho ne%watch.pdf

Now I want this file to be created as a zip file which should have a name = (wallbay\)+1-50m+ipad+iphone%watch.pdf.zip

I tried below commands :

cd /Users/rkr5/Downloads/admin && zip --symlinks -r \(wallbay\)+1-50m+ipad+iphone%watch.pdf.zip   \(wallbay)+1-50 m+ ipad+i pho ne%watch.pdf
                        
cd /Users/rkr5/Downloads/admin && zip --symlinks -r "\(wallbay)+1-50 m+ ipad+i pho ne%watch.pdf.zip"   "\(wallbay)+1-50 m+ ipad+i pho ne%watch.pdf"
                        
cd /Users/rkr5/Downloads/admin && zip --symlinks -r \(wallbay)+1-50 m+ ipad+i pho ne%watch.pdf.zip   "\(wallbay)+1-50 m+ ipad+i pho ne%watch.pdf"

None of them are working and throwing error like "parse error near ``)'"

The only command that works is

cd /Users/rkr5/Downloads/admin && zip --symlinks -r \(wallbay\)+1-50m+ipad+iphone%watch.pdf.zip    \(wallbay\)+1-50m+ipad+iphone%watch.pdf

But above commands work only when there is no whitespaces in pdf filename.

Currently my folder structure is

/Users/rkr5/Downloads/admin/(wallbay)+1-50m+ipad+iphone%watch.pdf.zip
/Users/rkr5/Downloads/admin/(wallbay)+1-50 m+ ipad+i pho ne%watch.pdf

I am using MacOs currently..

1

There are 1 answers

1
jaume On BEST ANSWER

Since the names of the files contain characters that are interpreted by the shell (like ( and )) or white space, you need to either escape or quote them.

I'd recommend quoting: it's easier and requires that you just surround the unmodified filename with double quotes (") as follows:

cd /Users/rkr5/Downloads/admin && zip --symlinks -r "(wallbay)+1-50 m+ ipad+i pho ne%watch.pdf.zip" "(wallbay)+1-50 m+ ipad+i pho ne%watch.pdf"

If you prefer, you can use a shell variable to avoid repeting the filename twice:

filename="(wallbay)+1-50 m+ ipad+i pho ne%watch.pdf"
cd /Users/rkr5/Downloads/admin && zip --symlinks -r "$filename.zip" "$filename"

(Since you are compressing only one file, the option -r is not necessary.)

Why don't your commands work?

This command:

cd /Users/rkr5/Downloads/admin && zip --symlinks -r \(wallbay\)+1-50m+ipad+iphone%watch.pdf.zip   \(wallbay)+1-50 m+ ipad+i pho ne%watch.pdf

doesn't work because white space and the second parenthesis in the PDF filename are not escaped.

This command:

cd /Users/rkr5/Downloads/admin && zip --symlinks -r "\(wallbay)+1-50 m+ ipad+i pho ne%watch.pdf.zip"   "\(wallbay)+1-50 m+ ipad+i pho ne%watch.pdf"

doesn't work because it is both quoted and (partially) escaped.

This command:

cd /Users/rkr5/Downloads/admin && zip --symlinks -r \(wallbay)+1-50 m+ ipad+i pho ne%watch.pdf.zip   "\(wallbay)+1-50 m+ ipad+i pho ne%watch.pdf"

doesn't work because the it is both quoted and (partially) escaped, and a double quote is missing at the end of the name of the zip archive.