Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
    
Four years since this question was asked and nobody mentioned CDPATH until today. :( – kojiro Jun 28 at 13:23

10 Answers 10

up vote 9 down vote accepted

There are two options:

  1. If you want to be in a specific directory everyt time you open a bash terminal, edit your ~/.bashrc file and just add the line cd Directory, for example cd ~/Desktop.

  2. If you want to have several short-cuts, you can always use global variables, which you can set in your ~/.bashrc file as follows export a=/tmp and then you would be able to do cd $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.

share|improve this answer
    
Second one works well. Thanks for the help – Developer Android Dec 7 '12 at 9:42
1  
To avoid restarting the terminal you can use source ~/.bashrc or, shorter, . ~/.bashrc. – Gerhard Burger Dec 8 '12 at 9:33

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 have

/home/user/this/long/and/annoyingly/deep/directory/workstuff

then if you have visited it once you can jump to it by

j workstuff

or even

j stuff

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

/home/user/stuff

and you do

j stuff 

and then TabTabTab you get (in the order of most visited)

$ j stuff__
stuff__1__/home/user/this/long/and/annoyingly/deep/directory/workstuff
stuff__2__/home/user/stuff

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 add

source /usr/share/autojump/autojump.bash

to 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)

share|improve this answer
    
That seems very interesting. Thanks a lot – Developer Android Dec 7 '12 at 10:25
    
crtl + r in a terminal also looks for "partial matches" in previous commands. Really handy as well. – Dan Dec 7 '12 at 13:03

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:

    alias jump1='cd /long/path/name/that/is/frequently/used'
    
  • Close and open the terminal again, or run source ~/.bashrc.

  • From now on, you can just run jump1 to execute that long cd command.

See also:

share|improve this answer

Even though there are some good answers already, I thought I'd mention for completeness the old pushd and popd Bash builtins with allow you to move very quickly between directories in deep paths in your filesystem. In contrast to the autojump bookmarks mentioned by burger.ga, the directory stack created by using pushd is only temporary.

As the GNU manual notes, pushd and popd are Bash builtins used to construct a temporary directory stack that is a

list of recently-visited directories. The pushd builtin adds directories to the stack as it changes the current directory, and the popd builtin removes specified directories from the stack and changes the current directory to the directory removed. The dirs builtin displays the contents of the directory stack.

It 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 and popd:

Firstly, use dirs or dirs -l to list directories on the stack and dirs -c to clear the stack.

In a sense you bookmark the location where you want to return by entering

pushd /home/mike/Pictures/Canon/2012_07_01

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:

dirs
~/Downloads/folder/interest ~/Music/artist/album ~ ~/Pictures/Canon/2012_07_01

You actually don't need to use popd straightaway as that removes directories from the stack; the best thing to do is to use pushd 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):

pushd +3
~/Pictures/Canon/2012_07_01 ~/Downloads/folder/interest ~/Music/artist/album ~

So the prompt reads,

~/Pictures/Canon/2012_07_01$

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 with pushd, 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, from Pictures we can return to Downloads:

popd
~/Downloads/folder/interest ~/Music/artist/album ~

and the prompt reads

~/Downloads/folder/interest$

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 with popd while removing them from the directory stack. Then with your last popd 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:

share|improve this answer

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.

enter image description here

Then use Nautilus bookmarks to go faster in your favorite directories, and just right click on an empty space and select Open in Terminal.

enter image description here

Now you can have both command line and file manager quick access to your favorite directories.

share|improve this answer
    
The original poster is trying to work from the terminal and it is often quicker to work from the terminal. – haziz Dec 8 '12 at 6:28
1  
@haziz: Yes, but AskUbuntu questions and answers should be useful not only to the OP, but specially for all people who found this in the web. This is a valuable answer for me. – Javier Rivera Dec 8 '12 at 13:00

anc was designed exactly for that use case.

https://github.com/tobimensch/anc

Here's an excerpt from the README.md:

# make the current directory the default anchor:
$ anc s

# go to /etc, then /, then /usr/local and then back to the default anchor:
$ cd /etc; cd ..; cd usr/local; anc

# go back to /usr/local :
$ anc b

# add another anchor:
$ anc a $HOME/test

# view the list of anchors (the default one has the asterisk):
$ anc l
(0) /path/to/first/anchor *
(1) /home/usr/test

# jump to the anchor we just added:
# by using its anchor number
$ anc 1
# or by jumping to the last anchor in the list
$ anc -1

# add multiple anchors:
$ anc a $HOME/projects/first $HOME/projects/second $HOME/documents/first

# use text matching to jump to $HOME/projects/first
$ anc pro fir

# use text matching to jump to $HOME/documents/first
$ anc doc fir

# add anchor and jump to it using an absolute path
$ anc /etc
# is the same as
$ anc a /etc; anc -1

# add anchor and jump to it using a relative path
$ anc ./X11 #note that "./" is required for relative paths
# is the same as
$ anc a X11; anc -1

# using wildcards you can add many anchors at once
$ anc a $HOME/projects/*

# use shell completion to see a list of matching anchors
# and select the one you want to jump to directly
$ anc pro[TAB]

Full disclosure: I'm the author of anc.

share|improve this answer

You can place an alias in your .bashrc file (or the config file for your favorite shell-they are usually in your home directory):

alias cd1="cd directory/directory/my_destination/"

A useful info link about using aliases in bash is here.

share|improve this answer

Consider setting the CDPATH environment variable in your .bashrc or .bash_profile. CDPATH takes the same colon-delimited form that other PATH like variables take, and lets cd take shortcuts in finding your directories. For example:

$ mkdir -p /tmp/foo/bar/baz
$ CDPATH='/tmp/foo/bar'
$ cd baz
/tmp/foo/bar/baz
share|improve this answer

you can set up your own shortcut to a specific directory. For example you want to go to nested directory ,generally you do

cd /home/dir-1/dir-2/dir3/dir4/dir5/dir6/dir-target

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.

alias dir-target = '/home/dir-1/dir-2/dir3/dir4/dir5/dir6/dir-target'

Now, you have to just use the alias name ( dir-target) to move into your target directory.

share|improve this answer

I use DirB (Directory Bookmarks) to do this. Very nice! http://www.linuxjournal.com/article/10585

I have modified mine to suit my taste and be easier to use and clearer though.

share|improve this answer
    
Welcome to Ask Ubuntu! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – fossfreedom Dec 13 '12 at 21:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.