Reputation: 577
I'm trying to optimize sample.jpg with mozcjpeg, but I want it to compress sample.jpg, and not creating another file.
I run this command:
mozcjpeg -quality 80 sample.jpg > sample.jpg
and I get
Empty input file
What's the right way to do it?
P.S. I have a bash script that runs once a day to optimize images created in the last 24 hours.
Upvotes: 2
Views: 783
Reputation: 49527
anycmd foo.jpg > foo.jpg
is understood as:
foo.jpg
for write (overwriting any existing data).anycmd foo.jpg
.So, by the time the command is executed, the original file is already gone. How can you work around that?
Well, you can create a temporary file, as suggested in the other answer.
Alternatively, you can use sponge
. It's part of moreutils package.
$ whatis sponge
sponge (1) - soak up standard input and write to a file
This tool allows you to write:
mozcjpeg -quality 80 sample.jpg | sponge sample.jpg
It works because sponge
will first read all of the stdin (in memory), and only after stdin reaches EOF it will open the file for writing.
Upvotes: 2
Reputation: 5714
You’re reading the file and overwriting it at the same time. Instead, make an intermediary file:
tmp_file="$(mktemp)"
mozcjpeg -quality 80 sample.jpg > "${tmp_file}"
mv "${tmp_file}" sample.jpg
Upvotes: 1
Reputation: 2854
Just use a different output file name, otherwise it gets immediately overwritten.
Upvotes: 0