41

I have a directory ~/plugins/ and inside there are many sub-directories. If I wanted to create a backup somewhere else of just the sub-directories starting with abc could I do that with a one line copy command? I would assume something like this would work (but it doesn't):

cp -R ~/plugins/abc* ~/destination/

I would rather use a one-line command, if possible, because I would also like to use the same syntax for rsync, and if I have to do something like

find ~/plugins/ -type d -name "abc*" -exec cp -R {} ~/destination;

then that works fine for the cp command but it would mean that I would have to run rsync once for each directory and that just doesn't seem efficient :(

cwd
  • 53,018
  • 53
  • 161
  • 198

4 Answers4

61

Not sure why what you're trying didn't work (but what is the "copy" command?), but this works on Linux at least:

cp -r ~/plugins/abc* ~/destination
David Gelhar
  • 27,873
  • 3
  • 67
  • 84
  • 1
    i used dos for so many years before switching. that's what the copy command is. I'll try it again and see if it works :) – cwd Mar 16 '11 at 12:52
5

Here is an old trick I still use frequently:

 (cd ~/plugins/ && tar cfp - abc/) | (cd ~/destination && tar xfpv -)

where the p preserves attributes, and ~/destination can be anywhere.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
2

It is possible to use the output of find with rsync:

# warning: untested
find ~/plugins/ -type d -name "abc*" -print0 | rsync -av --files-from=- --from0 ~/plugins/ ~/destination
  • the -print0 in find, and --from0 in rsync makes sure that we handle files with spaces correctly
  • the --files-from=- states that we are reading a list of files from stdin
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
0
#!/usr/bin/env perl
# copie un fichier avec l'arbo
#
#
use File::Basename;
use File::Copy;
my $source = shift;
my $dest = shift;
if( !defined $source){ print "Manque fichier source"; exit(0); }
if( !defined $dest){ print "Manque repertoire dest"; exit(0); }

my $dir = dirname($source);
my $file = basename($source);

my @arbo = split(/\//, $dir);
my $direct  = $dest;
if( !-d $direct ) { mkdir $direct; }
foreach my $d(@arbo) {
        $direct.="/".$d;
        if( !-d $direct ) { mkdir $direct; }
}
copy($source,$direct);

name this code snippet for example copyfile

TESTED : usage : ./copyfile