Skip to content

Instantly share code, notes, and snippets.

@rubo77 rubo77/ln-conv-hl
Last active Apr 24, 2019

Embed
What would you like to do?
convert hard-links to symbolic links
#!/bin/bash
#
# converts hard-links to symbolic links
#
#-------------- Author: -------------#
# by Ruben Barkow
# git: https://gist.github.com/7a9a83695a28412abbcd.git
#--- Copyright and license -----------#
#This code is covered by the GNU General Public License 3.
#default: no dry-run
DRY=false
# Execute getopt on the arguments passed to this program, identified by the special character $@
PARSED_OPTIONS=$(getopt -n "$0" -o hn --long "help,dry-run" -- "$@")
#Bad arguments, something has gone wrong with the getopt command.
if [ $? -ne 0 ]; then
exit 1
fi
# A little magic, necessary when using getopt.
eval set -- "$PARSED_OPTIONS"
# Now go through all options with a case and using shift to analyse one argument at a time.
# $1 identifies the first argument, and when we use shift we discard the first argument,
# so $2 becomes $1 and goes again through the case.
while true; do
case "$1" in
-h|--help)
echo " This script converts all hard-links into symbolic links that it finds in a
relative source directory from working directory (first argument) that are the
same as in the working directory (optional second argument, default \"./\" )
NAME:
ln-conv-hl
DESCRIPTION:
this script converts hard-links into symbolic links
GLOBAL OPTIONS:
Mandatory arguments to long options are mandatory for short options too.
-n, --dry-run
the output only shows what will be done
-h, --help
Display this help documentation
$1 (first argument)
soure directory wherein to search for another file with the same inode
$2 (second argument)
directory where to replace all hard-links in
USAGE EXAMPLES:
# dry-run for converting all hard-links inside /etc/apache2/sites-enabled into sympolic links
ln-conv-hl /etc/apache2/sites-available /etc/apache2/sites-enabled -n
DEVELOPER INFO
This project on git: https://gist.github.com/7a9a83695a28412abbcd.git
"
exit
shift;;
-n)
# dry-run
DRY=true
shift;;
--)
shift
break;;
esac
done
if [ $# -eq 0 -o $# -ge 3 ]; then
echo ERROR: this script has to be called with 2 arguments
echo
exec "$0" -h
fi
if [ $# -eq 1 ]; then
echo "No working directory defined - use `pwd` as working directory"
WORKING_DIR="."
else
WORKING_DIR=$2
fi
SOURCE_DIR=$1
echo relative source directory from working directory: $SOURCE_DIR
echo working directory: $WORKING_DIR
if [ $DRY == true ]; then
echo "dry-run: $DRY"
fi
# find all files in WORKING_DIR
cd "$WORKING_DIR"
find "." -type f -links +1 -printf "%i %p\n" | while read working_inode working_on
do
find "$SOURCE_DIR" -type f -links +1 -printf "%i %p\n" | sort -nk1 | while read inode file
do
if [[ $inode == $working_inode ]]
then
echo -n ln -vsf '"'$file'" "'$working_on'"'
if [ $DRY == true ]; then
echo " # nothing done"
else
echo " # executing..."
ln -vsf "$file" "$working_on"
fi
fi
done
done
@dannysauer

This comment has been minimized.

Copy link

dannysauer commented Dec 7, 2014

The help statement should probably use a heredoc to avoid the quoting issues, and should look more like "echo -- <<ENDOFHELP" so as to avoid potential issues with different echo implementations which might interpret, for example, the "-n" in the string.

The argument count check on line 81 should use "-ge" to catch all values outside of the accepted range.

The shift on line 68 is a no-op. :)

@rubo77

This comment has been minimized.

Copy link
Owner Author

rubo77 commented Mar 1, 2015

@dannysauer: about line 68 and 81, like this? https://gist.github.com/rubo77/7a9a83695a28412abbcd/revisions

And what do you mean with "echo -- <<ENDOFHELP"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.