17

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
Flimm
  • 41,766
  • Four years since this question was asked and nobody mentioned CDPATH until today. :( – kojiro Jun 28 '16 at 13:23
  • I suppose this may depend on your console, but in Kubuntu terminal there is a Bookmarks menu where I can just bookmark a directory and jump to it that way ! –  Jan 29 '20 at 07:42

12 Answers12

22

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)

  • 1
    crtl + r in a terminal also looks for "partial matches" in previous commands. Really handy as well. – Dan Dec 07 '12 at 13:03
15

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.

Flimm
  • 41,766
Dan
  • 673
8

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:

Flimm
  • 41,766
  • This should be the correct answer as it's the simplest way to use the built in bashrc functionality. In addition it is shorter to write jump1 instead of cd $jump1 as mentioned in the accepted answer. – phi Sep 22 '20 at 15:06
5

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:

2

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.

remjg
  • 3,663
  • The original poster is trying to work from the terminal and it is often quicker to work from the terminal. – haziz Dec 08 '12 at 06: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 08 '12 at 13:00
2

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.

Tobi
  • 21
1

I created a set of bash function for this and added it to my .bash_profile

Script

goto(){
    cd $(getFolders $1)
}

gf(){
    getFolders $1
}

getFolders (){

    local folderList=(
     'alias'
     'Description of alias'
     '/path/to/alias/folder'
     'alias2'
     'Description of alias2'
     '/path/to/alias2/folder'
    )
    local moved="0"
    local count=0

    # Returns the path to the project
    while [ "${folderList[count]}" != "" ]
    do
        if [ "$1" == "${folderList[count]}" ]; then
            echo ${folderList[$(( $count + 2 ))]}
            moved="1"
        fi
        count=$(( $count + 3 ))
    done

    # Returns all project names
    if [ "$moved" != "1" ]; then
        count=0
        while [ "${folderList[count]}" != "" ]
        do
            echoc 6 ${folderList[count]}
            echo - ${folderList[count+1]}
            count=$(( $count + 3 ))
        done
    fi
}

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

getFolders alias

outputs

/path/to/alias/folder

Example of getting available folders.

getFolders

outputs

alias
- Description of alias
alias2
- Description of alias2

gf

gf is a shortcut for getFolders

gf alias2

outputs

/path/to/alias2/folder

goto

goto is a simple way to cd to the selected folder.

goto alias

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.

muru
  • 197,895
  • 55
  • 485
  • 740
1

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.

haziz
  • 2,929
1

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
kojiro
  • 321
0

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.

sanjay mundhra
  • 141
  • 1
  • 6
0

The quickest way I found is xd, an "eXchange Directories" program written by a Dutchman named Frank B. Brokken.

It works by finding all paths on your filesystem that can resolve to an abbreviation you typed, e.g. ulb could stand for /usr/local/bin if such folder exists your system:

% cx ulb
Multiple Solutions:
 1: /usr/lib/binfmt.d/
 2: /usr/local/bin/

On pressing 2 you're in /usr/local/bin.

It's really old software and no longer available at original location, no contact with author, so to prevent it being lost, I put it in Github repo:

https://github.com/mrkafk/exchange_directories

-1

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.