Define alias for a directory as an argument to cd in Bash [closed]
Asked Answered
R

3

6

I am using Windows 10 Linux Subsystem (Ubuntu Bash).

I want to access my Windows folder from Ubuntu Bash.

The folder I want to access is (note that there are spaces in the names):

/mnt/c/Users/some folder1/some folder2/destination - folder/

What I do now in Bash is:

~$ cd /mnt/c/Users/some\ folder1/some\ folder2/destination\ -\ folder/

Because the directory is too deep, I don't want to type the long command every time. So I want to define an alias for that folder.

In Bash, I created a file ~/.bash_aliases. And in the file ~/.bashrc there is following command:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Next, I want to add the following line in ~/.bash_aliases:

alias mf=<the correct format of the directory>

After mf is defined, I want to switch to the directory using the following command:

~$ cd mf

Can you help me with this?

1) How do write the <the correct format of the directory>?

2) What else do I need to do in order to use

~$ cd mf
Regnant answered 16/1, 2018 at 9:30 Comment(2)
I wanted the same functionality so I made a simple CLI that creates a cdd terminal command which works almost like cd but helps create aliases to any path on the go. Hope it helps :) github.com/nsrCodes/cddOast
Stack Overflow is for programming questions only, so shell config and interactive use is off-topic, though there is overlap. You can ask on Ask Ubuntu instead. Other options are Unix & Linux and Super User.Irisation
T
4

As this page notes, "an alias is only meaningful at the beginning of a command". So your alias can't be an argument to cd. What you can do is

alias cdmf='cd /mnt/c/Users/some\ folder1/some\ folder2/destination\ -\ folder/'

i.e. including the cd command in your alias, so that typing 'cdmf' takes you to your target directory.

To use cd mf, you'd normally use a symbolic link (or a bash function), not an alias. Try

ln -s /mnt/c/Users/some\ folder1/some\ folder2/destination\ -\ folder/ mf

However, the link that this produces only resides in the current directory, so you can't navigate to your target directory from anywhere.

Tryma answered 16/1, 2018 at 10:9 Comment(1)
Yep symlinks are the way to go. I'd generally put such symlinks in ~ (e.g. ln -s ... ~/mf) so that you can do cd ~/mf from anywhere.Interlunation
C
11

Rather than alias, you can use the cdable_vars option available in bash (documented here). To enable, add shopt -s cdable_vars to your .bashrc and then create an environment variable that contains the directory path. For example, you might add this to your .bashrc

shopt -s cdable_vars
mydir=/really/long/dir/path

and then you will be able to use this in the shell as follows:

mike:/home/mike$ cd mydir
/really/long/dir/path
mike:/really/long/dir/path$ 
Cornell answered 4/3, 2020 at 22:16 Comment(2)
works great if you have like Documents on another drive but still want to use cd Documents (also remapped it in nautilus)Klug
Just wanted to add that BOTH lines need to go in the .bashrc as the shopt status doesn't retain between sessions.Paulita
T
4

As this page notes, "an alias is only meaningful at the beginning of a command". So your alias can't be an argument to cd. What you can do is

alias cdmf='cd /mnt/c/Users/some\ folder1/some\ folder2/destination\ -\ folder/'

i.e. including the cd command in your alias, so that typing 'cdmf' takes you to your target directory.

To use cd mf, you'd normally use a symbolic link (or a bash function), not an alias. Try

ln -s /mnt/c/Users/some\ folder1/some\ folder2/destination\ -\ folder/ mf

However, the link that this produces only resides in the current directory, so you can't navigate to your target directory from anywhere.

Tryma answered 16/1, 2018 at 10:9 Comment(1)
Yep symlinks are the way to go. I'd generally put such symlinks in ~ (e.g. ln -s ... ~/mf) so that you can do cd ~/mf from anywhere.Interlunation
G
0

I didnt want to go the cdable_vars method because I was worried that I could have parsing errors. Perhaps I would have a variable name that happened to occur in a directory path I didnt want to have problems that way.

I also avoided alias cdmf='cd ...' for the same reason.

I decided to go with the dollar sign variable names to help curtail parsing errors. To do this, edit your ~/.profile file (I think ~/.bash_profile also works)

emacs ~/.profile

put the following at the bottom.

export ldn="/long/directory/name"
export vldn="/very/long/directory/name"

To make this active in the current terminal, you need to run

source ~/.profile

This will make it active in the current terminal, and it should persist across sessions. If I open a new terminal and type

echo $ldn

It should list the path created in the file. You access the variable with a dollar sign (perl style) instead of plain text. This is probably better because then you wont have problems with parsing issues, i.e. your variable name has occurred in a path name and creates problems.

cd $ldn

should work like you expect, also subdirectories should also work so

cd $ldn/subdirectory/in/this/path

should also work like you expect

Goat answered 2/8, 2024 at 15:53 Comment(2)
"for the same reason" - What do you mean by that? It's not using variable names, so how does the same reason apply?Irisation
When to wrap quotes around a shell variable? (basically always, at least when posting on a site where your code will be copy/pasted to all kinds of weird places).Cordey

© 2022 - 2025 — McMap. All rights reserved.