Create executable file for comand line Linux

2019-09-17 08:17发布

问题:

When ever I log into Linux I usually go straight to the same folder i was wondering if in stead of typing in:

$cd Document/..../..../..../..../....

I could create an executable so I could just type ./csFolder and it would go straight there.

回答1:

You can add a shell function in your .bashrc and restart your terminal:

csf() {
  cd Document/..../..../..../..../....
}

Whenever you want to go to that directory, you just run csf.



回答2:

Yo can do a symlink

ln -s /path/to/file /path/to/symlink



回答3:

In addition to the other options (though if you use the function/alias option you want to use an absolute path to the target directory so it works from wherever you happen to be) you can use the environment variable CDPATH to help with this if you have a location you often go to from various other locations.

From the POSIX specification:

CDPATH

A -separated list of pathnames that refer to directories. The cd utility shall use this list in its attempt to change the directory, as described in the DESCRIPTION. An empty string in place of a directory pathname represents the current directory. If CDPATH is not set, it shall be treated as if it were an empty string.

Which means that if you set CDPATH to the parent of your target directory you can just use cd dirname from anywhere and go directly to the directory you wanted to be in.