LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-21-2005, 07:21 PM   #1
thanhvn
Member
 
Registered: Mar 2005
Location: CA
Distribution: RHEL3, FC4
Posts: 46

Rep: Reputation: 15
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?
 
Old 11-21-2005, 10:56 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,280

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
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
 
Old 11-21-2005, 11:18 PM   #3
thanhvn
Member
 
Registered: Mar 2005
Location: CA
Distribution: RHEL3, FC4
Posts: 46

Original Poster
Rep: Reputation: 15
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.)
 
Old 11-22-2005, 01:34 AM   #4
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
Have a try with tar
Code:
$ tar -c --exclude=*.dll --exclude=*.exe sourceDir | tar -x -C destDir
Hopes it help,
Giang Hu
 
Old 11-22-2005, 05:13 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
try something like:

find blah_blah -type f | cpio -pd destination

this will preserve directories
 
Old 11-22-2005, 07:08 AM   #6
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,578

Rep: Reputation: 127Reputation: 127
You could also use rsync
 
Old 12-01-2005, 08:26 PM   #7
thanhvn
Member
 
Registered: Mar 2005
Location: CA
Distribution: RHEL3, FC4
Posts: 46

Original Poster
Rep: Reputation: 15
freegianghu, bigearsbilly, that's worked. Thanks.
 
Old 08-16-2010, 10:27 AM   #8
Imbeciles
LQ Newbie
 
Registered: Oct 2005
Location: Centurion
Distribution: Ubuntu
Posts: 11

Rep: Reputation: 1
Question 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!
 
Old 01-27-2012, 11:24 AM   #9
Ta-mater
LQ Newbie
 
Registered: Apr 2011
Posts: 10

Rep: Reputation: 0
Quote:
Originally Posted by bigearsbilly View Post
try something like:

find blah_blah -type f | cpio -pd destination

this will preserve directories
7 years later, thanks.
 
Old 01-27-2012, 11:41 AM   #10
sadiqdm
Member
 
Registered: Nov 2003
Location: London, UK
Distribution: openSUSE, Ubuntu
Posts: 357

Rep: Reputation: 35
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.
 
  


Reply

Tags
scp


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy Directory Structure Only ronin1 Linux - Newbie 10 06-08-2013 06:23 AM
[SOLVED] rm files only - retain directory structure schneidz Programming 15 05-30-2013 04:08 PM
Using wget to copy entire ftp directory stuartmunro Linux - Newbie 7 06-17-2005 07:06 PM
Copy directory structure? tpe Programming 2 06-02-2005 04:59 AM
Program files and directory structure Carel Linux - Newbie 5 04-11-2001 10:57 AM


All times are GMT -5. The time now is 03:27 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration