Find the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find the file
# 1  
Old 11-12-2012
Find the file

Hi,

I am not expert in unix shell scripting but able to write simple one. So please help me to implement following functionality.

There are two directories :
1) /home/guptar/wrk
2) /home/guptar/arc

I need to write a script "arch.sh" which would be executing every 10 minutes through cron. If in last ten minutes there is file reached in wrk directory then script will send a mail and will copy that file to arc folder.

File name convention is prn<counter>.txt.

I know that mtime or ctime can be used but not sure how can I do this?

Kindly help me with the solution.

Thanks in advance.
# 2  
Old 11-12-2012
Sounds a lot like homework. What have you tried so far?
# 3  
Old 11-12-2012
Below is the command to find the files which are 10 minutes old:
Code:
find /home/guptar/wrk/prn* -mmin +5

But unable to use this with if condition. Please guide me.

Last edited by Franklin52; 11-13-2012 at 08:17 AM.. Reason: Please use code tags
# 4  
Old 11-12-2012
I think it should be -mmin -10 to get list of files created in the last 10 minutes. You can do something like below:-
Code:
if [ `find /home/guptar/wrk -mmin -10 | wc -l` -ne 0 ]
then
      for file in `find /home/guptar/wrk -mmin -10`
      do
         cp $file /home/guptar/arc/
      done
fi

# 5  
Old 11-12-2012
Why are you willing to deal with than 10 min constraint ?

Can't you just tell your script to archive all the prn<counter>.txt files ?

Since your script will be scheduled every 10 minutes in the crontab it will just pickup those files that have meanwhile been created (since the last run).

If you are afraid to harvest a file that is currently being updated you can pick up all but not the last one (or all but not the "n" last, just adapt the tail or head number to skip).

Below, some examples to skip the last one.

Code:
ls -t1 | egrep -e 'prn[0-9]+.txt' | tail -n +2 | while read f
do mv "$f" "/my_arch/$f"
done

or ... if you want to process them in the same order they were created (and if your head version does support the negative value of -n option):

Code:
ls -rt | egrep -e 'prn[0-9]+.txt' | head -n -1 | while read f
do mv "$f" "/my_arch/$f"
done

of course this can be enhanced (adapt with you own path, make a copy instead of a move, check the status of the copy or perform some checksum operation before deleting the original file etc ...)

Last edited by ctsgnb; 11-12-2012 at 01:29 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can you use find with ps or doing find excluding file in use

Hi, I am currently using the find below to remove old files. I am redirecting the listing to a file and then use a while-loop and do a rm cd ${directory} find . \( ! -name . -prune \) \( -type f -name "*.trc" -mtime +10 \) | sed 's#^./##' | sed "s#^#${directory}/#" 2>/dev/null | tee -a... (4 Replies)
Discussion started by: newbie_01
4 Replies

2. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

3. Shell Programming and Scripting

wanted to find both link file and ordinary file using single find command

find . -type fl o/p is only the ordinary file. where in it wont give the link files. (2 Replies)
Discussion started by: nikhil jain
2 Replies

4. Shell Programming and Scripting

Find multiple string in one file using find command

Hi, I want find multiple string in one file using find coomand. And keeping it in one variable.grep is not working. (5 Replies)
Discussion started by: vivek1489
5 Replies

5. Shell Programming and Scripting

How to use grep & find command to find references to a particular file

Hi all , I'm new to unix I have a checked project , there exists a file called xxx.config . now my task is to find all the files in the checked out project which references to this xxx.config file. how do i use grep or find command . (2 Replies)
Discussion started by: Gangam
2 Replies

6. Linux

Simplified find command to find multiple file types

Hi, I'm using the following command to find the multiple requierd file types and its working fine find . -name "*.pl" -o -name "*.pm" -o -name "*.sql" -o -name "*.so" -o -name "*.sh" -o -name "*.java" -o -name "*.class" -o -name "*.jar" -o -name "*.gz" -o -name "*.Z" -type f Though... (2 Replies)
Discussion started by: vickramshetty
2 Replies

7. UNIX for Dummies Questions & Answers

How to find a file whick is consuming larger disk space in file system

Hello, Can anybody please tell me the command to find out the filesystem or a file which is consuming larger disk space sing i want to find out the file and want to compress it please help me out any help would be appreciated (6 Replies)
Discussion started by: lokeshpashine
6 Replies

8. UNIX for Dummies Questions & Answers

how to find a file named vijay in a directory using find command

I need to find whether there is a file named vijay is there or not in folder named "opt" .I tried "ls *|grep vijay" but it showed permission problem. so i need to use find command (6 Replies)
Discussion started by: amirthraj_12
6 Replies

9. Shell Programming and Scripting

how to find a file in UNIX without find command?

given a start directory,a filename,how to find it? (3 Replies)
Discussion started by: bluo
3 Replies
Login or Register to Ask a Question