5
3
I have a bunch of image files from misnamed scans/faxes I need to fix for our Linux users. It turns out we have a bunch of scans that are PNG files that are labled as *.jpg visa versa. Under Windows this was never an issue as Explorer/Office would just ignore the extension. But under Linux, Eye of GNOME etc end up just refusing to open the file because the contents don't match the extension.
Does anyone have any recommendations for a tool or a little piece of script that could do this? I could write a C program to do this, but that seems a bit overkill. Just sitting down and manually renaming manually isn't an option, there are thousands.
Edit: I see the file
command will look at the actual contents of the file and display what it is. I'm not quite sure how to use the information from it though.
1That looks pretty close to the script I started working on. But when I try run that script I get
??? what is this: *.{jpg,JPG,png,PNG,jpeg,JPEG}
– BlamKiwi – 2014-09-16T13:24:11.6901@MorphingDragon What shell or version of Bash are you using? Did you disable brace expansion? Or is there no such file in your directory? Run
shopt -s nullglob
before, maybe, so that the glob doesn't expand in that case. – slhck – 2014-09-16T13:27:47.2874.3.11 (Mint 17) – BlamKiwi – 2014-09-16T13:28:54.360
Instead of the braces you can use find commands, so the first script line would be
for f in $(find -iname "*.jpg") $(find -iname "*.png") $(find -iname "*.jpeg"); do
. This also allows the files to be in subdirectories, though it would be a lot more complicated if there are blanks in any of the directory or file names. – AFH – 2014-09-16T14:11:38.690This is bash syntax, so do not
sh script.sh
, usebash script.sh
– glenn jackman – 2014-09-16T14:32:15.8401@AFH yes and no. That will break if the files contain whitespace which is why a glob is preferred. If you use
find
, you should at least combine it with-print0
andwhile IFS= read -d''
. – terdon – 2014-09-16T16:03:44.470@terdon
read -r
actually :) (to be super precise) – slhck – 2014-09-16T17:09:48.743@slhck true, I keep forgetting backslashes. – terdon – 2014-09-16T17:10:43.627
This almost worked for me in Bash 4.3.39 with GNU grep 2.21 on Chakra Linux. I had to change the regex after it kept failing on, e.g.,
JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, Exif Standard: [TIFF image data, little-endian, direntries=2, software=Google], baseline, precision 8, 1467x1586, frames 3
. I don't know Perl regex, so I used two regexes in sequence:egrep -o '(JPEG|PNG) image' | egrep -o 'JPEG|PNG'
. Is there a way for a single regex to handle "JPEG TIFF" cases like this, without hardcoding the allowed formats? – Mark Haferkamp – 2016-02-05T10:24:22.240As given, the routine failed for PNG files using file 5.0.3 on Ubuntu 10.04: Most image files return "image data" when tested by file, but PNG images return just "image". By removing "data" in the second line, grep finds all images. – Andrew P. – 2017-01-20T17:40:31.190