1751829 Members
5025 Online
108782 Solutions
New Discussion юеВ

Powerfull Find

 
SOLVED
Go to solution
AZayed
Super Advisor

Powerfull Find

Dears,

Good day,

I have an issue with find command I would like to find files older than 2 days and move them to specific location. The issue is I want to build the exact same tree in the destination directory.

For example, I want to search /var/LOGS and move anything older than two days to /backup/var/LOGS. If there was a sub directories in the source I want to create it in the destination.

Regards,
Success seems to be connected with action. Successful people keep moving. They make mistakes, but they don't quit.
32 REPLIES 32
Jeeshan
Honored Contributor

Re: Powerfull Find

Salam Ahmed

try this command

#find /var/LOGS -type f -mtime +7 -exec mv -r {} /backup/var/LOGS \;
a warrior never quits
Jeeshan
Honored Contributor

Re: Powerfull Find

mistake

#find /var/LOGS -type f -mtime +2 -exec mv -r {} /backup/var/LOGS \;
a warrior never quits
James R. Ferguson
Acclaimed Contributor

Re: Powerfull Find

Hi:

# cd /var
# find ./LOGS -depth -mtime +2 -print | cpio -pudlvm /backup/var

...will *copy* and replicate the directory/file structure in the destination directory. You will then need to remove what you don't want from the source tree in a separate pass.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Powerfull Find

This solution does it in one pass but you'll need to write a customized script:

Starting from Ahsan's basic find:
find /var/LOGS -type f -mtime +2 -exec fancy_script {} +

Then create fancy_script:
#!/usr/bin/ksh
# Move files to /backup, keeping directory paths
for file in $*; do
mkdir -p /backup/$(dirname $file)
mv $file /backup/$file
done
AZayed
Super Advisor

Re: Powerfull Find

Thanks for reply,

Dear ahsan, mv doesn't have -r option it's only in cp.

Dears Dennis Handly and James R. Ferguson, I have this idea already, creating a script and calling it from find command. the script will use cp -r -p $1 $2 to create the specific tree. but I want to use mv command. This solution is very good but I prefer mv.

FANCY SCRIPT :

cp -r -p $1 $2
rm $1


FIND COMMAND :

find /var/LOGS -type f -mtime +2 -exec FANCY_SCRIPT {} /backup/var/LOGS-DATE \;

Any suggestion.

Regards,
Success seems to be connected with action. Successful people keep moving. They make mistakes, but they don't quit.
OFC_EDM
Respected Contributor

Re: Powerfull Find

Thought I'd throw my hat in the ring with another idea.

Step 1. Copy the entire directory structure to the new location. Including the files you don't want.
"cp -r" or "cp -p -r" to preserve permisssions

Step 2. Nuke the files from the new structure which you don't want.

I'd test this out first by replacing rm with ls -l just to make certain....I don't have a system to test this on today and this is from memory.

Example:
Find files in current directory only older than June 3rd 2008

1. Create reference file
touch -t 200806030000 /tmp/june3.ref
Format of the numeric is YYYYMMDDHHMM
- use some logic in the script to calculate proper value to represent 2 days agos. I like this because you can control it down to the minute.

2 Run the command
find ./loc/A/* ! -type d ! -newer /tmp/june3.ref -exec rm {} \;

If you like it then stick this in a script to automate.

I'll try to find my script which has all the little things like calculating the date etc for the touch command. But it'll be a few days.

Cheers
The Devil is in the detail.
OFC_EDM
Respected Contributor

Re: Powerfull Find

I'll add one more option.

The previous example I put in goes into sub-directories.

However if at some point you only want to process the current directory stick in the -prune argument

find ./loc/A/* -prune ! -type d ! -newer /tmp/june3.ref -exec rm {} \;

Regards
The Devil is in the detail.
Steven E. Protter
Exalted Contributor

Re: Powerfull Find

Shalom,

A word of caution.

All it takes is about 4 finds starting out at root to totally bring even a powerful system to its knees.

Use carefully, preferably one at a time.

Very interesting thread.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Dennis Handly
Acclaimed Contributor

Re: Powerfull Find

>I have this idea already, creating a script and calling it from find command. The script will use cp -r -p $1 $2 to create the specific tree. but I want to use mv command.

My script already does mv(1). What's wrong with it? It will only create the needed parts of the tree.

>find /var/LOGS -type f -mtime +2 -exec FANCY_SCRIPT {} /backup/var/LOGS-DATE \;

If you are changing the path in your target, then you'll need to fiddle with my script a little:
find /var/LOGS -type f -mtime +2 -exec FANCY_SCRIPT /backup/var/LOGS-DATE {} +

FANCY_SCRIPT:
#!/usr/bin/ksh
TARGET=$1
shift
for file in $*; do
mkdir -p $TARGET/$(dirname $file)
mv $file $TARGET/$file
done