Unix Shell - Script required to rename files

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2004 > Script required to rename files





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Script required to rename files
virus

2004-12-25, 7:22 pm

I'd like to have a script that renames all those strange filenames that
my digital camera gives out.
ie my digital camera supplies a file like 100_0104.jpg and I'd like to
search for all such files and delete the first 4 characters and leave
only 0104.jpg - maybe substitute the 1st 4 character to something more
useful like the date ?

thanks

Grant


--

No virus checking was performed on this outgoing email
Since no Microsoft products were used in its generation


Powered by :-






















#####
# # # ## #### # # # # ## ##### ######
# # # # # # # # # # # # # # #
##### # # # # #### # # # # # # #####
# # ###### # # # # ## # ###### ##### #
# # # # # # # # # ## ## # # # # #
##### ###### # # #### # # # # # # # # ######

# ### ###
## # # # #
# # # # # #
# # # # #
# # # ### # # ###
# # # ### # # ###
##### ### ### ### ###
Janis Papanagnou

2004-12-25, 7:22 pm

virus wrote:
> I'd like to have a script that renames all those strange filenames that
> my digital camera gives out.
> ie my digital camera supplies a file like 100_0104.jpg and I'd like to
> search for all such files and delete the first 4 characters and leave
> only 0104.jpg - maybe substitute the 1st 4 character to something more
> useful like the date ?


find /your/camera/directory -type f -name "*.jpg" -print |
while read f
do
mv -i "$f" "$(date +%Y%m%d)_${f#????}"
done

You may want to change the "*.jpg" search pattern (if you have other
jpg-files in the directory tree that you don't want to rename) to
something like "100_[0-9][0-9][0-9][0-9].jpg".

Janis
Chris F.A. Johnson

2004-12-25, 7:22 pm

On Sat, 25 Dec 2004 at 22:16 GMT, virus wrote:
> I'd like to have a script that renames all those strange filenames that
> my digital camera gives out.
> ie my digital camera supplies a file like 100_0104.jpg and I'd like to
> search for all such files and delete the first 4 characters and leave
> only 0104.jpg - maybe substitute the 1st 4 character to something more
> useful like the date ?


date=`date +%Y-%m-%d`
for file in 100_*.jpg ## adjust pattern to taste
do
newname=${date}_${file#????} ## use date in place of first 4 chars
mv "$file" "$newname"
done

Or, to use the dates on the files themselves:

eval "`date "+y=%Y m=%m"`"
ls -l 100_*.jpg |
while read -r perms links owner group size month day year file
do
case $month in
Jan) month=01 ;; Feb) month=02 ;; Mar) month=03 ;; Apr) month=04 ;;
May) month=05 ;; Jun) month=06 ;; Jul) month=07 ;; Aug) month=08 ;;
Sep) month=09 ;; Oct) month=10 ;; Nov) month=11 ;; Dec) month=12 ;;
esac
case $year in
*:*) [ ${month#0} -gt ${m#0} ] && year=$(( $y - 1 )) || year=$y ;;
esac
newname=${file#????}
newname=${newname%.jpg}_${year}-${month}-${day}.jpg
mv "$file" "$newname"
done

--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Chris F.A. Johnson

2004-12-29, 8:47 pm

On Sat, 25 Dec 2004 at 22:16 GMT, virus wrote:
> I'd like to have a script that renames all those strange filenames that
> my digital camera gives out.
> ie my digital camera supplies a file like 100_0104.jpg and I'd like to
> search for all such files and delete the first 4 characters and leave
> only 0104.jpg - maybe substitute the 1st 4 character to something more
> useful like the date ?


date=`date +%Y-%m-%d`
for file in 100_*.jpg ## adjust pattern to taste
do
newname=${date}_${file#????} ## use date in place of first 4 chars
mv "$file" "$newname"
done

Or, to use the dates on the files themselves:

eval "`date "+y=%Y m=%m"`"
ls -l 100_*.jpg |
while read -r perms links owner group size month day year file
do
case $month in
Jan) month=01 ;; Feb) month=02 ;; Mar) month=03 ;; Apr) month=04 ;;
May) month=05 ;; Jun) month=06 ;; Jul) month=07 ;; Aug) month=08 ;;
Sep) month=09 ;; Oct) month=10 ;; Nov) month=11 ;; Dec) month=12 ;;
esac
case $year in
*:*) [ ${month#0} -gt ${m#0} ] && year=$(( $y - 1 )) || year=$y ;;
esac
newname=${file#????}
newname=${newname%.jpg}_${year}-${month}-${day}.jpg
mv "$file" "$newname"
done

--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com