How do I symlink all files from one directory to another in bash? [closed]
Asked Answered
L

4

146

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? :)

Lately answered 28/8, 2009 at 13:49 Comment(2)
How many files is "lots", and how fast you deem "fast"?Posey
This is 50:50 whether it would fit "serverfault" or "superuser", but it isn't programming, so not for stackoverflow.Skelp
L
236
ln -s /mnt/usr/lib/* /usr/lib/

I guess, this belongs to superuser, though.

Lilla answered 28/8, 2009 at 13:52 Comment(7)
This does not include hidden files, and it links whole directories. If either of these is not what you want, see my answer. Otherwise, it's the shortest way.Toon
You're right. But libraries aren't hidden usually. In any case dotfiles are involved your solution comes in more handy.Lilla
will it point to the other folder permanently?Kale
@YuPPie I guess, it just creates hardlink to every file in the directory. (this is what I wanted today, so +1 :D)Itemize
@SargeBorsch It creates symbolic links (hard links are different).Gladine
Yeah, for sure. I was too stupid at these days, now I understand this stuff better.Itemize
This is not working for me. I just get * as soft link instead of all files and folders inside libMckoy
B
102

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.

Beare answered 28/8, 2009 at 14:4 Comment(5)
Your current directory should be /usr/ to make symbolic link this wayUnderplay
@Beare If the destination is existing folder, then the lib folder is created inside in /usr/ folderHeartrending
Good answer. Can also be used to create hardlinks with -l instead of -s.Protractile
Is there an option to copy hidden files as well?Bakerman
@Bakerman sure you can use cp -rs /myconfigfolder/. /mynewconfigfolder/ So should everything included hiddenfiles symlinkedZachery
S
21
ln -s /mnt/usr/lib/* /usr/lib/
Soundless answered 28/8, 2009 at 13:53 Comment(0)
T
21

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
Toon answered 28/8, 2009 at 14:0 Comment(1)
I believe this should also work as a way to wildcard in hidden files, if you have extended globbing turned on in bash. It matches everything starting with a dot, followed by something other than nothing or another dot (i.e. it excludes ./ and ../): ln -s /mnt/usr/lib/.!(|.)* /usr/libToon

© 2022 - 2024 — McMap. All rights reserved.