Here is my problem:
I have folder called archive
that contains many subdirectories and files in the following format:
/home/user/archive/$YYYY/$MM/$DD
I would like to copy some specific files to /tmp
while keeping part of the directory tree.
So far, I have come up with
find /home/user/archive -mtime +1 -type f -exec cp --parents {} /tmp;
(the find ... -mtime +1 -type f
part actually returns what I want to copy)
However, the output is in format /tmp/home/user/archive/$YYYY/$MM/$DD
whereas my desired output format is /tmp/archive/$YYYY/$MM/$DD
Any solutions? :)
One option would be run the copy through awk's system function after parsing the output of find. Such a solution would require that the directory/subdirectory structures are the same in all cases though:
find /home/user/archive -mtime +1 -type f | awk -F\/ '{ print "mkdir -p /tmp/"$4"/"$5"/"$6";cp --parents "$0 " /tmp/"$4"/"$5"/"$6"/"$7 }'
This will print the copy command that you need to execute for each entry found from the find command. Ensure that the commands print as expected (This step is very important)
Once you have verified that commands display as expected, you can run the actual copy commands through awk's system function:
find /home/user/archive -mtime +1 -type f | awk -F\/ '{ system("mkdir -p /tmp/"$4"/"$5"/"$6";cp --parents "$0 " /tmp/"$4"/"$5"/"$6"/"$7) }'