Copying a files from a filter list and creating their associated parent directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying a files from a filter list and creating their associated parent directories
# 1  
Old 05-04-2010
Copying a files from a filter list and creating their associated parent directories

Hello all,

I'm trying to copy all files within a specified directory to another location based on a find filter of mtime -1 (Solaris OS). The issue that I'm having is that in the destination directory, I want to retain the source directory structure while copying over only the files that have been modified within 24 hours (so cp -r option is out).

For example, if I've got the following directory structure as my source

Code:
/datafiles
/datafiles/a/a1.txt (mod 12 hours ago) 
/datafiles/a/a2.txt (mod 36 hours ago)
/datafiles/b/b1.txt (mod 36 hours ago)
/datafiles/b/b2.txt (mod 12 hours ago)

And my target is /modfiles, then after the copy I would want the following:

Code:
/modfiles/datafiles/a/a1.txt
/modfiles/datafiles/b/b2.txt

I'm trying:

Code:
find /datafiles -mtime -1 -exec cp '{}' /modfiles \;

I'm trying to avoid having to use TAR for this to maintain the directory structure but it's starting to look like my only option. Any ideas?

Thanks!

Last edited by Scott; 05-05-2010 at 05:25 PM.. Reason: Code tags please...
# 2  
Old 05-04-2010
I was not sure of this , how ever googled and end up in the link:

How do I selectively copy files from a directory structure? :: Free Tech Support :: Ask Dave Taylor!

So , you can modify your "find" command something like this:

Code:
 
find /datafiles -mtime -1  | cpio -pavd DESTINATION_DIRECTORY

Let me know , if it worked out!
# 3  
Old 05-04-2010
That's a great solution, so long as he has cpio and none of the filenames contain a newline (usually a sane assumption Smilie).

If the OP has cpio but needs to handle filenames with a newline, some implementations of cpio support a -0 option which can be used in conjunction with find's -print0.

Regards,
Alister
# 4  
Old 05-04-2010
This solution does work in regular shells unfortunately the shell I'm using is a custom one from a product that is based on UNIX but does not have the CPIO command which is very unfortunate.
Thank you for the response...I think I may have to look into some alternatives.
# 5  
Old 05-05-2010
Just a quick update on my progress...it seems that the pax utility does the trick so far for what I need:

Code:
pax -rw -u -T 0000 -p am /datafiles /modfiles

This seems to copy all files that were modified within 1 day from /datafiles to /modfiles directory and does not preserve mod time (which is what I need).

It also seems to copy over the parent directory structures of the files which is great. Now for some further testing...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying files to directories based on first 6 character

guys, i did create a script but its too long, though it function the same. # cat nightlyscan.sh #!/usr/ksh deyt=`date +"%Y-%m-%d"` for i in `ls -lrt|grep $deyt|awk '{print $9}'` do cp -f $i /S1/Sophos/logger/ done # but i did not paste it all. this is the desired. (9 Replies)
Discussion started by: kenshinhimura
9 Replies

2. Shell Programming and Scripting

Archiving and moving files into directories, creating directories, etc.

how can i move "dataName".sql.gz into a folder called 'database' and then move "$fileName".tar.gz * .htaccess into a folder called 'www' with the entire gzipped file being "$fileName".tar.gz? Is this doable or overly complex. so mydemo--2015-03-23-1500.tar.gz > database -... (5 Replies)
Discussion started by: wyclef
5 Replies

3. Shell Programming and Scripting

Copying files from parent directory only

Hello, Please can someone assist on a issue I am having. I want to find specific files in the parent directory only that have been modified over the last 2 days and copy them to another location. NOTE: The version of AIX I am using does not have MAXDEPTH. I have currently written the... (3 Replies)
Discussion started by: Dolph
3 Replies

4. Shell Programming and Scripting

Copying data from files to directories

I have the following that I'd like to do: 1. I have split a file into separate files that I placed into the /tmp directory. These files are named F1 F2 F3 F4. 2. In addition, I have several directories which are alphabetized as dira dirb dirc dird. 3. I'd like to be able to copy F1 F2 F3 F4... (2 Replies)
Discussion started by: newbie2010
2 Replies

5. Shell Programming and Scripting

Copying the files after filter

Hi Guys, i want copy the all files another direcotry after filtering the command. and tried as like below...it's not working. ls -ltr|awk '{print $9}'|grep "images\|\.htm"|cp *.* /home/oracle Thanks (13 Replies)
Discussion started by: bmk
13 Replies

6. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

7. UNIX for Dummies Questions & Answers

Creating named directories from a list of names

Hi There, I'm a competent computer user that is learning basic unix commands. This is a slightly hypothetical question (for now at least!) but I can see a need for doing something like this in the near future. So I recently learned this command mkdir foo{1..100} which of course create's... (4 Replies)
Discussion started by: lookslikejames
4 Replies

8. Shell Programming and Scripting

Moving files from several directories into parent

I am fairly new to bash(but am proficient in C++), and have only completed a few simple scripts. This is my first script that I actually need to do a serious task. All of my audiobooks are stored in traditional MP3 format: Music/Artist/Album/*.mp3 (which in this case is... (0 Replies)
Discussion started by: gamendorf
0 Replies

9. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

10. Shell Programming and Scripting

Backup script: Copying and removing directories based on list

I am writing a simple backup script, but I cannot figure out how to remove directories that are found in a list. For example: DONT_COPY=" .adobe/ .bin/google-earth " tar -zcvf - * --exclude=$DONT_COPY | openssl des3 -salt -k $1 | dd of=$(hostname)-$(date +%Y%m%d).tbz > COPIED Note that... (4 Replies)
Discussion started by: dotancohen
4 Replies
Login or Register to Ask a Question