10

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

?

Prasanth P
  • 315
  • 2
  • 3
  • 8

7 Answers7

20

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.

10

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.

10

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.

muru
  • 197,895
  • 55
  • 485
  • 740
Eduardo Cola
  • 5,817
9

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.

rbialon
  • 194
5

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.

  • 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 '16 at 11:08
3

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.

amon
  • 131
2

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.