Last active
November 22, 2024 14:39
-
-
Save rubo77/7a9a83695a28412abbcd to your computer and use it in GitHub Desktop.
convert hard-links to symbolic links
This file contains hidden or 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 | |
# | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@dannysauer: about line 68 and 81, like this? https://gist.github.com/rubo77/7a9a83695a28412abbcd/revisions
And what do you mean with
"echo -- <<ENDOFHELP"
?