FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 01-04-2012, 07:42 PM
Peng Yu
 
Default Get the proper suffix depending on the file type.

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

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 01-05-2012, 04:18 PM
Johnny Rosenberg
 
Default 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

# 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
ジョニー・*ーゼンバーグ

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 01-05-2012, 04:21 PM
Johnny Rosenberg
 
Default 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
 
Old 01-05-2012, 04:23 PM
Johnny Rosenberg
 
Default 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
 

Thread Tools




All times are GMT. The time now is 10:34 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org