Traverse subdirectores and symlink certain files (but not all)

143 views Asked by At

I have following directory structure that I pull from github;

dotfiles/bash
     .bashrc
     .bash_profile
     .some_other
     env

dotfiles/tmux/
     .tmux.conf
dotfiles/???/
     .whatever

bash/In my bashcode (below) I'd like to (in my script symlink.sh) to iterate trough the subfolders and symlink certain, not all, dotfiles to my ~home dir. How can I do that dynamically? Since I don't know how many subfolders there are, and what files in those subfolders that I wan't to symlink this has to be done dynamically.

My code below

!/bin/bash
############################
# .make.sh
# This script creates symlinks from the home directory to any desired dotfiles in ~/dotfiles
############################
set -x
trap read debug
########## Variables


dir=~/dotfiles                   # dotfiles directory
olddir=~/dotfiles_old             # old dotfiles backup directory
files=".bashrc .bash_profile env .tmux.conf"    # list of files/folders to symlink in homedir

##########

# create dotfiles_old in homedir
echo "Creating $olddir for backup of any existing dotfiles in ~"
mkdir -p $olddir
echo "...done"

# change to the dotfiles directory
echo "Changing to the $dir directory"
cd $dir
echo "...done"
paus -p ;clear
# move any existing dotfiles in homedir to dotfiles_old directory, then create symlinks 
for file in $dir/*/*; do
  echo $dir;ls -a
        if [[ -f $dir/${files##*/} && ! -L $dir/${files##*/} ]]; then
                $olddir=$(mktemp -d olddotfiles.XXXXXX)
                #mv "~/${files##*/}" "$backupdir"
                ls -a
        ln -s "$files" ~/${files##*/}
      fi
done
1

There are 1 answers

0
dstronczak On BEST ANSWER

Just use the find command - change this:

for file in $dir/*/*; do

To this:

for file in $(find $dir); do