LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-22-2005, 01:23 AM   #1
fbfd1338
LQ Newbie
 
Registered: Mar 2004
Location: Motor City, MI
Distribution: Redhat 7.3
Posts: 12

Rep: Reputation: 0
Question File count with script?


I am trying to write a small script that will rename my digital camera photo's the have weird names like DCD 20040925 to a simpler naming of just a 4 digit numbering system ie. 0001, 0002, 0003.jpg, etc. I was suggested to me that I do a file count of the folder that the pics are in, then have the names changed to progress up to the file count? Wel I am having trouble finding how to obtain a file count, and curious if there may be a better way to do this? Thanks a bunch.

-Kevin
 
Old 10-22-2005, 01:53 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
One possible way is to run the following command:
Code:
ls -1 | wc -l
Just to be thorough, that's a one (1) for the option to ls.

That will count how many things are in a directory. "Things" includes regular files and directories. If there are directories mixed with the images, I would suggest reading up on the find command (man find) with specific attention to the "-type" and "-maxdepth" options.
 
Old 10-22-2005, 09:55 AM   #3
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Originally posted by Dark_Helmet
One possible way is to run the following command:
Code:
ls -1 | wc -l
Just to be thorough, that's a one (1) for the option to ls.

That will count how many things are in a directory. "Things" includes regular files and directories. If there are directories mixed with the images, I would suggest reading up on the find command (man find) with specific attention to the "-type" and "-maxdepth" options.
ls will automatically infer -1(one) if its output isn't routed to a terminal, so ls | wc -l is equivalent (and shorter). Try:
Code:
ls 
ls | cat
to see the difference.
 
Old 10-22-2005, 10:24 AM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
Here is something which may get you started. You should try it on a practice folder first.
Code:
#!/bin/bash
#Example: ./test /home/images
usage()
{
   echo "Usage: $0 Directory_Name"
   echo "Example: ./test /home/images"
   exit 1;
}

test -d "$1" || usage

dir="$1"

mkdir $dir/tmp 2> /dev/null

ls $dir | \
while read i ; do
   n=$(( $n + 1 ))
   j=`echo "$i" | awk -F. '{print $2}'`
   mv -v "$dir/$i" "/tmp/file$n.$j"
if [ $n -lt '10' ]; then
   mv "/tmp/file$n.$j" "/tmp/file00$n.$j"
elif [ $n -ge '10' -a $n -lt '100' ]; then
   mv "/tmp/file$n.$j" "/tmp/file0$n.$j"
fi
done
find /tmp -type f -name 'file*' -exec mv -v {} $dir \;

Last edited by homey; 10-22-2005 at 10:53 AM.
 
Old 10-23-2005, 04:02 AM   #5
fbfd1338
LQ Newbie
 
Registered: Mar 2004
Location: Motor City, MI
Distribution: Redhat 7.3
Posts: 12

Original Poster
Rep: Reputation: 0
Homey.....
Thank you for the help, with a few minor alterations to the script I was able to use alot of what you gave me and it works perfect.
 
  


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
command to count how many lines in a file? dr_zayus69 Linux - Software 3 01-06-2005 11:42 AM
How to count number of argument received by a script? philipina Linux - General 2 07-05-2004 03:35 AM
Count number of line in a file. philipina Programming 7 03-18-2004 06:04 PM
Should posts in general count on your post count? Joey.Dale General 16 01-27-2004 02:31 AM
Found file count tmoorman Linux - Software 3 10-19-2003 10:48 PM


All times are GMT -5. The time now is 08:14 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration