1760021 Members
4529 Online
108889 Solutions
New Discussion юеВ

find problem

 
Hunki
Super Advisor

find problem

I have to do a find and then move over the files to a different directory ...

from /log to /log/bak

Which piece am I missing here :

find /log -type f -name "*.gz" -mtime -1 -exec mv{} /log/bak

Also once they are moved the log files to /log/bak I have recreate them in order to record the logs :

is this a gud way :

for files in /log
do
touch $i
done

Can there be a better way here.

Thx!
8 REPLIES 8
Dave Hutton
Honored Contributor

Re: find problem

This should work:
find /log -type f -name "*.gz" -mtime -1 -exec mv{} /log/bak \;

Think your only missing the \; piece to it.
Hunki
Super Advisor

Re: find problem

It still says incomplete statement...
Dave Hutton
Honored Contributor

Re: find problem

You'll get a generic message if /log/bak isn't there. Also I'm assuming off of / really want those files under there. Hopefully you'll never fill it up with some large old log files and up causing bigger issues.

Heres how I tested it:
servera:root /home/dahmin/dahtemp # ll
total 16
drwxr-xr-x 2 root sys 96 Jul 11 15:50 .
drwxr-xr-x 4 dahmin users 8192 Jul 11 15:51 ..
-rw-r--r-- 1 root sys 0 Jul 11 15:50 dahtest
servera:root /home/dahmin/dahtemp # cd ..
servera:root /home/dahmin # ll
total 176
drwxr-xr-x 4 dahmin users 8192 Jul 11 15:51 .
drwxr-xr-x 13 root root 8192 Jun 14 12:08 ..
-rw------- 1 dahmin users 0 Sep 30 2005 .ICEauthority
-rw------- 1 dahmin users 73 Sep 30 2005 .TTauthority
-rw------- 1 dahmin users 49 Sep 30 2005 .Xauthority
-r--r--r-- 1 dahmin users 814 Nov 7 1997 .cshrc
drwxr-xr-x 11 dahmin users 8192 Sep 30 2005 .dt
-rwxr-xr-x 1 dahmin users 5451 Sep 30 2005 .dtprofile
-r--r--r-- 1 dahmin users 347 Oct 27 1997 .exrc
-r--r--r-- 1 dahmin users 341 Nov 7 1997 .login
-rw-r--r-- 1 dahmin users 446 Sep 21 2005 .profile
-rw------- 1 dahmin users 1906 Jul 10 10:24 .sh_history
drwxr-xr-x 2 root sys 96 Jul 11 15:50 dahtemp
-rw-r--r-- 1 root sys 0 Jul 11 15:51 davefile
servera:root /home/dahmin # find . -name *davefile* -exec mv {} ./dahtemp \;
servera:root /home/dahmin # cd dahtemp
servera:root /home/dahmin/dahtemp # ll
total 16
drwxr-xr-x 2 root sys 96 Jul 11 15:52 .
drwxr-xr-x 4 dahmin users 8192 Jul 11 15:52 ..
-rw-r--r-- 1 root sys 0 Jul 11 15:50 dahtest
-rw-r--r-- 1 root sys 0 Jul 11 15:51 davefile

Mel Burslan
Honored Contributor

Re: find problem

if you are having problems with exec construct and can not figure out where it is coming from, you can try to divide the problem into two as follows:

find /log -type f -name "*.gz" -mtime -1 >/tmp/filelist
for file in `cat /tmp/filelist`
do
mv $file /log/bak
done

Hope it helps`
________________________________
UNIX because I majored in cryptology...
Greg Vaidman
Respected Contributor

Re: find problem

be careful with syntax recommended and the specific directories you list...

if your source files are in /log and your destination is in /log/bak, then the find command you list will also find the files in your destination directory, and either create errors or worse, destroy the files when it tries to move them into the same filename.

you'll want to use someting like:
find /log -type f -path "/log/*.gz" \
-mtime -1 -exec mv {} /log/bak \;
(all on one line)

note the "-path" instead of "-name" will only find files in /log but not in subdirectories.
Hunki
Super Advisor

Re: find problem

How do I create the empty log files that have been moved to bak...
James R. Ferguson
Acclaimed Contributor

Re: find problem

Hi:

Your original post:

# find /log -type f -name "*.gz" -mtime -1 -exec mv{} /log/bak

...has *no* whitespace between 'mv' and the opening curly brace. It needs that! :

find /log -type f -name "*.gz" -mtime -1 -exec mv {} /log/bak

Regards!

...JRF...
Hunki
Super Advisor

Re: find problem

Thx to all of you ... figured out everything to this problem