linux中为用户添加权限_如何在Linux中为“ cd”命令定义基本目录

linux中为用户添加权限

linux中为用户添加权限

00_lead_image_define_cd_base_directory

By default, the Terminal window in Linux opens to your home directory. To change to any directory that is not directly in the home directory, you must provide the full path or use the “cd” command multiple times.

默认情况下,Linux中的“终端”窗口打开到您的主目录。 要更改到不直接位于主目录中的任何目录,必须提供完整路径或多次使用“ cd”命令。

For example, I regularly work with multiple directories within the Documents directory, which is in the home directory. I would like to be able to cd to folders in the Documents directory without having to type cd Documents first (or providing the full file path).

例如,我经常在主目录中的Documents目录中使用多个目录。 我希望能够cd到Documents目录中的文件夹,而不必先键入cd Documents (或提供完整的文件路径)。

Sound confusing? Here’s an example. Let’s say I have a folder called htgarticles inside my Documents folder, and I want to access it through the terminal.

听起来令人困惑? 这是一个例子。 假设我的Documents文件夹中有一个名为htgarticles的文件夹,我想通过终端访问它。

We’ll start by opening a Terminal window and entering the pwd (Print Working Directory) command to show that the current directory is indeed our home directory, /home/lori.

我们将从打开“终端”窗口并输入pwd (打印工作目录)命令开始,以显示当前目录确实是我们的主目录/ home / lori。

01_running_pwd_command

If we type the following command, we get an error saying that there is no such file or directory. That’s because we are not in the Documents directory.

如果键入以下命令,则会收到一条错误消息,提示没有此类文件或目录。 那是因为我们不在Documents目录中。

cd htgaricles
02_no_such_file_or_directory

To get to the htgarticles directory, first we have to change to the Documents directory.

要进入htgarticles目录,首先我们必须转到Documents目录。

cd Documents/

Then, we have to change to the htgarticles directory.

然后,我们必须转到htgarticles目录。

cd htgarticles/
03_changing_directories

Alternatively, we could arduously type out the full file path:

或者,我们可以艰苦地键入完整的文件路径:

cd ~/Documents/htgarticles

It doesn’t have to be this way, though. We can make any directory the base directory for the Terminal window, so we can cd to its sub-directories without typing the full path.

但是,不必一定是这种方式。 我们可以将任何目录作为“终端”窗口的基本目录,因此可以在不键入完整路径的情况下将其cd到其子目录。

In our example, we want to set the Documents directory as the base directory, because all the directories I work with–like htgarticles–are inside the Documents directory. So, we type the following command at the prompt and press Enter.

在我们的示例中,我们希望将Documents目录设置为基本目录,因为我使用的所有目录(例如htgarticles)都位于Documents目录中。 因此,我们在提示符下键入以下命令,然后按Enter。

export CDPATH=~/Documents/

NOTE: In the above command, export must be all lowercase and CDPATH must be all uppercase. Replace ~/Documents/ with the path to whichever parent directory contains the subdirectories you work with most often.

注意:在以上命令中, export必须全部为小写,而CDPATH必须全部为大写。 将~/Documents/替换为包含您最常使用的子目录的父目录的路径。

04_export_cdpath_command

Now, still in our home folder, we can run cd htgarticles and go straight to ~/Documents/htgarticles.

现在,仍然在我们的主文件夹中,我们可以运行cd htgarticles并直接转到〜/ Documents / htgarticles。

05_changing_directory_now_works

If you want to automatically set a certain directory as the base directory every time you open a Terminal window, you can do that by editing the .bashrc file. The .bashrc file is a script that runs every time you open a Terminal window, and you can add whatever commands you want to it. So, we can add the export CDPATH command to set a specific directory as the parent directory for every Terminal window.

如果要在每次打开“终端”窗口时自动将某个目录设置为基本目录,则可以通过编辑.bashrc文件来实现。 .bashrc文件是一个脚本,该脚本在每次打开“终端”窗口时运行,您可以向其中添加任何命令。 因此,我们可以添加export CDPATH命令以将特定目录设置为每个“终端”窗口的父目录。

To do this, cd back to your home directory if you aren’t already. Then, type the following command to open the .bashrc file. You can use whichever text editor you want to use, but we’re going to use gedit in our example.

为此,如果还没有,请cd返回您的主目录。 然后,键入以下命令以打开.bashrc文件。 您可以使用任何想要使用的文本编辑器 ,但是在示例中我们将使用gedit。

gedit .bashrc
06_opening_bashrc_file

Scroll to the bottom of the .bashrc file and add the following command.

滚动到.bashrc文件的底部,然后添加以下命令。

export CDPATH=~/Documents/

Again, replace ~/Documents/ with the parent directory that contains the subdirectories you work with most.

同样,将~/Documents/替换为包含您最常使用的子目录的父目录。

You can add a comment above the command so you remember what the command does. Simply put a number sign (#) at the beginning of the line, and then any description you want to add.

您可以在命令上方添加注释,以便记住命令的作用。 只需在行的开头加上数字符号(#),然后再添加任何要添加的描述。

Click “Save”.

点击“保存”。

07_adding_export_command_to_bashrc_file

Close gedit (or whatever text editor you’re using) by clicking the “X” in the upper-left corner of the window.

单击窗口左上角的“ X”,关闭gedit(或您使用的任何文本编辑器)。

08_closing_gedit

The command you just added to the .bashrc file will not affect the current Terminal window session. You must close the current Terminal window and open a new one. So, type exit at the prompt and press Enter or click the “X” button in the upper-left corner of the window. Then, press Ctrl+Alt+T to open a new Terminal window.

您刚刚添加到.bashrc文件中的命令不会影响当前的“终端”窗口会话。 您必须关闭当前的“终端”窗口并打开一个新窗口。 因此,在提示符下键入exit ,然后按Enter或单击窗口左上角的“ X”按钮。 然后,按Ctrl + Alt + T打开新的“终端”窗口。

09_closing_terminal_window

Now, even though the current working directory is the home directory, you can directly change to the subdirectories from the base directory you chose.

现在,即使当前工作目录是主目录,您也可以从所选的基本目录直接更改为子目录。

10_base_directory_changed

If you want to revert back to the home directory as the parent directory in the Terminal window, simply open the .bashrc file in a text editor and either delete the command you added or comment it out by adding a pound sign (#) at the beginning of the line. Commenting out the line is useful if you want to activate the command again in the future. You can also easily change which directory you want to use as the base directory simply by changing the directory in the command in the .bashrc file and saving the file.

如果要在“终端”窗口中还原为父目录的主目录,只需在文本编辑器中打开.bashrc文件,然后删除添加的命令或在命令行上添加井号(#)对其进行注释。行的开头。 如果您想将来再次激活该命令,则注释掉该行很有用。 您只需更改.bashrc文件中命令中的目录并保存文件,即可轻松更改要用作基本目录的目录。

If you have one specific directory you work in most of the time, you can also add a command to the .bashrc file that will open the Terminal window to that directory every time.

如果您通常在一个特定的目录中工作,则还可以向.bashrc文件中添加命令,该命令每次都会向该目录打开“终端”窗口

翻译自: https://www.howtogeek.com/274742/how-to-define-the-base-directory-for-the-cd-command-in-linux/

linux中为用户添加权限

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值