The command line is great for doing all sorts of things, but navigating to the directory you want to work in using ‘cd’ and remembering the full path address can be time consuming and annoying.
This post shows you how to write a simple file which will allow you to use shortcuts on the command line to navigate to directories.
On your Mac open a new terminal window. The starting directory of your terminal window should be your home folder, if not navigate there. In the terminal type:
This will show all files including the hidden ones.
Look for a .bash_profile file. If it doesn’t exist, create it in the terminal using the command below. If it is already there then skip to the next point.
Now to open the file with TextEdit type:
open -a TextEdit.app .bash_profile
In the .bash_profile file create your shortcut(s) in the format:
alias yourShortcut =’pathToYourDirectory’
alias nodeEx='cd /Applications/XAMPP/xamppfiles/htdocs/nodejsExe'
Next refresh the bash profile so it can be used:
Now type your shortcut command in the terminal to quickly navigate to the folder you specified. For my example I just need to type nodeEx to move the terminal to my directory.
Note that the shortcut names are case sensitive so neither nodeex or NODEEX will work for the above example.
To get back to the home directory from any location type:
In windows you can write a batch script (.bat) that will sit in the root of your hard drive to provide shortcuts to folders. There are different syntaxes you can use to achieve this, but the principle remains the same. Once it is set up you will be able to type the ‘go music’ from the command prompt to be taken to your music directory.
Open a text editor and paste in the following code. Change the labels (like :NU) to match the shortcuts you wish to type and the ‘cd’ paths to match the locations of your commonly used directories.
:NU cd F:\NU_Creative
:music cd "C:\Documents and Settings\[My ID]\My Documents\My Music"
:downloads cd C:\shared\downloads
:logs cd C:\[project path]\logs
Save the file as ‘go.bat’ in the root of your hard drive such as ‘C:’ or’F:’.
You may not have permission to save this in the root of the hard disk windows is on and may be prompted to save in your user directory instead. This is fine, but you will need to make sure you are in your user directory before using the shortcut file. To quickly get to your user directory use ‘cd \’ for example ‘cd \users/paultrotter’.
Once that is saved in the command prompt navigate to the directory the .bat is saved and type:
This will move you in to the music folder
To jump to your downloads folder
How the script works
‘@echo off’ turns off the command echoing feature that is on by default. Without this line each command is printed to the terminal when the bat file is run.
‘GOTO %1’ is used to jump to the line in the script where the label name such as ‘:pictures’ matches the argument that was typed along with the bat file name eg. ‘go pictures’. 1% is used as a placeholder for the first argument.
The ‘cd’ command is used to change to the required directory.
‘GOTO :END’ is required after changing directory to jump to the end of the script. This prevents subsequent commands in the script from being executed.
Windows command line navigation tips
When you have used your batch file to change directory once, you will need to navigate back to the location of you .bat file before using it again.
To quickly get back to the root of your current drive type:
You can change drive by typing the drive letter:
If you have saved your batch file in your user directory then use:
If you want to navigate to a folder on a different hard driver just specify the change of drive before changing directory in your bat file. For example: