Prev: how to find and omit a directory containing a file entirely
Next: Directory exclusion from the output of 'echo **'
From: Lao Ming on 18 May 2010 20:02 On May 14, 3:16 am, pk <p...(a)pk.invalid> wrote: > Guillaume Dargaud wrote: > > Hello all, > > I would like some way to escape file names I get from something like that: > > (this is bash syntax) > > > # Get the 10th file in the archive > > Image=$( zipinfo -1 "$1" | head -10 | tail -1 ) > > unzip -p "$1" "$Image" | pipe it somewhere > > > The problem is that is $Image contains one of [ ] { } ( ) < > ' " \, etc > > Then the 2nd line fails > > According to the man page, unzip uses the same wildcard characters as the > shell: "*", "?" and "[...]", so those are the only characters that need to > be escaped. What about the dollar sign? Dollar signs can be used in the OSX Finder so they can be encountered in the shell. Excellent tip though. Thanks a bunch. > With bash, you can use printf "%q" to correctly escape a filename the way > the shell would: > > $ image='myfile[]with?funny*chars.txt' > $ printf "%q\n" "$image" > myfile\[\]with\?funny\*chars.txt > > $ image=$(printf "%q" "$image") > $ unzip myzip.zip "$image" > Archive: myzip.zip > extracting: myfile[]with?funny*chars.txt > > That should be enough for your situation. If not, please provide a real > example of names that don't actually work.
From: Guillaume Dargaud on 21 May 2010 04:27
> Here is the problem (I can reproduce it on my system as well). It seems > that > unzip and zipinfo do not show multibyte characters in file names > correctly. > Once the file is added to the zip archive, unzip and zipinfo show those > characters as question marks. Thanks, that confirms my suspicion. I had to use a different and much slower method: extract all files to a temp directory, and get the names from there... -- Guillaume Dargaud http://www.gdargaud.net/ |