How do I symlink all files from one directory to another in bash?

BashSymlinkLn

Bash Problem Overview


I want to link ( ln -s ) all files that are in /mnt/usr/lib/ into /usr/lib/

There are lots of files, how can it be done quickly? :)

Bash Solutions


Solution 1 - Bash

ln -s /mnt/usr/lib/* /usr/lib/

I guess, this belongs to superuser, though.

Solution 2 - Bash

GNU cp has an option to create symlinks instead of copying.

cp -rs /mnt/usr/lib /usr/

Note this is a GNU extension not found in POSIX cp.

Solution 3 - Bash

ln -s /mnt/usr/lib/* /usr/lib/

Solution 4 - Bash

The posted solutions will not link any hidden files. To include them, try this:

cd /usr/lib
find /mnt/usr/lib -maxdepth 1 -print "%P\n" | while read file; do ln -s "/mnt/usr/lib/$file" "$file"; done

If you should happen to want to recursively create the directories and only link files (so that if you create a file within a directory, it really is in /usr/lib not /mnt/usr/lib), you could do this:

cd /usr/lib
find /mnt/usr/lib -mindepth 1 -depth -type d -printf "%P\n" | while read dir; do mkdir -p "$dir"; done
find /mnt/usr/lib -type f -printf "%P\n" | while read file; do ln -s "/mnt/usr/lib/$file" "$file"; done

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKooTView Question on Stackoverflow
Solution 1 - Bashuser156676View Answer on Stackoverflow
Solution 2 - BashcafView Answer on Stackoverflow
Solution 3 - BashMichael Krelin - hackerView Answer on Stackoverflow
Solution 4 - BashCascabelView Answer on Stackoverflow