LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to copy an entire directory structure except certain files? (https://www.linuxquestions.org/questions/programming-9/how-to-copy-an-entire-directory-structure-except-certain-files-385321/)

thanhvn 11-21-2005 07:21 PM

How to copy an entire directory structure except certain files?
 
Suppose I have a large directory with subdirectories nested to arbitrary levels and I want to copy this entire directory, except certain files in it, to another location, what are the shell commands to do this?

This is what I have so far:

Code:

find sourceDir \( ! -name file1.txt -a ! -name file2.log \) -exec cp -ap {} destDir \;
But this doesn't work. Suppose I have a directory as follows:

dir1/dir1sub1/file3.log

when I runs the above command to copy dir1 to dir2, I get this:

dir2/dir1/dir1sub1/file3.log
dir2/dir1sub1/file3.log
dir2/file3.log

That is, the command just recursively copies everything in dir1 to dir2 without preserving directory hierarchies. I've tried several ways but none seems to work. The clause -prune only works on directories, not files. Grep searches file contents, not filenames. When -exec expands the arguments to the cp command from {}, the directory hierarchies are lost.

Any ideas?

chrism01 11-21-2005 10:56 PM

It might be easier to copy everything first, eg
cp -r <from> <to>
then write script to remove unwanted files.
Something like this:
Code:

for file in `find .`
do
    base=`basename $file`
    rslt=`grep $base exclude.lst`
    if [[ $? -eq  0 ]]
    then
        rm $file
    fi
done


thanhvn 11-21-2005 11:18 PM

That would probably be fine by me if space isn't an issue. The thing is, I'm copying from HD to my USB flash drive, and the files to be excluded are really huge.

I suppose I can make a copy of the directory on HD, remove the unwanted files, copy the cleaned up directory to the thumb drive, then delete the cleaned up directory. But that seems wasteful in terms of disk space and waiting time (the directory is tens of GBs), not to mention the impracticality when disk space is low.

If there is another way, I would prefer to avoid this route. (Not to mention the excitement of enhancing my Linux skills.)

freegianghu 11-22-2005 01:34 AM

Have a try with tar
Code:

$ tar -c --exclude=*.dll --exclude=*.exe sourceDir | tar -x -C destDir
Hopes it help,
Giang Hu

bigearsbilly 11-22-2005 05:13 AM

try something like:

find blah_blah -type f | cpio -pd destination

this will preserve directories

keefaz 11-22-2005 07:08 AM

You could also use rsync

thanhvn 12-01-2005 08:26 PM

freegianghu, bigearsbilly, that's worked. Thanks.

Imbeciles 08-16-2010 10:27 AM

HOTO using scp
 
I have 2 servers and like to copy my /var/www directory BUT want to skip certain directories in it.

I can make a abckup and rester it but the elegant way would be to use SCP
Code:

scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ...
        [[user@]host2:]file2

However i'm unsure how to specify the "NOT/Exclude part"
E.G.

Code:

/usr/bin/scp -R root@source-server:/var/www/* /var/www
would copy everything I 'd like to skip some!

Ta-mater 01-27-2012 11:24 AM

Quote:

Originally Posted by bigearsbilly (Post 1963492)
try something like:

find blah_blah -type f | cpio -pd destination

this will preserve directories

7 years later, thanks.

sadiqdm 01-27-2012 11:41 AM

Try Krusader. It's a 2 panel file manager like the old PC Commander from the days of DOS. It has very good filters which include ignoring individual files, directories, extensions, dates, etc.

I had been using Unison which is a useable GUI for rsync, but Krusader is not only better but faster. I now use it for backups to USB & NAS and for copying between desktop and laptop.


All times are GMT -5. The time now is 05:00 PM.