PDA

View Full Version : Problem with find command in a for loop in Shell Scripting



kaylayap
23-Nov-2011, 19:46
Hi All,

I want to write a shell script that will find all the files modified
within 24 hours and copy those files into a new directory.
I have written the script but it is not working, it return me with all
the files instead of only those that modified within 24 hours.
Below is my script, appreciated if some one can help........

#!/bin/sh

DATE=`date +%y%m%d`

################################################## ###############################
# Part 1 : Move IDM log files to log archive directory
#
################################################## ###############################

cd /var/opt/novell/log/
mkdir /data/daily_log_archive/IDM_log_$DATE

for i in `ls`
do
if find ./$i -mtime -1 -type f
then
cp $i /data/daily_log_archive/IDM_log_$DATE
fi
done

tar cvzfp /data/daily_log_archive/IDM_log_$DATE.tar.gz
/data/daily_log_archive/IDM_log_$DATE
rm -rf /data/daily_log_archive/IDM_log_$DATE

################################################## ###############################


--
kaylayap
------------------------------------------------------------------------
kaylayap's Profile: http://forums.novell.com/member.php?userid=94293
View this thread: http://forums.novell.com/showthread.php?t=448641

malcolmlewis
23-Nov-2011, 20:03
On Wed, 23 Nov 2011 18:46:02 GMT
kaylayap <kaylayap@no-mx.forums.novell.com> wrote:

>
> Hi All,
>
> I want to write a shell script that will find all the files modified
> within 24 hours and copy those files into a new directory.
> I have written the script but it is not working, it return me with all
> the files instead of only those that modified within 24 hours.
> Below is my script, appreciated if some one can help........
>
> #!/bin/sh
>
> DATE=`date +%y%m%d`
>
> ################################################## ###############################
> # Part 1 : Move IDM log files to log archive directory
> #
> ################################################## ###############################
>
> cd /var/opt/novell/log/
> mkdir /data/daily_log_archive/IDM_log_$DATE
>
> for i in `ls`
> do
> if find ./$i -mtime -1 -type f
> then
> cp $i /data/daily_log_archive/IDM_log_$DATE
> fi
> done
>
> tar cvzfp /data/daily_log_archive/IDM_log_$DATE.tar.gz
> /data/daily_log_archive/IDM_log_$DATE
> rm -rf /data/daily_log_archive/IDM_log_$DATE
>
> ################################################## ###############################
>
>
Hi
Do it all on one line, eg;


find . -type f -mtime -1 -exec ls {} \;


You might be better of using rsync?

--
Cheers Malcolm °¿° (Linux Counter #276890)
SUSE Linux Enterprise Desktop 11 (x86_64) Kernel 2.6.32.46-0.3-default
up 19:40, 2 users, load average: 0.00, 0.02, 0.00
GPU GeForce 8600 GTS Silent - Driver Version: 290.10

sammyjeep
24-Nov-2011, 04:06
command "find ./$i -mtime -1 -type f " would return 0 even if it didn't
find the file.
using "find ... -exec" or "find ... | xarg ..." instead.


--
sammyjeep
------------------------------------------------------------------------
sammyjeep's Profile: http://forums.novell.com/member.php?userid=120054
View this thread: http://forums.novell.com/showthread.php?t=448641