for file in ???? - command

using <B>Bourne shell</B> how can I list all files in such a directory that file name don't match a pattern.

e.g:
a dir. /mydir contains these files
[tamer001]-/mydir$ ls
2001-01-23.log
2001-01-23_08-30.log
2001-01-23_15-59.log
2001-01-24_00-00.log
2001-01-24_11-52.log
2001-01-24_18-34.log
2001-01-25.log
2001-01-25_13-30.log

I want to move all files except the files of 25th of the month (which is today files) and rename the files by adding a string to the begining of the file name.

so here what I did:

#!/usr/bin/sh
dir=/var/dist
cd /mydir

for file in *.log
do
 if [ -f $file ]
  then
    if /usr/bin/mv $file $dir/access_log_$file
    then
        echo "$file : moved to $dir successfuly "
    else
        echo "can not move or rename access_log_$file"
    fi
  fi
done

how can I exclude moving today log files from the dir /mydir ?

please help.

[Edited by tamer on 01-28-2001 at 05:42 PM]
added code tags for readability --oombera

DATE=`/bin/date "+%Y-%m-%d"`
for file in !(${DATE}*.log); do
do
...

done

Thanks for reply.
but is this in Bourne Shell !!

because this error occured after running the script

test.sh: syntax error at line 7: `!' unexpected

#!/usr/bin/sh

dir=/var/dist
cd /mydir
DATE=`/usr/bin/date "+%Y-%m-%d"`

for file in !(${DATE}*.log); 
do
  if [ -f $file ]
  then
    if /usr/bin/mv $file $dir/access_log_$file 
     then
       echo "$file : moved to $dir successfuly " 
     else
       echo "can not move or rename access_log_$file"
    fi
  fi
done

added code tags for readability --oombera

Sorry, I dont know enough about the Bourne shell to help you. My answer will work in bash or ksh.

OK thanks PxT.

any one can help me.

Tamer: Seems like a good exercise to take PxT's script and convert it to the shell script you want. I thought PxT did a great job answering this for you. After all, if you have the script in one shell, it should be minimal work to figure out how to convert it to another. It is good practice for you to do a little 'shell conversion' don't you think? Please post your version when you are finished so others can learn from your work too!

Thanks; but I was spent 2 days to write 200 line of code to logroll my mail server log files, zip them and transfer them to another server for anther purposes; and this was the only problem I faced, anyway I will do it and post it again.

I solve it as follows:

#!/usr/bin/sh

dir=/var/dist
cd /mydir
<b>
today=`/usr/bin/date +%Y-%m-%d`
export today
todayLogs=`ls $today*`
export todayLogs
</b>
for file in *.log 
do 
if [ -f $file ] 
then 
<b>fileExists=`echo $todayLogs | grep $file`
   if [ "$fileExists" = "" ]</b>
      if /usr/bin/mv $file $dir/access_log_$file 
    then 
     echo "$file : moved to $dir successfuly " 
    else 
     echo "can not move or rename access_log_$file" 
    fi 
 fi
fi 
done 

its work.
and I'll convert PxT script to bourne soon........

[Edited by tamer on 01-30-2001 at 09:37 AM]
added code tags for readability --oombera

Here is Perl Script that could solve your problem:

#/bin/perl

use File::Copy;

$directory = ".";
$moveto = "/var/dist";
$prefix = "access-log";
$ignoredFiles = "";
$count=0;

my ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = localtime;
$month=$month+1;
if($mon < 10) { $month="0".$month; }
$year =~ s/1/20/;

$today = join("-", ($year, $month, $mday));

opendir (DIR, "$directory") || die "Can't open $directory,Reson$!";
@dir = grep(/.log$/,readdir(DIR));
closedir (DIR);

# Now, for every file in the directory...
foreach $file (@dir) {
# If it contains today's date ignore it
# If it is older than 'n' day (-M "$directory/$file" > n)

if ("$directory/$file" =~ $today) {
$ignoredFiles .= "$file\n"; }
else {
copy("$directory/$file", "$directory/$moveto/$prefix-$file") || die("Can't Copy. Reason:$!\n");
$count = $count+1;
}
}

print "\n$count files moved";
print "\nFiles ignored(Today's file): $ignoredFiles";

I know perl prides itself on the fact that "There is more than one way to do it", but why do you do a string substitution here, when simple arithmetic would work just as well?

$year +=1900;

Using a string sub. is really just obfuscation. And as an added benefit, the arithmetic method will ensure that your scripts continue to work in the 22nd century and beyond. :wink:

Thank you for Pointing out the blunder in my script Pxt. I were unaware of that method :smiley:

As you said I want to run that script 22nd century people and email me "oh man what a script" :wink: