Having to cd..
over and over again in sub directories can be a tedious and annoying thing to do. Although it may be cool when you can quickly navigate through multiple directories it can be really annoying if you are trying to be efficient in your terminal.
Every time you open up a new session if your terminal there are custom-loaded preferences that are loaded into that session, whether that be for programs you have or for your bash environment. For example, every time I open up Vim
in my terminal I have the :set number
property to always be on, so I can see what line I am on or want to go to, for example:
Creating Your Own Custom Bash Command File
-
So in order to make your own custom commands you should make your own
Custom Bash Commands File
and you can call this whatever you want. As long as it is memorable, sotomato_soup.sh
would not be a good file name. π- Also, since you donβt want to see this file every time you
ls
in your system. You should preface the name of your file with a.
so that it is a hidden file in your system. If you wanted to view the hidden files in the current directory you are in you can use thels
command along with the-a
or the--all
flag, which just means list all the files in the current working directory.
- Also, since you donβt want to see this file every time you
Once you created your own file with the
touch
command such astouch .custom_bash.sh
you can useVim
orNano
to edit the file and make changes to it. (UseVim
its cooler π)Now that you are inside the file you created, use this code in order to
cd..
back multiple directories.
#!/bin/bash
function cd_up(){
cd $(printf "%0.s../" $(seq 1 $1));
}
alias 'cd..'='cd_up'
Place that code anywhere inside your custom file and remember to save before you exit.
After you have saved we need to test out the command to make sure it works.
Use the source
command along with the custom bash file you made so, souce ~/.custom_bashcommands.sh
Then cd
into some subdirectories and try cd.. 2
to see if it works or not.
Although, using this method you would need to run the source
command every time you open up a new terminal instance, to avoid this we can link our custom bash file into our bash profile so that every time we open up a new terminal our custom commands are preloaded.
To do this open your .bash_profile
file and place the preceding command anywhere in the file,
so in our case you would put souce ~./custom_bashcommands.sh
into .bash_profile
. Exit .bash_profile
and close and reopen your terminal to see changes.
Hope this was useful to you!
Top comments (1)
Nice one, maybe bash could do some wildcard thing to insert a blank between every single word command it receives, but was meant as a short command with a parameter.
Also, CDPATH is useful for avoiding cd .. all the time. Just put .. and ../.. in your CDPATH.