LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-30-2006, 01:48 AM   #16
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Rep: Reputation: 45

I admit saving the index number in a file is not 100% failsafe. It might not be as important to you to have uniquely named files regardless of the directory they are stored in, but I would like my own script to do that. Maybe it's something I will play with separate from this post. If any one has any better ideas on how to store a global indexing number please give me some ideas. As far as the original problem goes, let me see if I can put everything I have learned together:

Code:
#!/bin/sh

# Courtesy of Chocolate for the original script code.
# And to Archtoad for the "picnum" and $FILE loop code.
# Adapted (probably incorectly) by drkstr.

picdir=`date +%m-%d-%y`
usage="Options: [-c camere name] [-d delete source pictures]"
sourcedir=media/usbdisk/DCIM/100_FUJI
filenum=`ls $/"$sourcedir"/* | cut -c 4- |sort -n -r |head -1`

while getopts ":c:d" options; do
  case $options in
    c ) newcam=$OPTARG;;
    d ) delsrc=$OPTIND;;
    h ) echo $usage;;
    \? ) echo $usage

         exit 1;;
    * ) echo $usage
          exit 1;;
  esac
done

if [ $newcam ]; then
camdir=$newcam
else
camdir='fuji-a303'
fi

mkdir -p "$camdir"
mkdir -p ~/"$camdir/$picdir"

filenum= $filenum + 1         # does filnum++ work in bash?
for FILE in `ls $sourcedir`
do
  newfilename='DSC$filenum'   # is this valid?
  cp -r /"$sourcedir/$FILE" ~"/$camdir/$picdir/$newfilename"
  filenum=$filenum + 1
done


if [ $delsrc ]; then
rm /"$sourcedir"/*
fi
As I have said before, I am new to all of this (escpecually bash scripting), and would greatly appreciate anyone looking over the code and giving me some pointers. I will still look into storing an indexing number after this is working, and in case anyone else is interested, will post what I find out.

Thanks!
...drkstr
 
Old 03-31-2006, 04:12 PM   #17
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by chocolatetoothpaste
... This is bad because it will overwrite the picture I took this morning. ...
If the objective here is to avoid overwriting pictures as safely & easily as possible, then I still think making the camera keep track of the numbers is the cleanest way to go.

I have repeated my automatic numbering experiment on my cell phone, a Sony Ericsson W600i, with the same result. I now believe that the normal behavior of any digital cameras when saving pictures is to add 1 to the highest serial # in its memory. If memory is empty, that means start (over) at 1. If anyone has a camera or phone, that does not do this, I'd like to hear about it.

I, like drkstr, believe that unique picture names are good for many reasons; not just the avoidance of overwriting when transferring. If that is true for you also, then why not let the camera (or phone) do the work of keeping track of the #'s?

All that is required is to leave one picture on the camera; and that is safe, easy, & scriptable. the algorithm is:
  1. Make all pics on cam r/w.
  2. (Optional) On cam, delete 1st (old) pic.
  3. Copy all pics from cam to box.
  4. On cam, make last pic r/o.
  5. On cam, delete all pics.
  6. On cam, make last pic r/w.
  7. (Optional) On box, find & delete any duplicate pics.

Here is my revised code:
Code:
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# 
#                            WARNING 1
#
#  Mount points are DYNAMIC, this code does NOT take that into account.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# 
#                            WARNING 2
#
#  I believe, from experience, that sorting the 'ls' output is unnecessary
#   -- 'ls' lists in directory order, which is both chronlogical & serial.
#  At least in this case.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #


###   Define vars
# CAMERA=<path_to_pics_on_cam>
# DESTDIR=<path_to_pics_on_box>

CAMERA=/mnt/flash/image/camera_semc/100msdcf             # on my sys., today -- cell phone
DESTDIR=~/Pictures/SE_W600i-image-camera_semc-100msdcf
PIC-0="$CAMERA/`ls $CAMERA  | head -1`"
PIC-n="$CAMERA/`ls $CAMERA  | tail -1`"

# Make all pics on cam r/w.
chmod 755 $CAMERA/*

# On cam, delete 1st (old) pic.
rm $PIC-0

# Copy all pics from cam to box.
cp  $CAMERA/* $DESTDIR

# On cam, make last pic r/o.
chmod -w $CAMERA/`ls $CAMERA |tail -1`

# On cam, delete all pics.
rm $CAMERA/*

# On cam, make last pic r/w.
chmod u+w $CAMERA/*                        
# for safety -- this duplicates step 1.

# (Optional) On box, find & delete any duplicate pics.  
# Not implemented -- no dupes were copied
Now, just because I think that script controlled renumbering is unnecessary to solve the original problem, doesn't meant that it isn't interesting or useful for something else.
 
Old 03-31-2006, 09:01 PM   #18
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Rep: Reputation: 45
Originally, I disagreed with archtoad's method of leaving the last pic behind simply because of annoyance. However I think I am now starting to lean more in that direction. This would eliminate the need to store an indexing counter, and seems easy enough to leave the last pic behind with the script posted by archtoad. All you would have to do is combine that in the "if [ $delsrc ]; then" section of the original script posted by chocolate. This is something I am going to play around with a little bit more.

regards,
...drkstr
 
Old 04-01-2006, 11:22 PM   #19
chocolatetoothpaste
LQ Newbie
 
Registered: Mar 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by drkstr
Originally, I disagreed with archtoad's method of leaving the last pic behind simply because of annoyance. However I think I am now starting to lean more in that direction. This would eliminate the need to store an indexing counter, and seems easy enough to leave the last pic behind with the script posted by archtoad. All you would have to do is combine that in the "if [ $delsrc ]; then" section of the original script posted by chocolate. This is something I am going to play around with a little bit more.

regards,
...drkstr
Here is the pinch, what happens when you put a different memory card in? Then you lose that reference point you had with the picture. Refering back to my original intentions, if you unoad BOTH of those cards in the same day, then they are conveniently put in the same folder. If you only have one card, or only have a phone or whatever, archtoad's method may work for you. Be VERY STRONGLY advised this is POOR programming practice.
 
Old 04-03-2006, 11:18 AM   #20
chocolatetoothpaste
LQ Newbie
 
Registered: Mar 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Mission accomplished! The script has reached the point I want it to. It will rename pictures from DSCF0001.x to pic0001.jpg or mov0001.avi, since my camera does movies too. Plus, if there is already a folder with todays date, it will see what pictures are in that folder, and re-increment the picture numbers! With the option of arguments and such, I think this is one fine little script! I hope someone can benefit from this. I know I have. I have become more familiar with shell scripting now. If anyone has some features they have added to this script, please post them so we can all benefit! Thanks to all who have helped!
Code:
#!/bin/bash
#script by ross paskett (aka chocolatetoothpaste) - script to transfer files from my camera, with args!

picdir=`date +%m.%d.%y`
usage="Options: [-c camera name] [-d delete source pictures]"
camsrc=/media/usbdisk/DCIM/100_FUJI

while getopts ":c:d" options; do
  case $options in
    c ) newcam=$OPTARG;;
    d ) delsrc=$OPTIND;;
    h ) echo $usage;;
    \? ) echo $usage

         exit 1;;
    * ) echo $usage
          exit 1;;
  esac
done

if [ $newcam ]; then
	camdir=$newcam
else
	camdir='fuji-a303'
fi

dst=~/$camdir/$picdir

mkdir -p ~/"$camdir/$picdir"
	if [ ! -e "$camsrc" ];then
		echo "Camera is not attached or incorrect path:"
		echo "$camsrc"
		exit 1
	fi

  for exstnc in $camsrc/*;do
    if [ ! -e $exstnc ];then
     echo "No pictures on camera!"
     exit 1
    fi
  done

clear

echo "Transfering pictures:"
  cd $camsrc
    for fl in *;
    do

    campic=`echo $fl | cut -dF -f2 | cut -d\. -f1`

    extn=`echo $fl | cut -d\. -f2 | tr [A-Z] [a-z]`

    if [ "X$extn" = "Xjpg" ];then
      pref="pic"
    else
      pref="mov"
    fi

    firstNum="$pref$campic"."$extn"

    cd $dst
    if [ -f "$firstNum" ];then
      cd $dst

      if [ "X$extn" = "Xjpg" ];then
        lastNum=`for x in *."$extn"
        do
        echo $x | cut -dc -f2 | cut -d\. -f1
        done | sort | tail -n1`
      else
        lastNum=`for x in *."$extn"
        do
        echo $x | cut -dv -f2 | cut -d\. -f1
        done | sort | tail -n1`
       fi


      if [ $lastNum -lt 9 ];then
        lastNum="$pref"000`expr $lastNum + 1`."$extn"
      elif [ $lastNum -lt 99 ];then
        lastNum="$pref"00`expr $lastNum + 1`."$extn"
      elif [ $lastNum -lt 999 ];then
        lastNum="$pref"0`expr $lastNum + 1`."$extn"
      else
        lastNum="$pref"`expr $lastNum + 1`."$extn"
      fi
      echo "$dst/$lastNum"
      cp "$camsrc/$fl" "$dst/$lastNum"

    else
      cd $camsrc
      echo "$dst/$firstNum"
      cp $fl "$dst/$firstNum"

    fi
  done

if [ $delsrc ]; then
  echo " "
  for DEL in $camsrc/*; do
  echo "Deleting: $DEL"
	rm -f "$DEL"
  done
fi

echo "Transfer complete!"
echo " "

Last edited by chocolatetoothpaste; 08-29-2007 at 12:11 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mass file manipulation Drack Linux - General 5 02-27-2006 07:40 AM
file manipulation with c C.Aymen Programming 2 09-01-2005 01:48 PM
C File Manipulation in Linux drigz Programming 5 10-01-2004 08:28 AM
File name manipulation with tr, concatination troubles goofyheadedpunk Programming 9 07-06-2004 03:39 AM
Some basic File Manipulation commands sureshp1980 Linux - General 2 09-24-2003 09:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration