LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Brand new user needs help with bash shell script (https://www.linuxquestions.org/questions/programming-9/brand-new-user-needs-help-with-bash-shell-script-676105/)

jlsr 10-13-2008 03:30 PM

Brand new user needs help with bash shell script
 
Hi,
I'm fairly new to Linux shell scripting and brand new to this forum. I've searched and can't find how to do exactly what I need to do in a script. Here's the scenario. I created a shell script to find all changed files within the last 7 days, and email me the report. That works. However, I now need to exclude certain files or certain directories from my listing (in particular, those ending in .txt and .log). The machine is running SuSE Linux Enterprise Server (SLES9).

Here's my script (please don't laugh... I'm a beginner, ok?)

#-----------
find . -name "*.*" -mtime -7 -exec ls -ltrh >> /home/myuserid/changed.log {} \; |grep `date +%Y-%m`

/bin/mail -s "Changed Files Log" myemail@mycompany.com < /home/myuserid/changed.log

mv /home/myuserid/changed.log /home/myuserid/changed.log.$DATE
#----------

what do I need to change on the syntax for find to exclude certain files or extensions in my resulting file (changed.log)?

Thank you very much for any help you could provide.

jan61 10-13-2008 03:49 PM

Moin,

first: Do you really want to limit your search on file names including a "."? The point has no special function on unix systems, it's a part of the name like any other character. If you want to include all files, you can omit the -name "*.*" option.

If you want to exclude some filenames you can use the -regex option:
Code:

find . -mtime -7 ! -regex '.*\.txt' ! -regex '.*\.log' -print | xargs ls -ltrh >/home/myuserid/changed.log
I used xargs instead of -exec because this will not create an own ls process for each found file.

Jan

jlsr 10-13-2008 04:08 PM

Thanks! I will try your example and see what I get. I probably should not use "*.*", good point.

jlsr 10-13-2008 05:29 PM

Thanks that worked for excluding the txt and log files (kind of). However it did include files in the top level directory that were modified much later than the criteria (last 7 days), and included directories (date stamp much later than last 7 days).

excerpt from log file:

-rw-r--r-- 1 appltst2 dba 24K 2008-04-29 15:41 some.log
-rw-r--r-- 1 appltst2 dba 376 2008-05-20 14:57 TST2_verisign.env
drwxr-xr-x 9 appltst2 dba 4.6K 2008-07-17 19:57 admin
-rwxrwx--- 1 appltst2 dba 304 2008-08-05 17:17 changed_files.sh
-rwx------ 1 appltst2 dba 111 2008-10-13 16:09 new_changed_log.sh


Now, if I run it from the command line using the following it works:
-------
find . -mtime -7 ! -regex '.*\.txt' ! -regex '.*\.log' -print >/home/mydir/newlog.log
-------

jlsr 10-14-2008 01:19 PM

I forgot to mention that running find . -mtime -7 ! -regex '.*\.txt' ! -regex '.*\.log' -print >/home/mydir/newlog.log doesn't give me the date and time so this won't work either.

I've tried several different things today but either I get way too much stuff (that isn't matching my criteria) or it errors out.

Thanks for all your help and consideration.

jan61 10-14-2008 01:34 PM

Moin,

you should take a look at the find manual. Add -type f to limit the search to regular files, use -printf "..." to list the information you need. This way the following "| xargs ls ..." is not neccessary. Of course, if you ls a directory, the contents of this directory will be listed. You can stop that using the -d option - it's described in man ls.

Jan


All times are GMT -5. The time now is 08:57 AM.