2

I'm trying to hard link files in one directory hierarchy to another directory. Code looks like this:

# Directories first, works fine, takes sub-second
find src_dir/sub1 src_dir/sub2 -type d -exec bash -c 'cd "$0" && mkdir -p "$@"' dest_dir {} +
# Files next, works fine, takes several seconds
find src_dir/sub1 src_dir/sub2 -type f -exec ln -f {} dest_dir/{} \;

The hard linking is very slow because of sequential calls to ln. I can't think of a form that works though, and can't find much about copy-as-hardlinks in cp and rsync manpages. Leaning towards writing a C program for the specific problem...

Before jumping down that rabbit hole, are there any suggestions how to make it faster using UNIX or otherwise renowned 3rd party tools?

3

GNU cp supports hard-linking instead of copying files:

cp -lR src_dir dest_dir

If dest_dir already exists, this will copy src_dir into dest_dir, you’ll end up with dest_dir/src_dir and copies of all the directories in src_dir.

If you only want a subset of src_dir, you can copy sub-directories explicitly:

mkdir -p dest_dir/src_dir
cp -lR src_dir/sub1 dest_dir/src_dir/sub1
cp -lR src_dir/sub2 dest_dir/src_dir/sub2
8
  • My src_dir is really many directories with explicit sub-directories that I want to preserve. Will update question since it would matter in this case.
    – Andreas
    Mar 18 '20 at 9:24
  • @Andreas the -R in option in this answer actually means "copy directories recursively", so that should already do the job ...
    – AdminBee
    Mar 18 '20 at 9:25
  • @AdminBee Problem is cp -R src_dir/sub1 dest_dir would copy sub1 directory directly into dest_dir. I want the result to be dest_dir/src_dir/sub1.
    – Andreas
    Mar 18 '20 at 9:27
  • I understand that, but I think that if you actually call cp -R src_dir dest_dir then the entire src_dir including its sub-directories should be copied into dest_dir. Perhaps you can give it a try ...
    – AdminBee
    Mar 18 '20 at 9:28
  • @AdminBee The reason to point out sub1 and sub2 is because I don't want to copy sub3. Then again that's because of a performance issue which cp -R might just overcome... Good talk.
    – Andreas
    Mar 18 '20 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.