The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 06-28-2006
grial's Avatar
grial grial is offline Forum Advisor  
El UNIX es como un toro
  
 

Join Date: Jun 2006
Location: Madrid (Spain)
Posts: 531
Helo.
If I've understood you, this would fit your needs:

Code:
#!/bin/bash

# Initial value used to compare.
val=0
echo "initial val=$val"
echo "-----------"

# For each file whose name starts with "DFC" and is under files/ folder do
for f in $(ls -1 files/DFC*); do
   echo "File: $f"
   # get the number from the filename
   num=$(echo $(basename $f) | cut -d. -f1|cut -c4-)
   echo "num: $num"
   # Compare it to val value
   if [ $num -gt $val ]; then
      # It's greater, so update val value and delete file.
      val=$num
      rm $f
      echo "${f} deleted."
   fi
   echo "val=$val"
   echo "--"
done

echo "-----------"
echo "###########"
echo "-----------"
echo "val=$val"

For bash, but most probably will work under ksh.

The script assumes your files are stored in ./files

Regards.