LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help with script to get user ftp logs (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-with-script-to-get-user-ftp-logs-728712/)

pdstu25 05-26-2009 06:32 PM

Need help with script to get user ftp logs
 
Hello,

I'm trying to write a shell script that will grab a users ftp logs and put it in his home dir. The server I will run this script on keeps two plan text logs and the other 6 are compressed with .gz. For example there is auth.log, auth.log.1 auth.log.2.gz ..... auth.log.8.gz I would like to scan all those plus xferlog (which is setup the same way) for a user and have it place a auth.log and xferlog file in the customers root. I have the below script which works but only does it for one file. All help is appreciated.

#!/bin/sh

DIR=/hsphere/local/var/proftpd/
USER=$1

if [ $# -eq 0 ]
then
echo "$0 : You must supply username"
exit 1
fi

if [ -f $DIR/auth.log ]
then
cat $DIR/auth.log | grep $USER >> /hsphere/local/home/$USER/auth.log
else
echo "File doesn't exist"
fi


P.S This is my first time posting to any Forum so please be gentle

chrism01 05-26-2009 06:46 PM

Code:

for file in `ls $DIR/auth.log*`
do
    if [[ $file =~ "gz" ]]
    then
        zcat $file | grep $USER >> /hsphere/local/home/$USER/auth.log
    else
        grep $USER $file >> /hsphere/local/home/$USER/auth.log
    fi
done

NB: Untested as I don't have Linux in front of me. Also, you need bash >= v3 for regex "=~" operator.
http://www.tldp.org/LDP/abs/html/bas...#REGEXMATCHREF

ghostdog74 05-26-2009 06:57 PM

Quote:

Originally Posted by chrism01 (Post 3553722)
[code]
for file in `ls $DIR/auth.log*`

no need ls.
Code:

for file in $DIR/auth.log*

pdstu25 05-26-2009 07:25 PM

Thanks works exactly as needed.


All times are GMT -5. The time now is 09:44 AM.