0

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

Nic3500
  • 8,144
  • 10
  • 29
  • 40
  • The first edit is free, have a look so you know for future questions. The easiest way to tackle this is to write a script that will cleanup your filenames (removing or replacing special chars and spaces). Otherwise everything you do will be hindered by these filenames. Your question has 2 aspects. Renaming the file, and processing special characters. One generic method you could use is with `find .... -print0`, as described here: http://mywiki.wooledge.org/BashFAQ/020 – Nic3500 Apr 25 '21 at 09:25

1 Answers1

0

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.

jaume
  • 381
  • 1
  • 5
  • 11
  • @RajeshKumar I'm glad I could help, if you think my answer solves your issue, please consider marking it as accepted, it will help others with a similar issue. Thanks! – jaume Apr 27 '21 at 10:08