- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Powerfull Find
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2008 04:36 AM
тАО09-21-2008 04:36 AM
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,
Solved! Go to Solution.
- Tags:
- find
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2008 04:52 AM
тАО09-21-2008 04:52 AM
Re: Powerfull Find
try this command
#find /var/LOGS -type f -mtime +7 -exec mv -r {} /backup/var/LOGS \;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2008 04:54 AM
тАО09-21-2008 04:54 AM
Re: Powerfull Find
#find /var/LOGS -type f -mtime +2 -exec mv -r {} /backup/var/LOGS \;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2008 05:21 AM
тАО09-21-2008 05:21 AM
Re: Powerfull Find
# 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2008 11:53 AM
тАО09-21-2008 11:53 AM
Re: Powerfull Find
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2008 11:03 PM
тАО09-21-2008 11:03 PM
Re: Powerfull Find
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2008 11:32 PM
тАО09-21-2008 11:32 PM
Re: Powerfull Find
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2008 11:47 PM
тАО09-21-2008 11:47 PM
Re: Powerfull Find
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-22-2008 12:14 AM
тАО09-22-2008 12:14 AM
Re: Powerfull Find
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-22-2008 01:19 AM
тАО09-22-2008 01:19 AM
Re: Powerfull Find
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