Pergunta

I'm trying to modify thoughtbot's bash script to symlink dotfiles into the home directory, but I want to store my files in a subdirectory.

The original from the thoughtbot script is:

#!/bin/sh

for name in *; do
  target="$HOME/.$name"
  if [ -e "$target" ]; then
    if [ ! -L "$target" ]; then
      echo "WARNING: $target exists but is not a symlink."
    fi
  else
    echo "Creating $target"
    ln -s "$PWD/$name" "$target"
  fi
done

My dotfiles are in a subdirectory called files. I tried changing the for loop to:

for name in files/*, for name in ./files/*, for name in 'files/*', etc, but none of that worked.

After a bit of research, I see that you can loop through files in subdirectories using find like so:

find ./files -type f -exec "do stuff here"  \;

And I see that I can get reference to each file with '{}', but I don't understand how I can operate on the file and make a symlink.

I tried:

find ./files -type f -exec "ln -s '{}' $HOME/'{}'"  \;

but that doesn't work because '{}' is the relative path of the file from the parent directory, not just the name of the file.

What's the correct way to do this?

For reference, this is what my directory structure looks like:

https://github.com/mehulkar/dotfiles

Foi útil?

Solução

Your original script isn't working for dotfiles because you need to say:

shopt -s dotglob

Saying

for file in *

wouldn't match filenames starting with dot by default.

Outras dicas

... but that doesn't work because '{}' is the relative path of the file from the parent directory, not just the name of the file.

Try

find `pwd`/files -type f -exec "ln -s '{}' $HOME/'{}'"  \;

or

find $(pwd)/files -type f -exec "ln -s '{}' $HOME/'{}'"  \;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top