You are not logged in.

#1 2011-12-22 05:27:09

remstereo
Member
From: Egypt
Registered: 2010-07-28
Posts: 16
Website

Comparing two directory , copy content of directory to third directory

I have two directories say A and B. I want to compare directory B with A. Directory A and directory B have many common directories(contains is same) and file.
If file is not present in directory A then copy file to directory C by maintaining directory structure.

e.g. If in Directory A following relative path not is not exist.

B/hellboy/MyScripts/dir1/

I want to copy this path and descendant files and directory to directory C.

I tried a lot with the diff command. But I think it is not possible. Please help me to get out of this.

Offline

#2 2011-12-22 06:16:59

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Comparing two directory , copy content of directory to third directory

Hmm, diff tells you what's different between text files and as an extension a collection of text files. diff is not really meant for this type of task.

The way I would approach it is to use find and test. find will loop over every file and test will test it.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#3 2011-12-22 07:09:21

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,281

Re: Comparing two directory , copy content of directory to third directory

You may possibly use the "cp" command with the "--update" option. See "man cp". Basically this means

- copy dir A to dir C using recursive cp
- update dir C from dir B using cp with the --update option. This should copy newer and missing files from dir B to dir C.

But be cautious. Make some test runs. I never really tried this.


To know or not to know ...
... the questions remain forever.

Offline

#4 2011-12-24 01:21:29

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Comparing two directory , copy content of directory to third directory

@bernarcher
Wouldn't dir C hold files from both A and B in the end?


It should work if you use comm instead of diff and copy with '--parent' switch so the path is preserved and '-r' switch if you want empty directories to be copied too.

├── A
│   ├── 0
│   │   ├── 00
│   │   ├── 01
│   │   │   └── foo
│   │   └── 02
│   ├── 1
│   │   └── 10
│   │       └── foo
│   └── 2
│       └── 20
│           └── foo
├── AnotB
├── B
│   ├── 0
│   │   ├── 01
│   │   └── 02
│   ├── 1
│   │   └── 10
│   │       ├── bar
│   │       └── foo
│   └── 2
│       └── 20
│           └── foo
└── BnotA

A and B differ a bit and I want AnotB to hold files that are in A but are missing from B and BnotA should hold files that are present in B but not in A.

$ cd A && find * | sort > ../a && cd ..
$ cd B && find * | sort > ../b && cd ..

Now I have two files that list the contents of A and B so let's compare them

$ comm -23 a b
0/00
0/01/foo
$ comm -13 a b
1/10/bar

and copy things over

$ cd A && cp -r --parent $(comm -23 ../a ../b) ../AnotB && cd ..
$ cd B && cp -r --parent $(comm -13 ../a ../b) ../BnotA && cd ..
├── AnotB
│   └── 0
│       ├── 00
│       └── 01
│           └── foo
└── BnotA
    └── 1
        └── 10
            └── bar

Offline

#5 2011-12-24 03:49:47

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Comparing two directory , copy content of directory to third directory

In (almost) pure bash,

foo()
{
  cd "$1"
  for file in *
  do
    ! [[ -e $A/$file ]] && { cp -a "$1/$file" "$C"; continue; }
    foo "$1/$file"
  done
}
foo "$B"

aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#6 2011-12-24 07:45:20

Diaz
Member
From: Portugal
Registered: 2008-04-16
Posts: 366

Re: Comparing two directory , copy content of directory to third directory

Seems like homework to me :x

Depending on what tools you can use there are some diferrent ways of doing this like mentioned above ^^.

Last edited by Diaz (2011-12-24 07:46:38)

Offline

Board footer

Powered by FluxBB