LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   File count with script? (https://www.linuxquestions.org/questions/linux-newbie-8/file-count-with-script-375645/)

fbfd1338 10-22-2005 12:23 AM

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

Dark_Helmet 10-22-2005 12:53 AM

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.

eddiebaby1023 10-22-2005 08:55 AM

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.

homey 10-22-2005 09:24 AM

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 \;


fbfd1338 10-23-2005 03:02 AM

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.


All times are GMT -5. The time now is 06:19 PM.