You are not logged in.
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
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
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
@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
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
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