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 \; 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? |
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 .` |
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.) |
Have a try with tar
Code:
$ tar -c --exclude=*.dll --exclude=*.exe sourceDir | tar -x -C destDir Giang Hu |
try something like:
find blah_blah -type f | cpio -pd destination this will preserve directories |
You could also use rsync
|
freegianghu, bigearsbilly, that's worked. Thanks.
|
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 ... E.G. Code:
/usr/bin/scp -R root@source-server:/var/www/* /var/www |
Quote:
|
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. |