Create alias for desktop directory
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
toCDPATH
environment variable ofcd
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
Related videos on Youtube
K Split X
Updated on September 18, 2022Comments
-
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
orcd desktop
it still gives the same error?-
ethanwu10 almost 7 yearsAliases 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 almost 7 yearsHow can I do this then?
-
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 almost 7 yearsJust be aware with the second suggestion by @icarus (
CDPATH
) this would makecd
always go to a directory in your home directory over a directory in the current directory.
-