Create alias for desktop directory

If I type in cd Desktop, no matter what folder the terminal is currently open in, I want it to navigate to /home/bob-ubuntu/Desktop

In my .bashrc file I have the following lines at the bottom:

alias desktop='/home/bob-ubuntu/Desktop'

alias Desktop='/home/bob-ubuntu/Desktop'

and then I source it, but when I type in cd Desktop or cd desktop it still gives the same error?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

alias desktop='/home/bob-ubuntu/Desktop'
cd desktop

An alias is for a command name. A parameter to the cd command is not a command name. The alias is not used in this context.

If you type just desktop, this invokes the alias. But by default you’ll get an error

bash: /home/bob-ubuntu/Desktop: Is a directory

Add the line shopt -s autocd to your ~/.bashrc so that typing a directory name in command position performs cd to that directory. This way you can change to the directory ~/Desktop by typing just ~/Desktop (instead of cd ~/Desktop) or, with your alias, desktop.

Alternatively, define an alias to a command that works:

alias desktop='cd /home/bob-ubuntu/Desktop'

Solution 2

There are many ways:

  • You can make a variable for $desktop and/or $D as a shortcut for it.
  • You can alias desktop='cd /home/bob-ubuntu/Desktop'
  • You can use $USER/Desktop
  • You can use $XDG_DESKTOP_DIR if XDG user directories is set.
  • You can add /home/bob-ubuntu to CDPATH environment variable of cd command

But you are really better off just using:

cd ~/Desktop

Tilda shouldn’t hurt! 😀

Note that you can also use tilda to switch to $HOME directories of many users in your system as follows:

cd ~root
ls ~ftp
echo ~nobody

Solution 3

As icarus suggested in the comments, one simple way is to create an alias that does cd ~/Desktop

However if you really want cd to behave this way, you can alias a function to cd that handles checking if the directory is Desktop:

_cd () {
  if [ "$1" == "Desktop" ]; then
    cd ~/Desktop
    cd $1
alias cd="_cd"

However this also causes tab completion for cd to break

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply