ln with dotfiles? - wildcard

I have a directory with several "dot files". I would like to symlink all these files.
I tried
$ ln -s /usr/dotfiles/* /usr/test
ln: creating symbolic link `/usr/test/*' to `/usr/dotfiles/*': No such file or directory

ln -s /usr/dotfiles/.[!.]* /usr/test
Inspired from
pavium’s answer

Related

Is there a similar flag like ln -sh from Mac osX for linux to skip over any file that is already symlinked?

e.g. from the Mac man pages:
man ln
-h If the NewLinkFile (or directory) is a symbolic link, do not follow
it. This is most useful with the -f option, to replace a symlink
which can point to a directory.
I am writing a script which once a day iterates over hundreds of files and symlinks them all and then periodically through out the day checks for new files and symlinks them. Would be great to use this -h check or something similar as at the moment it is done very awkwardly.
A GNU equivalent of the BSD ln -sh is ln -sn.
But GNU's ln -sT is safer, because it will skip both symbolic links and directories.
Compare:
cd "$(mktemp -d)"
mkdir bin
for d in bin usr etc; do ln -sn "/$d" "$d"; done
# will create a bin/bin -> /bin symlink
vs.
cd "$(mktemp -d)"
mkdir bin
for d in bin usr etc; do ln -sT "/$d" "$d"; done
ln: failed to create symbolic link 'bin': File exists

SYMLINK to Directory for all files

I want to symlink all the files which start with "sun" in the dir /myTest/logs/ to /finalProject/logs/sun
i tried using ln -sd /finalProject/logs/sun /myTest/logs/*
but i get error saying target is not a dir.
can somebody help.
You can't symlink multiple files with a single command. But a little bash for loop will do what you need:
for i in /finalProject/logs/sun*
do
ln -s $i /myTest/logs/
done

symbolic links used as directory

clones is a link
nrolland at mactoasty in ~
$ la clones
lrwxr-xr-x 1 nrolland staff 11B Oct 5 16:37 clones -> Sync/clones
from a subdirectory
nrolland at mactoasty in ~/clones/monoidAsComputation/app
$ ls ../../../
list all the files in ~/Sync
whereas this lists all the files in ~
cd ../../../; ls
If I try to point a symbolic link to a location above, that fails although I can cd into relative location
nrolland at mactoasty in ~/clones/monoidAsComputation/app
$ ln -s ../../../.emacs.d/reveal.js
whereas this will, because ln expands the clones symbolic link to its definition............
nrolland at mactoasty in ~/clones/monoidAsComputation/app
$ ln -s ../../../../.emacs.d/reveal.js
Is there any way to get back some sane referential transparency, or at least the same behavior between cd and ln ?
I use zsh, on macos. I will try with other shells
Not sure about zsh, but the following will work for bash:
To get the same behaviour as ln (and any other command), you can use cd -P in bash. In your example, you should see:
# nrolland at mactoasty in ~/clones/monoidAsComputation/app
cd -P ../../..; ls
listing files in ~/Sync, same as ls ../../../ does. You can get this behaviour with simple cd ../../.., by aliasing:
alias cd='cd -P'
For more information, see this answer.

Symlink and hide dotfiles

How could I rename a bunch of dotfiles and add the leading dot in the same command? I see people writing:
ln -s vimrc .vimrc
ln -s gitconfig .gitconfig
But I would like something like this:
ln -s {vimrc,gitconfig} ~/.$1
Using for loop:
for f in vimrc gitconfig; do ln -s $f .$f ; done
If you have the filename list in a file:
for f in `cat filename_list.txt`; do mv $f .$f ; done

Unable to make symlinks effectively with target files of the same names

I have a list of dotFiles at my workarea. For example, .bashrc and .vimrc.
I want to make a symlinks from them to my Home such that their names are the same as in my workarea -folder.
My attempt in pseudo-code
ln workarea/.[a-zA-Z] ~/.*
The problem is to have a bijection from [a-zA-Z] to the files which occur in my Home.
How can you make symlinks with the target files of same name as the original files?
'man ln' says:
ln [OPTION]... TARGET... DIRECTORY (3rd form)
So you need to do something like:
$ ln -s workarea/.* ~/
The possible uses of ln to create symbolic link(s) are:
ln -s <source-file> [<target-file]>
ln -s <source-file> ... <target-dir>
When you type
ln -s workarea/.[a-zA-Z]* ~/.*
(I think you were missing a *) the shell will expand out workarea/.[a-zA-Z] and ~/.*, so (presuming that the your HOME directory contains the files .abc and .def) you would end up with
ln -s workarea/.bash_profile workarea/.bashrc ~/.abc ~/.def
which fits neither usage of ln.
To use the second usage of ln, you would use:
ln -s workarea/.[a-zA-Z]* ~/.

Resources