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

Since bash 4, to shorten the depth of directory in command-line is done by using PROMPT_DIRTRIM in the .bashrc file. Just remember to reopen your terminal.

PROMPT_DIRTRIM=1

See the Bash Manual for more information.

Example

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

will be trimmed to

bob@bob-ubuntu:.../A3/$


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). The line in bashrc file looks like below:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '

Log out and in again, or do:

. .bashrc

or (you need to add this prefix '~/' if you are in others directory)

source ~/.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.


Assuming you're using bash, change the prompt string (variable PS1) so that it has \W instead of \w.

e.g. if your PS1 is currently \u@\h:\w\$, set it to \u@\h:\W\$

To make this permanent, you will have to change it in your bash startup files - e.g. ~/.bash_profile or ~/.bashrc.

see man bash and search for PROMPTING for full details and a list of backslash-escaped special characters.