Get the proper suffix depending on the file type.
Johnny Rosenberg
gurus.knugum at gmail.com
Thu Jan 5 17:18:00 UTC 2012
2012/1/4 Peng Yu <pengyu.ut at gmail.com>:
> Hi,
>
> The command 'file' print human readable file format. But I need to get
> the correct suffixes for some files with incorrect suffixes. I could
> parse the output of 'file' and then map to the correct suffixes. But
> I'm wondering if there is any more automatic way of doing so? Thanks!
>
> --
> Regards,
> Peng
I made a script the other day, that copies image files from the Opera
cache folder (Opera the web browser). All the files there are named
something like oprXXXXX.tmp where each X can be an upper case letter
or a number. Since I only copied images I used Image Magick (identify)
instead of file, but I guess file could be used as well, if the code
is adjusted accordingly.
I'm not a programmer or something, at least not a good one, so I don't
say this is the best way to write a script like this, but maybe it can
be of some kind of inspiration anyway, I don't know. Here it is, do
whatever you want with it…
#!/bin/bash
# Copies image files from the Opera cache folder to a place of your choice.
# Images with a resolution less than MINPIXELS will not be copied.
#
# Bash, ImageMagick and Zenity (2.32 or later) needs to be installed.
#
# Johnny Rosenberg
# 2011-12-31
CACHEDIR="${HOME}/.opera/cache"
# If your desktop is called something else than ”Desktop”, change the
line below accordingly.
DESTDIR="${HOME}/Desktop"
TMPFILE="${HOME}/OperaTmpFiles"
MINPIXELS=18000
# Select a destination folder.
DESTDIR=$(zenity --file-selection \
--filename="${DESTDIR}/" \
--title="Select folder" \
--directory \
--confirm-overwrite)
# Make a list of all the tmp files in the folder and all its sub folders.
find "${HOME}/.opera/cache/" -name "*.tmp" > "${TMPFILE}"
# Count the files.
FileCount=$(cat "${TMPFILE}" | wc -l)
# Do the actual work.
cat "${TMPFILE}" | while read File; do
Result=$(identify ${File} 2> /dev/null | head -n 1)
if [[ $Result ]]; then
Type=$(echo "${Result}" | awk -F " " '{print $2}')
Size=$(echo "${Result}" | awk -F " " '{print $3}')
Width=$(echo "${Size}" | awk -F "x" '{print $1}')
Height=$(echo "${Size}" | awk -F "x" '{print $2}')
let Pixels=Width*Height
if [[ Pixels -gt MINPIXELS ]]; then
BaseName=${File##*/} # FileName.suffix
cp "${File}" "${DESTDIR}"/"${BaseName%.*}.${Type,,}"
fi
fi
let FileNo+=1
let Percentage=100*FileNo/FileCount
echo $Percentage
done |
zenity --progress \
--title="Progress" \
--text="Managing your files. Don't worry, I know what I'm doing…" \
--percentage=0 \
--auto-close \
--no-cancel
rm "${TMPFILE}"
# END OF CODE ###################################
Maybe something in the code above can be of any use, I don't know.
Kind regards
Johnny Rosenberg
ジョニー・ローゼンバーグ
More information about the ubuntu-users
mailing list