1

Hy,

I'm trying to optimize the uploaded pictures on my webserver, with imgopt. The problem is that, when it finds a file with whitespace in the name, it throw error like that:

$imgopt file - name.jpg
stat: cannot stat 'file': No such file or directory
stat: cannot stat 'name.jpg': No such file or directory

Can anyone help me please?

Thanks, Dave.

Dávid Pörös
  • 83
  • 2
  • 11

2 Answers2

0

You're actually passing three options to the imgopt program :

  • file
  • -
  • name.jpg

The program probably treats each one of them as a separate file name (with - being stdin, following the standard Unix convention) and tries to open them. This, of course, fails. What you want are quotes :

imgopt "file - name.jpg"

This way, only one argument is given to the program and it contains a valid filename, with all the whitespace-y goodness.

Daniel Kamil Kozar
  • 18,476
  • 5
  • 50
  • 64
  • The script is borked (assuming that we're talking about https://raw.github.com/kormoc/imgopt/master/imgopt ). It doesn't properly pass filenames with whitespace to other programs that it uses (`stat` and `file` stand out the most) - by forgetting to use quotes. You're best off chucking that sorry excuse of a script and manually launching the programs that it launches. – Daniel Kamil Kozar Sep 30 '13 at 08:16
0

You can still use 'imgopt' but you'll have to rename your files with a command like this:

find your_folder -depth -name "* *" -execdir rename 's/ /_/g' "{}" \;

See the source for more details about renaming directories, etc...

Second answer: you can also pass file names with spaces with a command like this (source):

for fname in "$@"; do
  process-one-file-at-a-time "$fname"
done
Community
  • 1
  • 1
philshem
  • 24,761
  • 8
  • 61
  • 127
  • 1
    Thanks. Before rename, I have to copy to a tmp dir, cause it's an online website, and we don't want some broken pictures ;). – Dávid Pörös Sep 30 '13 at 09:28
  • Good, then renaming isn't an issue. I added a second answer that allows you to pass files with whitespace in their names to another command. – philshem Sep 30 '13 at 09:30
  • 1
    With renaming, it works good, just have to copy to a temp folder and rename after optimize to the original file name (I replaced thw whitespaces with ";something;" so it is easy to find the olda whitespaces). In other way, it's a bit safer, in case of the optimizer makes a mistake! :) – Dávid Pörös Sep 30 '13 at 09:36