In the Ubuntu terminal I find myself spending considerable time to switch to a particular directory every time the computer starts up. Is there some way I can make this process easier? Is there some hotkey or some quick directory change that I can use (like speed dial on a phone)?
eg:
cd 1:Changes to saved directory one
Also have a look at
autojump
, it builds a database with previously visited directories and then you can jump to it. So for instance you havethen if you have visited it once you can jump to it by
or even
because it also works with partial matches. If more than one directory matches you jump to the one that is most visited, if this is not the one you wanted, then repeat the command to go to the second.
However it gets better! If you also have the directory
and you do
and then TabTabTab you get (in the order of most visited)
and then you can just press the number of the directory you want!
To install you can just use
sudo apt-get install autojump
and then you need to addto your
~/.bashrc
.More information here: https://github.com/wting/autojump (also instruction on how to install this from src which gets you the most recent version)
There are two options:
If you want to be in a specific directory everyt time you open a bash terminal, edit your
~/.bashrc
file and just add the linecd Directory
, for examplecd ~/Desktop
.If you want to have several short-cuts, you can always use global variables, which you can set in your
~/.bashrc
file as followsexport a=/tmp
and then you would be able to docd $a
which would bring you to/tmp
.Remember that after editing your
.bashrc
file you have to restart the terminal or open a new one.Bash aliases are useful for creating short-cuts to commonly run commands.
In
~/.bashrc
, add a line similar to the following to create the alias:Close and open the terminal again, or run
source ~/.bashrc
.From now on, you can just run
jump1
to execute that longcd
command.See also:
Even though there are some good answers already, I thought I'd mention for completeness the old
pushd
andpopd
Bash builtins with allow you to move very quickly between directories in deep paths in your filesystem. In contrast to theautojump
bookmarks mentioned by burger.ga, the directory stack created by usingpushd
is only temporary.As the GNU manual notes,
pushd
andpopd
are Bash builtins used to construct a temporary directory stack that is aIt takes a bit of getting used to, but there are plenty of useful tutorials out there at this blog and this this site for example. There are no separate manpages for the commands, as they are included in the Bash manpage, but for quick reference you can view the GNU Bash page.
The clearest explanation is in this very useful article and is exactly the way I use
pushd
andpopd
:Firstly, use
dirs
ordirs -l
to list directories on the stack anddirs -c
to clear the stack.In a sense you bookmark the location where you want to return by entering
and then you can add more directories to the stack, so you get a dirs listing as below, which includes 3 designated folders and your
~
home folder:You actually don't need to use
popd
straightaway as that removes directories from the stack; the best thing to do is to usepushd
to rotate the stack, so that you can keep switching between the directories and order them as you want. For more on that useful stack rotation see the Bash manpage.If you have exactly the stack arrangement as the above
dirs
listing shows,pushd +3
switches you to the specific Pictures folder and places that on top of the stack (it is +3 and not +4 as you do not count your~
in the directory stack):So the prompt reads,
You can keep on doing this without removing them from the stack, although any folders you
cd
to, other than those added to the directory stack withpushd
, will alter your directory stack.Once you have the directory stack arranged in the order you want, you can use
popd
to quickly cycle through the directories and then return to the home folder; for example, fromPictures
we can return toDownloads
:and the prompt reads
In general the beauty of this is that you can set up the directory stack and the order of items within it with
pushd
and then, say you have three files to edit in each of those directories, you can return instantly to each one withpopd
while removing them from the directory stack. Then with your lastpopd
you will return to~
. It becomes particularly useful when you have particularly deep directories and can use it to set up the directory stack to quickly move between them.In addition, this question contains some tips that you might find useful when working on the command-line:
Even though there exists some command line way to do it faster, I would personnally recommand using a Nautilus extension called
nautilus-open-terminal
.First install the package
nautilus-open-terminal
and bookmarks your favorite folders with Nautilus. On Nautilus 3.6.*, you'll have to go into the gear menu and select Bookmark this Location.Then use Nautilus bookmarks to go faster in your favorite directories, and just right click on an empty space and select Open in Terminal.
Now you can have both command line and file manager quick access to your favorite directories.
anc was designed exactly for that use case.
https://github.com/tobimensch/anc
Here's an excerpt from the README.md:
Full disclosure: I'm the author of anc.
You can place an alias in your .bashrc file (or the config file for your favorite shell-they are usually in your home directory):
A useful info link about using aliases in bash is here.
Consider setting the
CDPATH
environment variable in your .bashrc or .bash_profile. CDPATH takes the same colon-delimited form that otherPATH
like variables take, and letscd
take shortcuts in finding your directories. For example:I created a set of bash function for this and added it to my .bash_profile
Script
Usage
getFolders
Using getFolders you have a list of shorthand, description, and location for each folder. When you use getFolders with a shorthand it will output the folder location. You can get a list of available folders and their descriptions by simply calling getFolders without a parameter
Example of getting the folder path
outputs
Example of getting available folders.
outputs
gf
gf is a shortcut for getFolders
outputs
goto
goto is a simple way to cd to the selected folder.
output
cd has run on /path/to/alias/folder
/path/to/alias/folder>
Why have multiple functions
At the heart the Unix and Linux you have simple reusable functions that can be chained together. I prefer this method as it does not just move you to a selected location but, is a function that by alias returns a path. You can use this function to do anything from that point like cd to that path.
you can set up your own shortcut to a specific directory. For example you want to go to nested directory ,generally you do
you usually use the cd command and then type all the directory names or may be you skip typing (using TAB button) but it still takes few of your minutes.
So, to save your time you can make an alias of it in the terminal. Its one-time task to set up alias.
Now, you have to just use the alias name ( dir-target) to move into your target directory.