Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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

This question already has an answer here:

Every time I wish to move to a directory I use very often, I must type this long directory path:

cd /media/prasanth/01D0F888E7BC91801/github projects

or I must use the gui to get into the folder and open the terminal.

Is it possible to assign this long path to a single name and enter it via the terminal like

cd mygitfiles

?

share|improve this question

marked as duplicate by muru, Serg, Braiam, Fabby, Pilot6 Jan 5 at 7:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

So far two great have been given in other answers, I have one more possibility:

ln -s /media/prasanth/01D0F888E7BC91801/github/projects ~/mygitfiles

Will create a symlink to the long path in your home.

Symlinks are like shortcuts that point to another file or directory, and most programs will treat them as normal. you can safely rm the symlink without effecting the path it points to. you can observe where the symlink points to with ls -l.

You can use the symlink as if it was a real directory and do cd ~/mygitfiles.

Note that ~ is simply an alias for your home directory, which might be a sensible place for such a symlink.

Be aware that using this symlink ultimately effects your current working directory.

In the general case I would suggest Nicolas Delvaux, or Eduardo Cola's answer, but using a symlink might be just as - or possibly more useful.

share|improve this answer

You can store your path in an environment variable. Run:

nano $HOME/.bashrc

Go to the end of the file, create a new line and write:

export mgf="/media/prasanth/01D0F888E7BC91801/github projects"

Hit Ctrl+O and ENTER to save the file. Hit Ctrl+X to exit. Now log out and log in. When youn open a terminal (running bash) you can run cd "$mgf" to change to your directory.

share|improve this answer
3  
There's no need to logout though. Either run . ~/.bashrc (if in the console or in a terminal) or close / reopen the terminal (if in a terminal). – kos Jan 4 at 15:12
3  
There's no need for the sudo. It's your .bashrc, you should be able to edit it. – Maerlyn Jan 4 at 16:51
1  
Good answer and this can work with subdirectories, like cd $mgf/subdir/. Upvoted – Serg Jan 4 at 19:32

You should use aliases. See help alias.

In a nutshell, you can define an alias by typing:

alias whatever="cd /media/prasanth/01D0F888E7BC91801/github/projects"

Then, typing whatever will move you to the defined folder.

You can put the alias command in your ~/.bashrc to keep it permanently.

share|improve this answer

It seems like you're looking for cdargs.

It provides bookmarks for the cd command.

You can add a bookmark by executing mark NAME in the directory that is bookmarked. Then you can change to this directory from anywhere using cv NAME with NAME being the name of the bookmark.

share|improve this answer
    
Wow. My life just got that much easier. Thank you! – Michael Hoffmann Jan 6 at 7:49

Bash has a handy little misfeature called CDPATH. When cd foo looks for a relative path, it looks in the current directory by default. By setting CDPATH to a colon-separated list of path prefixes, we can go to that target instead if no match was found. This works similarly to the PATH variable when looking for executable files.

In your case, you could export CDPATH=.:/media/prasanth/01D0F888E7BC91801/github/ in your ~/.bashrc, and then execute cd projects/foo from anywhere to get to /media/prasanth/01D0F888E7BC91801/github/projects/foo.

However, this will not work if your current directory contains a matching entry, so this can have some confusing error modes. There are some people that swear by this methods, I myself rather prefer symlinks in the home directory as suggested by Paul Grove.

share|improve this answer

You could have a look at autojump. It basically learns as you use cd by building a database of folders you visit and assigning weights to them: the more you visit a folder, the higher it is in the database.

Then, after you cd a few times to a directory (even once suffices):

cd /media/prasanth/01D0F888E7BC91801/github projects

you can jump there by using the j command:

j proj

You can use any part of the directory name as an argument to the j command and it will jump to the first entry that matches, so that's helpful when you don't remember the full path.

It has some other capabilities which are pretty neat such as jump to child (jc). I encourage you to inspect it.

share|improve this answer
    
I use autojump, and it works well. The best thing is that it learns from your use. I didn't know about jc, so that's something for me to look at. – Paddy Landau Jan 12 at 11:08

I would type

cd /media/prasanth/01D0F888E7BC91801/github projects #demo-proj

the first time I run that command. The content after # is a comment.

Then to call it, I would CtrlR and type in de (a convenient substring of demo-proj), till the previous command comes up.

(reverse-i-search)`de': cd /media/prasanth/01D0F888E7BC91801/github projects #demo-proj

I can hit CtrlR again for even-earlier uses.


This has no need to set/replace permanent aliases. I also do this for long commands or even netstat -tulpn which I find cryptic.

share|improve this answer

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