I'm writing a script to make symbolic links for my dotfiles, and I'm trying to use find to do this.

I am trying to subtract the prefix from the folder containing my dotfiles so that I only have the relative path so that I can link it into my home directory.

I read that with variables you can do ${string#prefix} if you have the string in a variable, but if I don't, how do I do this?

find "$DIR" -regextype posix-extended ! -regex ".*(\.sh|\.git).*" -exec echo ${"{}"#"$DIR"}\;

where $DIR is the full path of the dotfiles directory

  • The regex you're using is dubious -- it will also exclude files like foo.github-stuff.txt. But anyways, the ${str#px} will be expanded by the calling shell before the find command is run, so there's no way to fix that approach. Better try something like find "$SRCDIR" ... your predicates ... -exec echo ln -rst "$DESTDIR" {} + (read about the -r and -t options of GNU ln in its manpage, and remove the echo if the dry run's OK). – mosvy Oct 19 at 9:56
  • Thanks for pointing me in the right direction. I ended up not cd-ing to home before running find and just doing from the source dir ln -rs {} ~/{} – Albert Geantă Oct 19 at 10:29
  • At least with GNU find, you can use -printf '%P' to output the file's name with the name of the starting-point under which it was found removed – steeldriver Oct 19 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.