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
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
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.
#!/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
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)
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.