Here's my stab at solving this problem with Applescript. The following applescript will take selected aliases in the Finder and try and relink them to the new path replacing Backup
with External
in the POSIX path.
Hopefully it's straightforward. You could probably make it recursive to search for aliases in selected folders, but that's more work than I care to do -- and then there's the problem of dealing with aliases to folders. Things could get messy. ;-)
Hope it helps.
tell application "Finder"
set these_items to the selection
end tell
repeat with i from 1 to the count of these_items
set this_item to (item i of these_items) as alias
set this_info to info for this_item
if class of this_item is alias then
tell application "Finder"
set original_file to original item of this_item
set this_alias_file_name to displayed name of this_item
set container_folder to container of this_item
set the_path to the POSIX path of (original_file as alias)
set new_path to my replaceText("/Backup/", "/External/", the_path)
move this_item to trash
try
make new alias file at container_folder to (POSIX file new_path) with properties {name:this_alias_file_name}
on error errMsg number errorNumber
if errorNumber is -10000 then -- new original file not found, try relinking to old
try
make new alias file at container_folder to (POSIX file the_path) with properties {name:this_alias_file_name}
on error errMsg number errorNumber
if errorNumber is -10000 then -- old original not found. link's dead Jim
display dialog "The original file for alias " & this_alias_file_name & " was not found."
else
display dialog "An unknown error occurred: " & errorNumber as text
end if
end try
else
display dialog "An unknown error occurred: " & errorNumber as text
end if
end try
end tell
end if
end repeat
on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
All you need to do is type alias
at the prompt and any active aliases will be listed.
Aliases are usually loaded at initialization of your shell so look in .bash_profile
or .bashrc
in your home directory.
unalias
will only work for your current session. Unless you find where it is defined and loaded, it will be loaded again when you start a new Terminal session.
~/.bashrc
gets run for both login and non-login shells, ~/.bash_profile
only gets run for login shells.
See login shell vs non-login shell
As per comment from Chris Page:
You should put most of your customizations (including aliases) in ~/.bashrc
and have ~/.bash_profile
run ~/.bashrc
, so they apply to both login (~/.bash_profile)
and non-login (~/.bashrc)
shells. Also, decide which of these should be "primary" and if the profile is your choice, tack on the rc file at the end. If the rc file is primary, source that at the beginning of your profile
These lines should be in the file ~/.bash_profile
:
if [ -f "$HOME/.bashrc" ] ; then
source $HOME/.bashrc
fi
This will include ~/.bashrc
for login shells and in the order you wish if one file depends upon the other based on what you are setting.
Best Answer
No - aliases are a combination of a sym link pointing to a place as well as a unique file ID to track that file if it moves so the alias won't be broken if the file still resides on the original volume. (the file ID changes across volumes and an alias will only remain valid if the relative path is correct if and when the file ID half is broken by a deletion).
So - for this to work, the system actively tries to reconnect the alias after a delete of the original file by design so automatically deleting an alias would ruin that chance to reconnect.
Even though your question makes great logical sense, it amounts to a re-design of what an alias is engineered to do.
A hard link is what you want to use if you want it to go away when the file is deleted finally.