Skip to content

Instantly share code, notes, and snippets.

@porg
Last active June 1, 2022 23:09
Show Gist options
  • Save porg/5b389850a1dca4d04e5706b0500f795d to your computer and use it in GitHub Desktop.
Save porg/5b389850a1dca4d04e5706b0500f795d to your computer and use it in GitHub Desktop.
link2file.sh - Replaces macOS alias or symlink with the file it points to
#!/bin/sh
# link2file /path/to/item1 /path/to/item2 …
#
# - Checks per each item that is is a macOS alias or a symlink and its target is a regular file
# - Replaces each alias or symlink with the file it points to
# - Names the newly created file with the filename of the alias/symlink it replaces
# - and if that alias has no extension, uses the extension of the file it points to
# - and if that file has no extension either, the newly created file has none too.
# - Is fast and space-efficient on APFS (copy-on-write).
# - On HFS+ a real copy takes place, may take some time.
#
# v1.0 2022-06-02
#
# Dependencies: alisma — https://eclecticlight.co/taccy-signet-precize-alifix-utiutility-alisma/
for i in "$@"
do
# Print item
echo "ℹ️ $i"
# Setup
file=$(basename "$i")
fileName=${file%.*}
fileExt=$(echo "$file" |awk -F . '{print $NF}')
if [[ "$fileName" == "$fileExt" ]] ; then fileExt="" ; fi
if [[ -L "$i" ]] ; then
symlinkOriginal=$(readlink "$i") # /path/to/symlinkOrginal or -1 if it is no symlink.
aliasOriginal=-1
else
symlinkOriginal=-1
aliasOriginal=$(alisma -p "$i") # /path/to/aliasOrginal or -1 if it is no alias.
fi
# Actions
if [[ ! -e $i ]] ; then
echo "❌ Invalid filepath!"
echo
elif [[ $aliasOriginal == -1 && $symlinkOriginal == -1 ]] ; then
echo "❌ Not a symlink or alias!"
echo
elif [[ $aliasOriginal != -1 && ! -f "$aliasOriginal" ]] ; then
echo "👉 $aliasOriginal"
echo "❌ Alias destination is not a file!"
echo
elif [[ $symlinkOriginal != -1 && ! -f "$symlinkOriginal" ]] ; then
echo "👉 $symlinkOriginal"
echo "❌ Symlink destination is not a file!"
echo
elif [[ -f "$aliasOriginal" ]] ; then
if [[ $fileExt == "" ]] ; then # If the alias does not have a file extension
orig=$(basename "$aliasOriginal")
origName=${orig%.*}
origExt=$(echo "$orig" |awk -F . '{print $NF}')
if [[ "$origExt" == "$origName" ]] ; then origExt="" ; fi
if [[ "$origExt" != "" ]] ; then # … but the file the alias points to has an extension
fileFull="$fileName.$origExt" # …… then the original file replacing the alias gets the alias' filename and the original's extension.
else
fileFull="$fileName" # Or else the file gets the filename from the alias without an extension just like the file the alias pointed to.
fi
fi
filePath="$(dirname "$i")/$fileFull"
rm -f "$i"
cp "$aliasOriginal" "$filePath"
echo "👉 $aliasOriginal"
echo "✅ Converted alias to file:"
echo " $filePath"
echo
elif [[ -f "$symlinkOriginal" ]] ; then
if [[ $fileExt == "" ]] ; then # If the alias does not have a file extension
orig=$(basename "$symlinkOriginal")
origName=${orig%.*}
origExt=$(echo "$orig" |awk -F . '{print $NF}')
if [[ "$origExt" == "$origName" ]] ; then origExt="" ; fi
if [[ "$origExt" != "" ]] ; then # … but the file the alias points to has an extension
fileFull="$fileName.$origExt" # …… then the original file replacing the alias gets the alias' filename and the original's extension.
else
fileFull="$fileName" # Or else the file gets the filename from the alias without an extension just like the file the alias pointed to.
fi
fi
filePath="$(dirname "$i")/$fileFull"
rm -f "$i"
cp "$symlinkOriginal" "$filePath"
echo "👉 $symlinkOriginal"
echo "✅ Converted symlink to file:"
echo " $filePath"
echo
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment