Switching to an aliased directory - macos

Just wanted to know if it is possible to switch to an aliased directory using shell script.
To switch to a directory there is cd command. But i have aliased the directory and wanted to know as how to switch to the aliased directory.
Could someone please help me?

An alias is meant to be a shortcut for a command.
You can't cd to an aliased directory.
But if you have a variable referencing that directory, you can. e.g.
dev="/path/to/my/dev"
cd $dev
Or set up an alias to cd to the directory:
alias dev='cd /path/to/my/dev'

Related

Change directory in bash script in windows

How can I change my working directory in bash script in windows. I have
~dp0 = C:\test\docker\windows and I want to change my directory to C:\test\build
So it means 2 levels up and then int o build folder
Thanks
Since C:\ is mounted by default into /mnt/c this will work.
Create a .bashrc in your home path by following command:
echo "BUILDDIR=/mnt/c/test/build" >> ~/.bashrc;source ~/.bashrc
cd $BUILDDIR
# Do your work below for example ./configure.
./configure
On my system in Git bash the C:\ root is just /c/ and other dirs from there are whatever they are, so it would be cd /c/test/build/.
You could also still say cd ../../build/.
Good luck.
To change the directory using a bash script is just like you would using normal bash.
cd "C:/test/build"
echo "You're now in the folder, do what you will."
Save the file as .sh and it can be used as such.
Note, when navigation your folders using a bash script remember the directory you'll be starting from is always the home directory.

Home directory navigation in bash script

I need a simple bash script function for .bashrc file to navigate to my home directory.
We use hg for all our work purpose in the directory and contains many streams.
/home/<username>/RelStream1/
/home/<username>/RelStream2/
/home/<username>/RelStream3/
/home/<username>/RelStream4/
For namesake i call this /home//RelStream1/ path as repo home.
Now for work purpose, i need to navigate to sub folders from my repo home path.
/home/<username>/RelStream1/pkgdir/appsdir/moduledir/submoduledir/....
After finishing work, i need to come back to home repo directory for compilation.
I have added alias like this, but still its painful to remember the path back.
alias ..="cd .."
alias ..2="cd ../.."
alias ..3="cd ../../.."
alias ..4="cd ../../../.."
alias ..5="cd ../../../../.."
Please note many repo home directories would be used at any point of time and hence setting aliases as alias rhome='cd /home/<username>/RelStreamX/' will have little use.
I just need a simple bash function to get back to repo home directory (current repo).
It has to get the current path and navigate my directory back to home repo directory
Ex1:
From: /home/<username>/RelStream1/pkgdir/appsdir/moduledir/submoduledir/
To: /home/<username>/RelStream1/
Ex2:
From: /home/<username>/RelStream3/pkgdir/appsdir/moduledir/submoduledir/micromodule/
To: /home/<username>/RelStream3/
Ex3:
From: /home/<username>/RelStream4/pkgdir/appsdir/
To: /home/<username>/RelStream4/
I have tried some methods in bash(beginner), but does not give expected results.
code:
repohome_func()
{
cwd=$(pwd)
echo "CurrPath:${cwd}"
sep="/"
//My plan is to search the cur path with "/" char up to 4 times,
//cut the front string and then execute that cmd.
}alias rhome=repohome_func
This should work for you:
function repohome() {
local repodir=${PWD/$HOME\/}
cd "$HOME/${repodir%%/*}"
}
This should make:
alias rhome="cd $(pwd | sed 's~/[^/]*~~4g')"
It gets the current path with pwd and replaces it to what is needed. As this is within $() then we cd there.
You need to remove all current path but the first three blocks. With this sed, we do that.
See an example with a file:
$ cat a
/home/myuser/myrepo1/asld/as/asdf/asdf/asdf
/home/myuser/myrepo2/asdf
/home/myuser/myrepo3/asld/as/asdf/f/asdf
/home/myuser/myrepo4/asld/as/asdf/
$ sed 's~/[^/]*~~4g' a
/home/myuser/myrepo1
/home/myuser/myrepo2
/home/myuser/myrepo3
/home/myuser/myrepo4
From what was commented below, it seems like that the solution was not working as an alias, but it does as a function. The hypothesis is that there must be some alias in either cd, pwd or sed that makes them work differently.
As aliases are bypassed by functions, using it that way mades it. Otherwise, it could also work by using \ before any command, so the command alias gets bypassed as well.
alias rhome="\cd $(\pwd | \sed 's~/[^/]*~~4g')"
More info:
Temporarily disabling an alias / quoting a word with a backslash
\curl … | bash … what's the slash for?.
If you add your home directory to the front of CDPATH, for example,
CDPATH=/home/<username>:.
then any relative path used with cd will be looked up first in your home directory before the current working directory. Then
cd Relstream1
will take you to /home/<username>/Relstream1 before ./Relstream1.
You might want to try the builtin commands pushd and popd. If you use pushd <dir> instead of cd <dir>, you can then use popd to return to the previous working directory.

How can I cd to an alias directory in the Mac OSX terminal

Is there a way to get into an alias directory from shell with the command "cd" ? It always returns that "htdocs" isn't a directory.
Edit: I made the shortcut with the OS GUI -> rightclicked the htdocs directory and chose "Alias..." (i'm using a german OS if it's not alias maybe it's called shortcut in english?) then i moved it to my home directory (because my terminal starts from there when i open it).
All i want is to open my terminal and type "cd htdocs" so that i can work from there.
you can make symbolic link to it.
ln -s EXISTING_PATH LINK_NAME
e.g.
ln -s ~/Documents/books ~/Desktop/
Reference
Enter into a directory through an alias in Mac OS X terminal
All i want is to open my terminal and type cd htdocs so that i can work from there.
The easier approach is probably to ignore the links and add the parent directory of your htdocs directory to the CDPATH environment variable. bash(1) will check the contents of the CDPATH environment variable when you type cd foo to find the foo directory in one of the directories listed. This will work no matter what your current working directory is, and it'll be easier than setting symbolic links.
If the path to your htdocs is located /srv/www/htdocs/, then you could use CDPATH=/srv/www. Then, cd foo would first look for /srv/www/foo/ and change to it if it exists; if not, then it would look for foo in the current working directory and change to it if it exists. (This might get confusing if you have multiple htdocs directories on your system; in that case, CDPATH=.:/srv/www would let you change into a child directory easily but still use the /srv/www/htdocs/ version if no ./htdocs directory is present.)
You can add the CDPATH=/srv/www line to your ~/.bashrc file so it works every time you start a terminal.
I personally use this to quickly work in the directory which is present deep inside one of my Volumes in my Mac.
Open your ~/.bash_profile, create an alias to the directory by adding this:
alias cdh="cd /Volumes/Haiku/haiku/src/apps/superprefs"
Save it, restart your terminal. Now on typing cdh in your terminal should change the working directory to the one mentioned as the alias.
I am not sure how OSX exposes Alias links but since you are using bash you can just create a variable in your .bashrc file.
On its own line put:
htdocs=YourDirectoryPath/
Once you have restarted bash you can just type cd $htdocs
There is a old hint on macworld to do this in a way that is integrated with BASH: Enable 'cd' into directory aliases from the Terminal
Plus, here is an answer that uses this solution on superuser.
You may be able to use osascript to do this -- this command seems to work:
cd "`osascript -e "on run aFile" -e "set aFile to POSIX file aFile as alias" -e "tell application "\""Finder"\"" to return POSIX path of ( ( original item of aFile ) as text ) " -e "end run" path_to_my_Finder_alias 2>/dev/null`"
Basically this command is running an AppleScript that finds the destination path of the argument (path_to_my_Finder_alias) in a subshell, then wraps it in double quotes, and changes the directory to it.
Maybe someone with a little more bash expertise can turn it into a bash alias or function.
try:
alias cdgo=`echo cd /root/go/`
cdgo will run, then get command "cd /root/go/" and enter, and it will change your directory in current terminal process
It works on my centos, no test with osx

How to make a shell script global?

I am on Mac's OS 10.6, and I am trying to learn a thing or two about shell scripting. I understand how to save a shell script and make it executable, but I am wondering what I can do or where I can save the file to make it global (that is, accessible no matter what folder I am in).
For example, if I save a .sh file in the /Users/username/ directory and make it executable, I can only execute that script in that specific directory. If I navigate to /Users/username/Downloads, for example, I can't execute the script.
Also, any suggestions of resources for learning more about shell scripting would be helpful. Thanks
/usr/local/bin would be the most appropriate location. Mac OS X has it in the PATH by default
There are two ways to do it -
Put your script in usr/local/bin and make sure it is executable(chmod +x my_script)(This is already set in the path, you can check by doing an echo $PATH)
Create a folder in your home directory called bin. (For your personal scripts)
cd ~ (Takes you to your home directory)
mkdir bin (create a bin folder)
vim .bash_profile (to set path environment variable)
export PATH=~/bin:$PATH (Press i then add this line and then do esc and type :wq)
Now you can just type the name of your script and run it from anywhere you want.
** NOTE: If you want to run the script with a shortened command rather than typing your entire filename, add the following to your .bash_profile:
alias myscript='my_script.sh'
Then you can run the script by simply typing myscript. (you can sub in whatever alias you'd like)
Traditionally, such scripts either go in ~/bin (ie: the bin directory in your home directory) or /usr/local/bin/ The former means the script will only work for you, the latter is for scripts you want anybody on the system to be able to run.
If you put it in ~/bin, you may need to add that to your PATH environment variable. /usr/local/bin should already be on the path.
In mac operating system
Open bash ~/.bashrc file.
add path of your script in your bashrc file , using
export PATH="$PATH:/Users/sher.mohammad/Office/practice/practiceShell"
Open your ~./bash_profile file and add [[ -s ~/.bashrc ]] && source ~/.bashrc
open new terminal window
Now whenever you will open your terminal your script will be loaded
This one is super easy if you are familiar with your bashrc file! This will entirely use just your .bashrc file and takes 2 seconds to accomplish.
(I use Arch Linux Manjaro so I use .bashrc located in my home directory)
The code to be placed in your .bashrc file:
# Simple bashrc method to launch anything in terminal from any directory
YOURCOMMAND () {
cd /path/to/directory/containing/your/script/ && ./YOURSCRIPT
}
As you can see, first you use the simple 'cd' command and give it the directory of the scripts location, then use '&&' so that you can make the next command executed right after, and finally open your script just as you would normally! Super easy and saved right in your .bash file! :)
Hope I've helped someone!
Sincerely,
AnonymousX
On using bash shell, write that script as function and then put it to the .bashrc or source the file which containing that function by "source file_name"
Now execute the script by function call in the shell.
Either saving it in /usr/bin (or any other directory present in PATH) or editing PATH to include the directory you saved it in will basically make it run in any directory.
from the working directory of 'script.sh'" mv [script.sh] /usr/local/bin"( not tested but seems to be the least complex way IMO.)
You should put it in the global executable directory on your machine. I think that would usually be /usr/bin on Unix-based operating systems (this would however most often require super user privileges on that machine).
You could also put it in any other directory that is in the $PATH environment variable, although it would only work for those users who have that directory in that variable.
You can find the value of $PATH by typing echo $PATH in a shell. The directories are separated by :.

Add a single Bash command

I do not have su access and I have a perl executable in ~/et directory which is called exiftool.
I need to add that executable to bash commands (so that I can type exiftool instead of ~/et/exiftool).
The problem is that ~/et contains other files that are not executable (so I cannot use export PATH=$PATH:$HOME/et). Is there any alternative?
You could use an alias:
alias exiftool=~/et/exiftool
Or you can symlink it elsewhere and add that directory to your path:
mkdir -p ~/bin
ln -s ~/et/exiftool ~/bin
PATH=$HOME/bin:$PATH
I don't understand why having files that are not executable in the directory prevents you from adding the directory to your PATH anyway?
As an alternative, though, you can use an alias.
alias exiftool=$HOME/et/exiftool
You can place this in your .bashrc to have it always available.
softlink to a folder in PATH
alias exiftool='~/et/exiftool'

Resources