How do I shorten the current directory path shown on terminal?

Problem:

If I am in a deep directory, let’s say:

~/Desktop/Dropbox/School/2017/C/A3/

then when I open up terminal, it says

bob@bob-ubuntu:~/Desktop/Dropbox/School/2017/C/A3/$

and then I write my command. That is very long, and every line I write in the terminal goes to the next line. I want to know if there’s a way so that it only displays my current directory. I want it to display:

bob@bob-ubuntu: A3/$

This way it’s much clear, and always I can do pwd to see my entire directory. I just don’t want the entire directory visible in terminal because it takes too much space.

Solution:

  1. You need to modify PS1 in your shell startup file (probably .bashrc).

    If it’s there already, its setting will contain \w, which is what gives your working directory. Change that to \W (upper case). Log out and in again, or do:

    . .bashrc
    

    (or whatever your file is).

    If it isn’t there, add something like:

    PS1='\u@\h: \W:\$'
    

    to .bashrc or whatever. Look up PS1 in the bash manual page to get more ideas.

    Be careful; bash can use several more than one initialisation file, e.g. .bashrc and .bash_profile; it may be that PS1 is set in a system-wide one. But you can override that in one of your own files.

    OR

  2. Since bash 4, the straightforward way to shorten the depth of directory in command-line is using the below command in your bashrc file. Just remember to reopen your terminal and also the number (i.e. 1) specifies the depth of the directory to show.

    PROMPT_DIRTRIM=1

How to add a directory to the PATH ubuntu?

Edit .bashrc in your home directory and add the following line:

export PATH="/path/to/dir:$PATH"

You will need to source your .bashrc or logout/login (or restart the terminal) for the changes to take effect. To source your .bashrc, simply type

$ source ~/.bashrc