Create alias for desktop directory

9,134

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! :D

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
Share:
9,134

Related videos on Youtube

K Split X
Author by

K Split X

Updated on September 18, 2022

Comments

  • K Split X
    K Split X over 1 year

    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?

    • ethanwu10
      ethanwu10 almost 7 years
      Aliases only replace a command (so the first word in the command line) - you can't use them to replace command-line arguments
    • K Split X
      K Split X almost 7 years
      How can I do this then?
    • icarus
      icarus almost 7 years
      "how can I do it then?" Two ways. You can create a new "desktop" alas/function. desktop(){ cd $HOME/Desktop ; } or you can set the CDPATH variable to "$HOME:." and use "cd Desktop"
    • ethanwu10
      ethanwu10 almost 7 years
      Just be aware with the second suggestion by @icarus (CDPATH) this would make cd always go to a directory in your home directory over a directory in the current directory.