Find modify and delete files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find modify and delete files
# 1  
Old 09-16-2013
Find modify and delete files

hi every one. one of my friends has writen this script and send it to me. this script can find files that add-delete-modify and also send an alert by email
i'm not catch all part of it.
can anyone explain me how this work


Code:
#!/bin/bash
START="a.txt"
END="b.txt"
DIFF="c.txt"
mv ${START} ${END} #moving the first file to be compared
find /home -type f -mmin -5 -print > ${START}
diff ${START} ${END} | grep '<' > ${DIFF}
COUNT=$(cat ${DIFF} | wc -l)
if [[ -s ${DIFF} ]] ; then
cat -- ${DIFF} | mail -s "Modified files in last 5 minutes *** ${COUNT} ***" root
fi

# 2  
Old 09-16-2013
If it is working, it's just what you asked your friend for.
Why don't you ask your friend? Smilie

Next time, be more precise which part you do not understand instead, thanks.

Code:
#!/bin/bash

Use bash explicit as interpreter to run this script

Code:
START="a.txt"
END="b.txt"
DIFF="c.txt"

Define 3 variables with a file name.

Code:
mv ${START} ${END} #moving the first file to be compared

Renames the start file to the name of the end file. If it already exists, it is overwritten. Saves the data in the file from the previous run to b.txt so they are not lost and can be compared with actual data.

Code:
find /home -type f -mmin -5 -print > ${START}

Find files in directory /home that are not older than 5 minutes and write their path and name into the file a.txt.

Code:
diff ${START} ${END} | grep '<' > ${DIFF}

Find the lines that are different from the old file b.txt and write them to the file c.txt.

Code:
COUNT=$(cat ${DIFF} | wc -l)

Count the number of different lines.

Code:
if [[ -s ${DIFF} ]] ; then
     cat -- ${DIFF} | mail -s "Modified files in last 5 minutes *** ${COUNT} ***" root
fi

Check if the file c.txt exists and has a size greater 0.
If this is the case, tell cat to not accept any switches and send a mail with the subject "Modified files..." where the mail's body is the cat'ed different lines to root.

Last edited by zaxxon; 09-16-2013 at 02:31 PM.. Reason: messing around with code tags
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 09-16-2013
yep i know what this script do.
and also you explain all part very well TQ


is this part creat a.txt b.txt c.txt when shell execute?
Code:
START="a.txt"
END="b.txt"
DIFF="c.txt"

# 4  
Old 09-16-2013
? I do not understand that question. All of this code is executed by the bash shell. The part you mention is just a definition of strings as values to variable names. These 3 lines do actually not touch the files in any way. This comes in the following lines.
# 5  
Old 10-14-2013
hey. i have edit this script to check only .php and .pl file is it correct ?

Code:
#!/bin/bash
EMAIL=Mymail@yahoo.com
START="a.txt"
END="b.txt"
DIFF="c.txt"
mv ${START} ${END} #moving the first file to be compared
find /home -name "\*.php|\*.pl" -type f -mmin -5 -print > ${START}
diff ${START} ${END} | grep '<' > ${DIFF}
COUNT=$(cat ${DIFF} | wc -l)
if [[ -s ${DIFF} ]] ; then
cat -- ${DIFF} | mail -s "Modified files in last 5 minutes *** ${COUNT} ***" $EMAIL
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find command to delete the files

Hi All, I've created 2 files touch -t 201309101234 aa10 touch -t 201309111234 aa11 Exact 60 days before from today date is SEPT 12th . As per the following command as i gave +60 means the files which were created before sept12th should be deleted find /etc/logs/*aa* -type f -atime +60... (5 Replies)
Discussion started by: smile689
5 Replies

2. UNIX for Dummies Questions & Answers

Find command to delete old files

Hi, I want to delete all the log files that was created on year 2008. My command is not working. Any idea? find . -name '*.log' -mtime 1460 -exec ls -lt {} \; Thank you. (2 Replies)
Discussion started by: samnyc
2 Replies

3. Shell Programming and Scripting

Script to go Into Directories and Find/Delete files

I have a task, I usually do manually, but with growing responsibilities I tend to forget to do this weekly, I want to write a script that automates this, but I cant seem to work it out in my head, I have the shell of it out, but need help, and you guys have helped me with EVERY problem I have... (5 Replies)
Discussion started by: gkelly1117
5 Replies

4. UNIX for Dummies Questions & Answers

Script to find the files and delete them

This is a real world problem so I think you might found this interesting. We have servers which are shared by multiple team members. Each team member has its own user id and home directory. Now with time each user starts creating files which in end caused the disk to be full. Now for creating a... (5 Replies)
Discussion started by: Rohit06
5 Replies

5. Homework & Coursework Questions

Find and delete empty files and directories

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Need to make a script, to remove all empty files and folders from current category. It also should show the name... (2 Replies)
Discussion started by: Itixop
2 Replies

6. Shell Programming and Scripting

Script to find files and delete it by comparing

I have a directory where lot of "gzip" files are dropped in every 5 minutes. There is an application which will process these gzip and move it to another directory but will leave a gzip.out file with lot of output data. I need to remove all the outfiles except for the one which is being... (1 Reply)
Discussion started by: gubbu
1 Replies

7. Shell Programming and Scripting

find files for a particular date and delete

How can I delete files for a particular date ? I apologize in advance If there is solution please put the link. Thanks, (5 Replies)
Discussion started by: jville
5 Replies

8. UNIX for Dummies Questions & Answers

find and delete files

hi all , i want to find and interactively delete all the files having size greater than 20 bytes using "find" and other commands..... (8 Replies)
Discussion started by: sonu_pal
8 Replies

9. Shell Programming and Scripting

Find files including subdirectory and Delete

Hello Experts, I m newbie. Could u pls help me to write script on Sun solaris- I have backup directory "/var/opt/backup/" where files are backed up in different directory "backup1" "backup2" "backup3". I want to write a shell script which i will put in crontab and daily midnight it will... (1 Reply)
Discussion started by: thepurple
1 Replies

10. UNIX for Dummies Questions & Answers

batch delete using find, files with # character

UPDATE: Sorry, disregard this. It did work, I made a mistake; I just shouldn't have been using maxdepth. I do think it is good to know, however, that find | grep '#' | xargs rm will "clean up" funnily named files in a directory. Of course, some of those funnily named files are there... (0 Replies)
Discussion started by: tphyahoo
0 Replies
Login or Register to Ask a Question