Symbolic link source item can't be found
You want to create a symbolic link called Test2
in each directory in the current directory, and each created link should point to location/to/directory
.
for dir in */; do ln -s 'location/to/directory' "$dir/Test2"done
The slash after *
ensures that we will only match directories in the current directory, or links to directories in the current directory.
If you're only interested in real directories an not symbolically linked directories, you may use
find . -type d -mindepth 1 -maxdepth 1 \ -exec ln -s 'location/to/directory' {}/Test2 ';'
Note that the link destination is relative to the location of the link, so if a directory does not contain location/to/directory
, the link will be "dead".
You may solve this be specifying an absolute path for the links.
What are you attempting to do?
Think of a link as a cp
command. Maybe that will help:
# Copies the 'svnadmin' command from /opt/svn/bin to /usr/local/bin$ cp /opt/svn/bin/svnadmin /usr/local/bin# Links the 'svnadmin' command from /opt/svn/bin to /usr/local/bin$ ln -s /opt/svn/bin/svnadmin /usr/local/bin
Note that the ln
and cp
command have the same order of files.
In your command, you're linking whatever location/to/directory/
to $D/test2
over and over again.
Also, -maxdepth 0
won't be in the first level of the directory.
I use ln
when I install new software, and the binary commands are in some other directory. Instead of building on $PATH
to include all of these extra directories, I symbolically link them to /usr/local/bin
:
$ cd /usr/share/apache-ant/bin$ for file in *> do> [[ -f $file ]] || continue> ln -s $PWD/$file /usr/local/bin/$file> done
Note that the link simply copies the entire reference for the first file to the link. I want to make sure that this link works everywhere, so I prefix that $PWD
in front of it. This way, my links look like this:
$ ls -l antlrwxr-xr-x 1 root wheel 29 Sep 3 2014 ant -> /usr/share/apache-ant/bin/ant