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
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
01-05-2012, 04:18 PM
Johnny Rosenberg
Get the proper suffix depending on the file type.
2012/1/4 Peng Yu <pengyu.ut@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
# 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
ジョニー・*ーゼンバーグ
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
01-05-2012, 04:21 PM
Johnny Rosenberg
Get the proper suffix depending on the file type.
2012/1/5 Johnny Rosenberg <gurus.knugum@gmail.com>:
> 2012/1/4 Peng Yu <pengyu.ut@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 sure it was obvious why I wrote this at all, so just in case
I'll add that al the files in that folder had the suffix ”tmp”, but
they are actually images, html pages, flash videos and more. What I
wanted to do was to detect all the images files and copy them to
another folder. So it's not exactly the same as you want to do, but
parts of it are.
Kind regards
Johnny Rosenberg
ジョニー・*ーゼンバーグ
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
01-05-2012, 04:23 PM
Johnny Rosenberg
Get the proper suffix depending on the file type.
2012/1/5 Johnny Rosenberg <gurus.knugum@gmail.com>:
> 2012/1/5 Johnny Rosenberg <gurus.knugum@gmail.com>:
>> 2012/1/4 Peng Yu <pengyu.ut@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 sure it was obvious why I wrote this at all, so just in case
> I'll add that al the files in that folder had the suffix ”tmp”, but
> they are actually images, html pages, flash videos and more. What I
> wanted to do was to detect all the images files and copy them to
> another folder. So it's not exactly the same as you want to do, but
> parts of it are.
>
>
> Kind regards
>
> Johnny Rosenberg
> ジョニー・*ーゼンバーグ
Oh, just forgot to say that if you are not used to write scripts (you
didn't mention anything about it), always test your scripts with dummy
files in dummy folders, in case something is wrong. And always do
backups of important files. Obvious, I know, but still some people
don't.
Kind regards
Johnny Rosenberg
ジョニー・*ーゼンバーグ
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users