skip to Main Content

I use Ubuntu 20.04 and i have a lot of files under multiple subfolders under a parent folder. I need to copy only symlinks from within these subfolders to another folder maintaining the source folder structure. I need to retain them as it is with exactly same relative path information so that when they are in a new destination folder they still will continue to behave same as in source folder(destination folder has same folders/files as in source).

This is what I have:

Source/file1

Source/folder1/file2

Source/folder1/symlink1 -> ./file2

Source/folder2/symlink2 -> ../file1

This is what I need (symlinks will be ultimately moved to a folder where all files in source will be present in same folder structure)

Destinationfolder1symlink1 -> ./file2

Destinationfolder2symlink2 -> ../file1

Any help would be appreciated.

I have tried these below already

find ./ -type l | xargs -I % cp -va % Destination/
Result: all symlinks get copied to destination folder but they lose the source folder structure.

find . -type l | xargs -i cp {} Destination{}

Result: cp: cannot create regular file 'Destination./tools/i18n-uitext/node_modules/istanbul-lib-source-maps/node_modules/.bin/rimraf': No such file or directory

Here (below) are some of the symlinks i have in source folder.

Listed below symlinks using command find ./ -type l -print0 | xargs -0 ls -plah from within the source directory. I am intending to copy these symlinks to another directory where they need to maintain this same folder structure.

Note: The source directory is a CI machines workspace folder on one server and the destination directory is a folder outside this workspace folder where i need to copy these symlinks and folders containing them into.

lrwxrwxrwx 1   19 Jul  4 18:08 ./integrations/link_adyen_v20_1_3/node_modules/.bin/_mocha -> ../mocha/bin/_mocha
lrwxrwxrwx 1   18 Jul  4 18:08 ./integrations/link_adyen_v20_1_3/node_modules/.bin/acorn -> ../acorn/bin/acorn
lrwxrwxrwx 1   19 Jul  4 18:08 ./integrations/link_adyen_v20_1_3/node_modules/.bin/atob -> ../atob/bin/atob.js

lrwxrwxrwx 1   19 Jul  4 18:05 ./node_modules/.bin/_mocha -> ../mocha/bin/_mocha
lrwxrwxrwx 1   18 Jul  4 18:05 ./node_modules/.bin/acorn -> ../acorn/bin/acorn
lrwxrwxrwx 1   19 Jul  4 18:05 ./node_modules/.bin/atob -> ../atob/bin/atob.js
lrwxrwxrwx 1   32 Jul  4 18:05 ./node_modules/.bin/autoprefixer -> ../autoprefixer/bin/autoprefixer

lrwxrwxrwx 1   20 Jul  4 18:05 ./node_modules/@babel/compat-data/node_modules/.bin/semver -> ../semver/bin/semver
lrwxrwxrwx 1   20 Jul  4 18:05 ./node_modules/@babel/core/node_modules/.bin/semver -> ../semver/bin/semver

lrwxrwxrwx 1   22 Jul  4 18:05 ./node_modules/caniuse-api/node_modules/.bin/browserslist -> ../browserslist/cli.js
lrwxrwxrwx 1   23 Jul  4 18:05 ./node_modules/core-js-compat/node_modules/.bin/semver -> ../semver/bin/semver.js

lrwxrwxrwx 1   20 Jul  4 18:08 ./tools/i18n-uitext/node_modules/.bin/semver -> ../semver/bin/semver
lrwxrwxrwx 1   21 Jul  4 18:08 ./tools/i18n-uitext/node_modules/.bin/uuid -> ../uuid/dist/bin/uuid
lrwxrwxrwx 1   18 Jul  4 18:08 ./tools/i18n-uitext/node_modules/.bin/which -> ../which/bin/which
lrwxrwxrwx 1   20 Jul  4 18:08 ./tools/i18n-uitext/node_modules/fstream/node_modules/.bin/mkdirp -> ../mkdirp/bin/cmd.js
lrwxrwxrwx 1   16 Jul  4 18:08 ./tools/i18n-uitext/node_modules/fstream/node_modules/.bin/rimraf -> ../rimraf/bin.js

2

Answers


  1. People don’t use it very much but the legacy utility cpio works very well with input from find as long as the names aren’t too strange (eg. shouldn’t contain newlines):

    (cd src && find -type l | cpio -o) | (cd dest && cpio -d -i)
    

    Some implementations (eg. busybox) require archive format to be specified:

    (cd src && find -type l | cpio -o -H newc) | (cd dest && cpio -d -i)
    
    Login or Signup to reply.
  2. Assuming you have cp from GNU core utils, try this command:

    cd /path/to/source && find -type l -exec cp -P --parents {} /path/to/destination ;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search