Created
January 26, 2016 09:13
-
-
Save T1T4N/71c9541089720b5c216f to your computer and use it in GitHub Desktop.
Create symlinks of all files and folders from the source dir to the destination dir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ $# -lt 1 ] | |
then | |
echo "Create symlinks of all files and folders from the source dir to the destination dir" | |
echo "Usage: `basename $0` source [dest]" | |
exit 1 | |
fi | |
source=$1 | |
if [ $# -eq 2 ] | |
then | |
dest=$2 | |
else | |
dest="." | |
fi | |
currentDir=`pwd` | |
if [[ ! "$source" = /* ]] # Check if it is a relative or absolute path | |
then | |
source="$currentDir/$source" | |
fi | |
if [[ ! "$dest" = /* ]] # Check if it is a relative or absolute path | |
then | |
dest="$currentDir/$dest" | |
fi | |
echo -e "Source:\t\"$source\"" | |
echo -e "Dest:\t\"$dest\"" | |
if [ ! -d "$source" ] | |
then | |
echo "Source folder $source doesn't exist" | |
exit 1 | |
fi | |
if [ ! -d "$dest" ] | |
then | |
echo "Destionation folder $dest doesn't exist. Creating" | |
mkdir "$dest" | |
if [ ! -d "$dest" ] | |
then | |
echo "Folder could not be created." | |
exit 1 | |
fi | |
fi | |
echo "" | |
ls="gls -1" # Set ls command here | |
# The sed filters out the resolution of symbolic links | |
files=`$ls -l "$source/" | sed "s/[@]* ->.*$//" | | |
awk '{ | |
x=9; | |
while(x<=NF){ | |
if(x==NF) printf "%s", $x; | |
else printf "%s ", $x; | |
x++; | |
} | |
printf "\n"; | |
}'` | |
# Internal File Separator needs to be set to newline to parse awk results | |
OLDIFS=IFS | |
IFS=$'\n' | |
for file in $files | |
do | |
# echo "$source/$file" | |
if [ -e "$source/$file" ] # Check if file really exists (invalid symbolic link, insufficient permissions) | |
then | |
if [ ! -e "$dest/$file" ] # Check if the same filename exists at the destination | |
then | |
echo "\"$source/$file\" -> \"$dest/$file\"" | |
ln -s "$source/$file" "$dest/$file" | |
else | |
echo "Skipping: \"$source/$file\". Same filename exists at dest" | |
fi | |
fi | |
done | |
# Restore the IFS to the original | |
IFS=OLDIFS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment