Operating System - Linux
1748040 Members
5122 Online
108757 Solutions
New Discussion юеВ

Re: Shell script to run everyday with Exclusion Mechanism

 
Henry  Richards
Occasional Contributor

Shell script to run everyday with Exclusion Mechanism

Hi Guys
I have this script that runs everyday to Long list (ll in HPUX and ls -la in Solaris)all the files in all the directories.The Script when runs the nextday , captures the difference between previous day's longlist(ll) and currentday longlist (ll) using the diff command in HPUX "diff file1 file2 > file12"] and save it to new file titled "result".

Now I want to Add a mechanism for excluding files/directories/file systems from the report if they are known to change as part of regular operation of the system.Rightnow I do not what all the directories and files that are to be excluded so now I am looking to design the script with this capability of excluding some directories/files.

The script is as follows:

#! /usr/bin/sh
dt1=`date`
TODAY=`echo $dt1|cut -d" " -f3`
set -A DAYS Sat Sun Mon Tue Wed Thu Fri Sat
set -A MONTHS Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
YESTERDAY=$((`date +%d` -1))
MONTH=`date +%m`
YEAR=`date +%Y`
NDAY=`date +%u`

tfile=$MONTH$TODAY$YEAR.lst
echo $tfile
WEEKDAY=${DAYS[`date +%u`]}
if [ $YESTERDAY -eq "0" ]
then
MONTH=$((MONTH-1))
if [ $MONTH -eq "0" ] then
MONTH=12
YEAR=$((YEAR-1)) fi
set `cal $MONTH $YEAR`
shift $(($# - 1))
YESTERDAY=$1
fi
TMONTH=${MONTHS[MONTH]}
yfile=$MONTH$YESTERDAY$YEAR.lst
echo "Y day File Name " $yfile echo "T Day file Name " $tfile
ls -ltR /home/tt/tt2 >$tfile

diff $tfile $yfile >result.chg

I would appreciate if you guys could help me out on this.

Thanks
Henry
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: Shell script to run everyday with Exclusion Mechanism

Hi Henry:

I'd consider letting 'find' drive your principal 'ls' so that you can leverage directory selection and perhaps some basic filename selection or exclusion. For instance:

# find /etc/rc.config.d /sbin -type f ! -name "net*" -a ! -name "vg*" | xargs ls -l > /tmp/results

You could also use simple, stacked 'sed' commands to exclude other candidates before you 'diff' the two days files. For example:

# sed -e '/clean/d' -e '/set_parms/d' ...

If you like, the later specifications can be self-contained in a script that you pipe the output of the driving 'find' into, thereby creating the "results" to 'diff'.

Regards!

...JRF...
Sandman!
Honored Contributor

Re: Shell script to run everyday with Exclusion Mechanism

As mentioned, use the find command and play with its "-prune" option in order to exclude directories that are changed on a consistent basis.

cheers!
Henry  Richards
Occasional Contributor

Re: Shell script to run everyday with Exclusion Mechanism

Hi James(JRF) and Sandman

so can I use the
# find / -a ! -name "vg*" | xargs ls -l > /tmp/results

instead of ls -ltR

I have a doubt will
find / -a! name " /dev/ "
will exclude that whole directory and all its sub-directories & files.

Thanks
Henry
Rodney Hills
Honored Contributor

Re: Shell script to run everyday with Exclusion Mechanism

You can use parenthesis in a find-

Example-
find / ! \( -name "vg*" -a -path "/dev/*" \) | xargs ls -l > /tmp/results

Note the use of "path" for /dev/*.

HTH

-- Rod Hills
There be dragons...
Henry  Richards
Occasional Contributor

Re: Shell script to run everyday with Exclusion Mechanism

Hi Guys

How do I exclude a particular directory
or a particular file using the find command


I tried the following command to delete a file ytre.txt located at /dev/first/ytre.txt

find /dev/first !\(-name "ytre" -a path /dev/first \)|xargs ls -l

But it didn't work

Please let me know the syntax of excluding particular directory/file using the find command



your help is appreciated.

Thanks
Henry

Rodney Hills
Honored Contributor

Re: Shell script to run everyday with Exclusion Mechanism

Be sure to use quotes and wildcards-

example-

find / ! \( -name "*yte*" -path "/dev/vg01/*" \) -print

This will ignore any files that have "yte" as part of their name or has a path that starts with "/dev/vg01".

Do not use -a in this case since "!" is a negative it reverses the sense of what's in the parenthesis.

HTH

-- Rod Hills
There be dragons...
Mark Ellzey
Valued Contributor

Re: Shell script to run everyday with Exclusion Mechanism

Henry,

Another approach might be to create a list of directories that you DO want to list. Create this list in another file. Then your script can read this file and loop through that list doing your ls -l command.

In this way, once your script logic is set, you don't have to change it. You just add or remove directories from your 'directory file'.

Cheers,
Mark

Henry  Richards
Occasional Contributor

Re: Shell script to run everyday with Exclusion Mechanism

Hi All

Thanks James,Sandman,Rod & Mark

Mark,

I have question for you,
can you suggest a mechanism or an approach of how to fit in that file that contains the list of directories that HAVE to be included into my above posted script.

Thanks
Henry
James R. Ferguson
Acclaimed Contributor

Re: Shell script to run everyday with Exclusion Mechanism

Hi Henry:

You can do something like this:

...
while read PATHNAME
do
find ${PATHNMME} ...
done < your_file_of_directories

Rodneys' suggestion to use the '-path' option to exclude directories is a good one, but it may not be entirely portable. AIX doesn't allow it and I don't know about Solaris.

You can use '-prune' as an alternative. It operates OK if you first change directories before your 'find' and thus do something like:

# cd somedirectory
# find . -name NOTTHIS -prune -o -print

The above would print all files in "somedirectory" except those in the subdirectory "NOTTHIS".

I'm not fully sure what you ultimate goal is here. If you are trying to track changes in files, then you need to potentially look at more than just file timestamps, sizes and permissions. Checksum changes would be the next level to track.

If you are really going down that route, a perl program would be a good answer. Perl has a builtin find() function. You could couple its use with the stat() function to get the same information 'ls' returns. Tracking changes could be done with hashes containing the data you want to monitor.

Regards!

...JRF...