LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 07-06-2010, 06:26 PM   #1
MoonMan89
LQ Newbie
 
Registered: Jun 2006
Posts: 4

Rep: Reputation: 0
Question Command to copy files older than <n> days keeping dir structure.


Hey Guys,
I'm trying to truncate a postfix Maildata directory for one of our users.
I want to be able to move any files older than <n> days to a new location, but also copying the relevant directory structure. This should be doable in the one comman. I've used find to locate the files, and mv to move them, but I can't figure out how to build the directoryt structure on the fly in the new location.

This is what I have so far:
Code:
 find /Maildata/editor -mtime +42 -type f -exec mv -v {} /EditorEmailOld/ \; > ~/editor.txt
Any help is appreciated.
Thanks.
 
Old 07-06-2010, 07:56 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Hi, welcome to LQ!

You could create a script ... something like [untested!!]:
mv_w_dir.sh
Code:
#!/bin/bash
file=$1
dir=$(dirname $file)
mkdir -p /EditorEmailOld/${dir}
mv -v $file /EditorEmailOld/${dir}
Not elegant, no error checking, won't handle embedded spaces, ....


Use the script in your exec-statement.



Cheers,
Tink
 
Old 07-07-2010, 12:14 AM   #3
MoonMan89
LQ Newbie
 
Registered: Jun 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Hmm,
I've got special characters in the files, and maybe spaces.
I've seen someone use CPIO but I've never used that command and don't really understand it.

I may just have to add support for spaces/special chars into that script above.

Thanks!
 
Old 07-07-2010, 02:20 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,490

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
To manage spaces and special characters, double quotes around $dir and $file might be enough. An alternative could be rsync, which is suited to recreate the directory structure and manage weird file names as well. For example:
Code:
$ cd /Maildata/editor
$ find . -mtime +42 -type f | rsync -av --dry-run --files-from=- . /EditorEmailOld
Please note two things: 1. running the find command inside the source directory avoids additional steps to strip out the leading (unwanted) directories from the destination path (otherwise you will end-up with a directory /EditorEmailOld/Maildata/editor containing the copied files); 2. I intentionally put the option --dry-run of rsync for testing purposes: it just mimics the behaviour of rsync without actually copying anything. After you've checked the result (verbose output) you can safely run the command again without it. Hope this helps.
 
Old 07-15-2010, 10:28 PM   #5
MoonMan89
LQ Newbie
 
Registered: Jun 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
To manage spaces and special characters, double quotes around $dir and $file might be enough. An alternative could be rsync, which is suited to recreate the directory structure and manage weird file names as well. For example:
Code:
$ cd /Maildata/editor
$ find . -mtime +42 -type f | rsync -av --dry-run --files-from=- . /EditorEmailOld
Thank you, This was exactly what I needed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Delete old files older than 7 days rjo98 Linux - Newbie 3 09-18-2009 01:15 PM
Script help - delete files older than 45 days but exclude the system files jojothedogboy Linux - Software 3 06-13-2008 03:43 PM
Delete files older then 30 days stefaandk *BSD 1 01-07-2008 08:31 PM
How to move files older than 30 days gfem AIX 8 11-08-2006 04:58 AM
delete files older than 30 days using cronjob latheesan Linux - Newbie 5 06-14-2005 02:40 PM


All times are GMT -5. The time now is 03:22 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration