LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices


Reply
  Search this Thread
Old 02-15-2014, 12:54 PM   #1
rooster17
LQ Newbie
 
Registered: Feb 2014
Posts: 5

Rep: Reputation: Disabled
Cool find command and omit directories also create a text file and email out


Hi there,

I'm from an IT background and have recently taken on the responsibility of maintaining and administering Red Hat Critical servers. I have no experience in a Linux/Unix environment and all I know is self taught - treat me as a novice please.

I need to search to do the following:

1. Scan my FTP server for content 6 months or older;
2. Exclude 2 Directories from the find command;
3. Create a text file with it's findings and save it with the date and time as the name
4. Attach that file created and mail it out.

I've so far managed to do Steps 1, 3 & 4. I cannot seem to exclude Directories.

Also, despite the text files being created and attached, they are blank in the email but not in the directory they are created. Also, it only attaches the file from the location the file is created.

This command is to be put into a script so in theory it has to run from anywhere and not from within the folder - kind of defeat's my ambition and task here.

Here is what I managed to come up with:

find /ftp_mount/ftp_root/ -ctime +180 > /usr/bin/FTPpurgefiles/logs/ShowContent/ShowContent_`date "+%d-%m-%Y_%H-%M-%S"`.txt | mutt -s "Log" -a /usr/bin/FTPpurgefiles/logs/ShowContent/`ls -tr | tail -n 1` mail@me.com

If any genius out there can help with this, I'll send you a ton of Virtual Beer

Thanks in advance
 
Old 02-15-2014, 01:28 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
If you want to exclude directories you need the -prune option. Here is an example:
Code:
find /ftp_mount/ftp_root \( -wholename /ftp_mount/ftp_root/dir1 -o -wholename /ftp_mount/ftp_root/dir2 \) -prune -o -ctime +180 -print
where dir1 and dir2 are the directories to exclude.

Regarding the empty e-mail, it is due to the fact that the ls command is executed in the current working directory. Try to change it to:
Code:
mutt -s Log -a $(ls -tr /usr/bin/FTPpurgefiles/logs/ShowContent | tail -1) mail@me.com
Furthermore you don't need to put both the find and the mutt command in one line, since you redirect the output of find to a file and therefore nothing goes through the pipe. Hope this helps.
 
1 members found this post helpful.
Old 02-16-2014, 04:47 AM   #3
rooster17
LQ Newbie
 
Registered: Feb 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks colucix

3rd time editing this response - here goes.

Your prune command works a dream - many thanks!

The command I've now done is:

find /ftp_mount/ftp_root \( -wholename /ftp_mount/ftp_root/dir1 -o -wholename /ftp_mount/ftp_root/dir2 \) -prune -o -ctime +180 -print > /usr/bin/FTPpurgefiles/logs/ShowContent/ShowContent_`date "+%d-%m-%Y_%H-%M-%S"`.txt

I added your mutt command to the end of this line and it attached the file with the data in the text file, but only the first time. If you try again it attaches but is blank for some reason (not sure why this is). Also, it only attaches from within the folder it creates the file in like my original post:

find /ftp_mount/ftp_root \( -wholename /ftp_mount/ftp_root/dir1 -o -wholename /ftp_mount/ftp_root/dir2 \) -prune -o -ctime +180 -print > /usr/bin/FTPpurgefiles/logs/ShowContent/ShowContent_`date "+%d-%m-%Y_%H-%M-%S"`.txt | mutt -s Log -a $(ls -tr /usr/bin/FTPpurgefiles/logs/ShowContent | tail -1) mail@me.com

If I run this command outside the directory I get error messages:

Can't stat ShowContent_16-02-2014_12-25-35.txt: No such file or Directory
ShowContent_16-02-2014_12-25-35.txt: unable to attach file


If i run the above command without the mutt, it creates the file anyway. If i then run your Mutt command on it's own, outside the directory the file was saved to, it doesn't do what I want. instead it opens up Mail option where I confirm the Email address, Subject,
and an empty vi session.

Any ideas?

Hope I've made sense here :S

Last edited by rooster17; 02-16-2014 at 06:46 AM. Reason: Worked once and only in the folder itself.
 
Old 02-22-2014, 07:45 AM   #4
rooster17
LQ Newbie
 
Registered: Feb 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
Any one?!
 
  


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
[SOLVED] command to find/replace in a text file? sneakyimp Linux - Newbie 17 02-15-2011 12:30 AM
[SOLVED] Find a file in directories without using find command sikanders Linux - Newbie 14 08-06-2010 08:47 PM
mount command cant find smb directories but file-browsers can map to them. robertbas Linux - Networking 2 09-21-2006 03:12 AM
Linux shell command for makefile.in to create a text file and write to it alix123 Programming 8 01-07-2005 08:18 AM
Command to find file with text chamanrana Linux - Software 10 12-02-2004 10:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat

All times are GMT -5. The time now is 02:07 AM.

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