# Problem with find command in a for loop in Shell Scripting

**URL:** <https://forums.suse.com/t/problem-with-find-command-in-a-for-loop-in-shell-scripting/21874>\
**Category:** SLES Configure-Administer\
**Created:** [November 23, 2011, 8:46pm UTC](https://forums.suse.com/t/problem-with-find-command-in-a-for-loop-in-shell-scripting/21874 "2011-11-23T20:46:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![System1](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@System1](https://forums.suse.com/u/System1)\
**Post date:** [November 23, 2011, 8:46pm UTC](https://forums.suse.com/t/problem-with-find-command-in-a-for-loop-in-shell-scripting/21874/1 "2011-11-23T20:46:02Z")

</div>

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](http://forums.novell.com/member.php?userid=94293)  
View this thread: [http://forums.novell.com/showthread.php?t=448641](http://forums.novell.com/showthread.php?t=448641)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex022/uploads/suse/original/2X/5/5012ba89e3ffb5220dac47d5ea0ba032e2fe1cb6.png) [@system](https://forums.suse.com/u/system)\
**Post date:** [November 23, 2011, 9:03pm UTC](https://forums.suse.com/t/problem-with-find-command-in-a-for-loop-in-shell-scripting/21874/2 "2011-11-23T21:03:14Z")

</div>

On Wed, 23 Nov 2011 18:46:02 GMT  
kaylayap [kaylayap@no-mx.forums.novell.com](mailto:kaylayap@no-mx.forums.novell.com) wrote:  
[color=blue]

> 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
> 
> #################################################################################
> 
> [/color]  
> Hi  
> Do it all on one line, eg;

```auto
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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex022/uploads/suse/original/2X/5/5012ba89e3ffb5220dac47d5ea0ba032e2fe1cb6.png) [@system](https://forums.suse.com/u/system)\
**Post date:** [November 24, 2011, 5:06am UTC](https://forums.suse.com/t/problem-with-find-command-in-a-for-loop-in-shell-scripting/21874/3 "2011-11-24T05:06:01Z")

</div>

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](http://forums.novell.com/member.php?userid=120054)  
View this thread: [http://forums.novell.com/showthread.php?t=448641](http://forums.novell.com/showthread.php?t=448641)
