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


---- myfile.rb

---- myfolder/


---- myfile.rb

---- myfolder

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


and project2/myfolder just includes the text


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

project2/app.rb has the text


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.


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.


for file in $(find project2/ -type f )
  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
  • 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

Run the following from inside your project2 directory:

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

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.