0

I have a git repo I am working with that has files and folders that need to be symlink to files and folders inside another folder. All of the symlinks are refrenced as text in the second projects folder. I am just wanting to know how to go through and have all the symlinks automatically setup.

Example Structure

project1/

---- myfile.rb

---- myfolder/

project2/

---- myfile.rb

---- myfolder


and in the file project2/myfile.rb it only includes the following text on line 1.

../project1/myfile.rb

and project2/myfolder just includes the text

../project1/myfolder

This is repeated throughout the file structure. Another example would be

project2/app.rb has the text

../project2/app.rb

I am on Ubuntu and thinking there is a way to go through these in an automated way and set the symlinks rather than manually finding each one.

1

Not that git can version symlinks, so consider versioning the symlinks rather than such files. Also, you might want to consider git submodules so there is an actual relation between the two repositories.

If I got your question/setup right, this might do the trick. It searches for files (recursively) in folder ./project2 with line count of one and replaces each of those files with a link to the file named in there, given that the target exists.

#!/bin/bash

for file in $(find project2/ -type f )
do
  linecount=$(wc -l $file | awk '{print $1}')
  if [ $linecount -eq 1 ]; then
    target=$(cat $file)
    relTarget=$(dirname $file)/$target
    if [ -f $relTarget ] || [ -d $relTarget ]; then
      rm $file
      ln -s $target $file
    fi
  fi
done
  • Well that's a bit less of a hack than what I was posting. But you don't support filenames that contain the newline character xD – FSMaxB Jan 23 '15 at 19:01
0

Run the following from inside your project2 directory:

for file in *; do
    link="$(cat "$file")"
    rm "$file"
    ln -s "$link" "$file"
done

This isn't recursive, so it doesn't look into subdirectories.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.