Shell script to find if any new entry in directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to find if any new entry in directory
# 8  
Old 06-15-2015
Yes -maxdepth is not supported in all find implementations, we could try the -prune option as I believe this is more widely supported.

If this is run as a cron job you will be notified via email in a similar manor to how you were notified from your ls script above, by default all output from cron jobs is mailed to the jobs owner.

Code:
#!/bin/sh
touch 2.txt
[ -f 1.txt ] && find . ! -name . -prune -name "*dump*" -newer 1.txt -exec ls -l {} +
mv -f 2.txt 1.txt

# 9  
Old 06-16-2015
Hi,
Code:
#!/bin/sh
HOM_DIR=/full/path/to/source/dir
        i=$(head -1 $HOM_DIR/1.txt)
        j=$(ls -ltr $HOM_DIR/dump* | wc -l)
if [ $j -gt $i ]
then
        NEW=$(expr $j - $i)
        K=$(ls -ltr $HOM_DIR/dump* | nawk '{print $9}' | tail -$NEW)
        echo $K | xargs -n 1 | mailx -s "Subject" "mailaddr" -r "fromaddr"
else
        echo "0 files updated" | mailx -s "Subject" "mailaddr" -r "fromaddr"
fi
        ls -ltr $HOM_DIR/dump* | wc -l >$HOM_DIR/1.txt
exit;

Thanks

venkat
# 10  
Old 06-16-2015
Quote:
Originally Posted by bhas85
I am not sure whether my way is correct or not.First i manually fill 1.txt how many files are there then it will automatically updates.by comparing i can check the new file is created or not.But the code is not working as expected..

Code:
#!/bin/sh
i=`head -1 1.txt`
echo $i
j=`ls -ltr *dump*|wc -l`
echo $j
if [ $i -gt $j ]
then
k=`ls -ltr *dump* | tail -1`
echo $k
exit 1
fi
ls -ltr *dump* | wc -l >1.txt

Two mistakes.
1. You want the new number of dumps greater than the old number so it must be if [ $j -gt $i ] or if [ $i -le $j ].
2. If you exit then 1.txt is not updated.

---------- Post updated at 03:12 AM ---------- Previous update was at 03:08 AM ----------

Chubler's solution with find can avoid (most) duplicates by
Code:
find . ! -name . -prune -name "*dump*" -newer 1.txt \! -newer 2.txt -exec ls -l {} +


Last edited by rbatte1; 06-16-2015 at 09:22 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Automate DIRECTORY ENTRY when in remote SHELL

Good day all I am trying to figure out how to automate the command in a shell script that i have written. Some context: The script does the following on the local host #!/bin/sh banner 'QikCopy' echo "Please insert file name for SCP command!" read file DUMMY scp -p $file... (2 Replies)
Discussion started by: MrRobot
2 Replies

2. Shell Programming and Scripting

Shell script to find the GB files in /tmp directory in remote server

Hi, i need help on shell scripting. Main intention of the script is step 1: ssh to remote server Step 2: cd /tmp in remote server Step 3: in tmp i want to grep only files and directories which are in GB sizes All the servers list file is - tmpsrv.txt vi tmpsrv.txt ... (17 Replies)
Discussion started by: kumar85shiv
17 Replies

3. Shell Programming and Scripting

Shell script to find weblogic home directory

Hi, I am trying to find the weblogic home directory whether it is installed in the Linux box. if it is existing display the weblogic home and the corresponding Java home directory else display message as that wl home does not exist. I already wrote a program but it was not working properly.... (11 Replies)
Discussion started by: spgreddy
11 Replies

4. Shell Programming and Scripting

Shell script to find and replace contents of files in directory

Hi all This is my first post. Please bear with me with all my mistakes. I started learning shell since couple of days now and this might be quite basic for all, i want to search for files in a directory containing specific string and replace it with new string. The code i wrote is quite bulky... (2 Replies)
Discussion started by: theprogrammer
2 Replies

5. Shell Programming and Scripting

Commenting out a cron entry through a shell script

In my cron thare is a line like 24 11 * * * /usr/batch/bin/abc.sh > /usr/batch/log/abc.log 2>&1 along with other entries. I want to comment out this line through a shell script. My local variable 'line'ontains the full entry (i.e. 24 11 * * * /usr/batch/bin/abc.sh > /usr/batch/log/abc.log... (4 Replies)
Discussion started by: Soham
4 Replies

6. Shell Programming and Scripting

Shell script to search a pattern in a directory and output number of find counts

I need a Shell script which take two inputs which are 1) main directory where it has to search and 2) pattern to search within main directory all files (.c and .h files) It has to print number of pattern found in main directory & each sub directory. main dir --> Total pattern found = 5 |... (3 Replies)
Discussion started by: vivignesh
3 Replies

7. Shell Programming and Scripting

Need to find recursively all shell script in the /xyz directory

Pls. advise how to find or used grep recursively all shell script files. Some files doesnt have a .sh or .ksh extension name. find / -name "*" |xargs grep bin |grep sh ?? TIA (1 Reply)
Discussion started by: budz26
1 Replies

8. Shell Programming and Scripting

Unix script to detect new file entry in directory

Hi All, I want to detect each new file coming / getting created in unix directory. When every new file came to directory, i have to get its details like its size , date and time stamp and store it into another file. Could any one please tell me , how i can achieve that? Thanks. (13 Replies)
Discussion started by: james_1984
13 Replies

9. Shell Programming and Scripting

shell script to find and copy the files creted in the year 2006 to another directory

Hi All, I am new to UNIX. I will be thankful if some one helps me. I have to write a shell script for one of the requirement. I have files created from Jan 2006 to March 2008. My requirement is to write a script in such a way that 1) To find and copy(not Moving) the files created in the... (2 Replies)
Discussion started by: manas6
2 Replies

10. Shell Programming and Scripting

crontab entry modification using shell script

hi Friends, iam trying to write a script which will grep the particular entry in crontab of root and enable/disable it .iam a novice in scripting. your suggestions are most welcome..please help Cheers!! (4 Replies)
Discussion started by: munishdh
4 Replies
Login or Register to Ask a Question