LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-13-2008, 03:30 PM   #1
jlsr
LQ Newbie
 
Registered: Oct 2008
Posts: 5

Rep: Reputation: 0
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.
 
Old 10-13-2008, 03:49 PM   #2
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
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
 
Old 10-13-2008, 04:08 PM   #3
jlsr
LQ Newbie
 
Registered: Oct 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks! I will try your example and see what I get. I probably should not use "*.*", good point.
 
Old 10-13-2008, 05:29 PM   #4
jlsr
LQ Newbie
 
Registered: Oct 2008
Posts: 5

Original Poster
Rep: Reputation: 0
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
-------
 
Old 10-14-2008, 01:19 PM   #5
jlsr
LQ Newbie
 
Registered: Oct 2008
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 10-14-2008, 01:34 PM   #6
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do i change to super user then revert back to ordinary user ,using shell script? wrapster Solaris / OpenSolaris 6 03-18-2009 03:37 AM
super user privileges check for a normal user in bash script freeindy Programming 2 08-01-2008 06:08 AM
bash shell script naka0naka Linux - Newbie 7 05-28-2004 03:06 PM
bash/shell script mikis Programming 2 10-14-2003 11:39 AM
bash shell script MaryM Linux - Newbie 0 02-15-2002 11:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration