albfan
albfan

Reputation: 12940

Copy contents recursively with existing symbolic links to directories on target

mkdir -p source/{dir1,dir2}
touch source/a
touch source/dir1/b
touch source/dir2/c

mkdir -p target/dir1
mkdir dir_link
ln -s ../dir_link target/dir2

So at start we have:

tree

├── dir_link
├── source
│   ├── a
│   ├── dir1
│   │   └── b
│   └── dir2
│       └── c
└── target
    ├── dir1
    └── dir2 -> ../dir_link

Using:

cp -r source/* target

the result is:

cp: cannot overwrite non-directory 'target/dir2' with directory 'source/dir2'

├── dir_link
├── source
│   ├── a
│   ├── dir1
│   │   └── b
│   └── dir2
│       └── c
└── target
    ├── a
    ├── dir1
    │   └── b
    └── dir2 -> ../dir_link

I'm looking for a command (cp, rsync) that can understand that source/dir2/c should be copied to target/dir2 (which is a dir symbolic link)

The closest will be the -H option for cp

-H follow command-line symbolic links in SOURCE

but applied to TARGET

Upvotes: 0

Views: 1024

Answers (1)

albfan
albfan

Reputation: 12940

rsync -avhK source/* target

follow dir symbolic link correctly:

├── dir_link
│   └── c
├── source
│   ├── a
│   ├── dir1
│   │   └── b
│   └── dir2
│       └── c
└── target
    ├── a
    ├── dir1
    │   └── b
    └── dir2 -> ../dir_link

More info in https://linux.die.net/man/1/rsync -K --copy-dirlinks

Upvotes: 1

Related Questions