Visit The New, Modern Unix Linux Community


Copy Specific Files Recursively


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy Specific Files Recursively
# 1  
Copy Specific Files Recursively

Is it possible to only copy selected files+its directories when you are copying recursively?
Code:
find /OriginalFolder/* -type -d \{ -mtime 1 -o -mtime 2 \ } -exec cp -R {} /CopyTo/'hostname'__CopyTo/ \; -print

From the above line, I want to only copy *txt and *ini files from /OriginalFolder/*

Regards

Last edited by vbe; 12-31-2017 at 07:24 AM.. Reason: code tags please even for so lttle...
# 2  
Did you consider the -name or -iname test?
# 3  
Yeah. I am probably doing it wrong.

I tried the following command, which didn't work:
Code:
find /OriginalFolder/* -type -d \{ -mtime 1 -o -mtime 2 -o -name *.ini -o -name *.txt \ } -exec cp -R {} /CopyTo/'hostname'__CopyTo/ \; -print

So I am trying to see which folders were modified 1 or 2 days ago and only copy over the files *.ini and *.txt (including the folder which contains them).

Last edited by Scrutinizer; 01-06-2018 at 06:04 PM.. Reason: code tags
# 4  
I'm not sure (and can't test it right now) if the -type d gets in the way as it only allows for directories and you want files...

On top, the (logical connection of the) -mtimes might not deliver what you expect it to.
# 5  
The find primary -mtime 1 looks for file that were modified exactly 24 hours ago to the finest resolution of timestamps available on the filesystem you're traversing. If you want to look for files that were modified sometime up to 48 hours ago, you would do that with:
Code:
find ... -mtime -2

And, if you need to group primaries together in an expression, you need to surround them with escaped parentheses such as:
Code:
\( primary -o primary \)

not with braces such as:
Code:
\{ primary -o primary \}

and there can never be any space between the backslash character and the escaped parenthesis following it.

Maybe you want something more like:
Code:
find $(find /OriginalFolder -type -d -mtime -2) -depth \( -name '*.ini' -o -name '*.txt' \)

to get a list of absolute pathnames of the files you want to copy as long as the files you want to copy only appear in leaf directories. You'll have to do some post-processing of the output to recreate the directory hierarchy using something like cpio, pax, or tar to keep the original ownership and modes.

But, of course, this assumes that your list of directories updated in the last two days is not large enough to cause a command-line too long error as it constructs the outermost find command.

And, as RudiC noted, if a file in a directory is updated without creating a new file or deleting an old file, the directory timestamp won't be updated.

None of this has been tested, but hopefully it will give you a way to start moving forward. Note that if directories other than those that are leaves on the tree are updated, some of the files in the list produced might not actually be in a directory whose leaf directory changed in the last two days AND if non-leaf directories are updated some files may appear in the output list more than one time so you may need to weed out duplicates.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #195
Difficulty: Easy
The top 2 programming languages, according to the TIOBE Index for October 2019, was Java and C.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy files recursively to one single directory

I need to copy a complete directory structure into a new location. But I want to have all files copied into one directory and leave out the directory structure. So all files must be placed in one directory. (4 Replies)
Discussion started by: ReneVL
4 Replies

2. Shell Programming and Scripting

Find Large Files Recursively From Specific Directory

Hi. I found many scripts in the web of achieving this. But I like to use this one find /EDWH-DMT03 -xdev -size +10000 -exec ls -la {} \;|sort -n -k 5 > LARGE.rst But the problem is, why it still list out files with 89 bytes as the output? Is there anything wrong with the command? My... (7 Replies)
Discussion started by: aimy
7 Replies

3. Shell Programming and Scripting

How to recursively copy directory only for recent files?

I love the -newerct flag for the Cygwin find command on windows. Can I use "/usr/bin/find . -newerct '3 hours ago'" to conditionally copy a directory tree so that only the files in the directory tree that are younger than 3 hours are copied to my destination directory such that the directory... (4 Replies)
Discussion started by: siegfried
4 Replies

4. UNIX for Dummies Questions & Answers

Copy files recursively

Hello! I know what i s recursion, but can't imagine what shoudl be "recursicve copying" of files? Please, what should mean: cp -r /home/hope/files/* /home/hope/backup Can someone helpme with a simple example? Many thanks!!! (6 Replies)
Discussion started by: pinklemon
6 Replies

5. Shell Programming and Scripting

How to copy a directory without specific files?

Hi I need to copy a huge directory with thousands of files onto another directory but without *.WMV files (and without *.wmv - perhaps we need to use *.). Pls advise how can I do that. Thanks (17 Replies)
Discussion started by: reddyr
17 Replies

6. Shell Programming and Scripting

Recursively move directories along with files/specific files

I would like to transfer all files ending with .log from /tmp and to /tmp/archive (using find ) The directory structure looks like :- /tmp a.log b.log c.log /abcd d.log e.log When I tried the following command , it movies all the log files... (8 Replies)
Discussion started by: frintocf
8 Replies

7. Shell Programming and Scripting

copy specific files and count them - not as easy as it seems!

Hi all: Here's my dilemma: to identify files of a specific type, copy them to a new location while preserving the original file attributes (date, time, full path, etc), and at the same time capture the count of the number of files identified as a variable for later reporting. Here's where I... (9 Replies)
Discussion started by: Yamaha Evans
9 Replies

8. Shell Programming and Scripting

Copy only files recursively

Hi, find . | xargs -s 47518 can list all the files and directories recursively , is there any possibility to copy only files from directories and subdirectoreis once it is listed. Please help Thans & Regards Uma (3 Replies)
Discussion started by: umapearl
3 Replies

9. Shell Programming and Scripting

Recursively copy only specific files from a directory tree

Hi I am a shell-script newbie and am looking to synchronize certain files in two directory structures. Both these directory-trees are in CVS and so I dont want the CVS directory to be copied over. I want only .sh and .pl files in each subdirectory under these directory trees to be... (3 Replies)
Discussion started by: sharpsharkrocks
3 Replies

10. Solaris

To copy the files newer than specific date

Dear all, Can you help me in copying files newer than speciifc date Thanks in advance, Rajesh (3 Replies)
Discussion started by: RAJESHKANNA
3 Replies

Featured Tech Videos