LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Command to copy files older than <n> days keeping dir structure. (https://www.linuxquestions.org/questions/linux-general-1/command-to-copy-files-older-than-n-days-keeping-dir-structure-818366/)

MoonMan89 07-06-2010 06:26 PM

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.

Tinkster 07-06-2010 07:56 PM

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

MoonMan89 07-07-2010 12:14 AM

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!

colucix 07-07-2010 02:20 AM

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.

MoonMan89 07-15-2010 10:28 PM

Quote:

Originally Posted by colucix (Post 4025906)
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.


All times are GMT -5. The time now is 06:56 AM.