Reputation: 89
I want to set an alias to switch from two WordPress instances on the CLI. Each of them have the same paths except for the names of their respective sites e.g:
srv/deployment/work/sitename1/wp-content/uploads/2018/
srv/deployment/work/sitename2/wp-content/uploads/2018/
How do I create an alias that takes the "pwd" of the current location and cd s to exactly the same location on the other site?
Upvotes: 0
Views: 236
Reputation: 26
How about a bash function instead of an alias, gives you a little more freedom.
Save this bash function to a file like switchsite.sh
. Modify the variables to your needs. Then load it into your bash with:
source switchsite.sh
If you are in /srv/deployment/work/sitename1/wp-content/uploads/2018
, do
switchsite sitename2
and you will be in /srv/deployment/work/sitename2/wp-content/uploads/2018
.
switchsite() {
# modify this to reflect where your sites are located, no trailing slash
where_my_sites_are=/srv/deployment/work
# modify this so it includes all characters that can be in a site name
pattern_matching_sitenames=[a-z0-9_\-]
# this is the first argument when the function is called
newsite=$1
# this replaces the site name in the current working directory
newdir=$(pwd | sed -n -e "s@\($where_my_sites_are\)/\($pattern_matching_sitenames\+\)/\(.*\)@\1/$newsite/\3@p")
cd $newdir
}
How it works: The sed expression splits the output of pwd
into three parts: what is before the current site name, the current site name, and what comes after. Then sed puts it back together with the new site name. Just make sure the pattern can match all characters that could be in your site name. Research character classes for details.
Upvotes: 1
Reputation: 9908
Add the below lines into ~/.bash_aliases
export sitename1=srv/deployment/work/sitename1/wp-content/uploads/2018/
export sitename2=srv/deployment/work/sitename2/wp-content/uploads/2018/
After that
source ~/.bash_aliases
Then you can simply type sitename1
and sitename2
from anywhere to switch to respective directories
Upvotes: 0