如何在Linux中将终端打开到特定目录

00_lead_image_open_terminal_to_specific_directory

By default, new Terminal windows open to your Home directory. However, if there’s another directory you use often that you want immediate access to when you open the Terminal, there’s an easy way to set this up.

默认情况下,新的终端窗口会打开到您的主目录。 但是,如果打开终端时经常使用另一个要立即访问的目录,则有一种简便的方法来进行设置。

The .bashrc file in your Home directory contains commands that run when you open a Terminal window. So, we can add a cd command to change to a specific directory as soon as the Terminal window opens. To do this, press Ctrl+Alt+T to open a Terminal window. As we mentioned, you are in your Home directory by default, which is where you want to be right now.

主目录中的.bashrc文件包含打开“终端”窗口时运行的命令。 因此,一旦“终端”窗口打开,我们可以添加cd命令以更改到特定目录。 为此,请按Ctrl + Alt + T打开“终端”窗口。 如前所述,默认情况下,您位于主目录中,该目录现在是您想要的位置。

We’re going to edit the .bashrc file, so type the following command to open it. You can use whichever text editor you want to use, but we’re going to use gedit in our example.

我们将编辑.bashrc文件,因此键入以下命令将其打开。 您可以使用任何想要使用的文本编辑器,但是在示例中我们将使用gedit。

gedit .bashrc
01_opening_bashrc_file

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

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

cd ~/HTGArticles

The tilde character (~) is a shortcut for your Home directory, which in our example, is /home/lori. So, the full path for the directory in the above command is /home/lori/HTGArticles.

代字号(〜)是主目录的快捷方式,在我们的示例中为/home/lori 。 因此,以上命令中目录的完整路径为/home/lori/HTGArticles

Replace ~/HTGArticles with the directory you want to open when you open a Terminal window.

在打开“终端”窗口时,将~/HTGArticles替换为您要打开的目录。

The following line is a comment we added above the cd command, explaining what the command is doing. You don’t have to add a comment, but it helps make the .bashrc file easier to understand. The pound sign (#) at the beginning of a line indicates that line is a comment.

下一行是我们在cd命令上方添加的注释,解释了该命令正在执行的操作。 您无需添加注释,但是它有助于使.bashrc文件更易于理解。 行首的井号(#)表示该行是注释。

# Open to HTGArticles directory

Once you’ve added the command to the file, click “Save” in the upper-right corner of the window to save the file.

将命令添加到文件后,单击窗口右上角的“保存”以保存文件。

02_adding_cd_command_to_bashrc

Close the .bashrc file by clicking the “X” button in the upper-left corner of the window.

单击窗口左上角的“ X”按钮,关闭.bashrc文件。

03_closing_gedit

You must restart the Terminal window for this change to take effect. To close the Terminal window, either type exit at the prompt and press Enter, or click the “X” button in the upper-left corner of the window.

您必须重新启动终端窗口,此更改才能生效。 要关闭“终端”窗口,请在提示符下键入exit并按Enter,或单击窗口左上角的“ X”按钮。

04_closing_terminal_window

Now, press Ctrl+Alt+T to open the Terminal again. You are immediately in the directory you specified in the .bashrc file. You can still navigate to any other directory, but this is helpful if you work in this directory the most.

现在,按Ctrl + Alt + T再次打开终端。 您将立即在.bashrc文件中指定的目录中。 您仍然可以导航到任何其他目录,但是如果您最经常在此目录中工作,这将很有帮助。

05_terminal_window_open_to_specific_directory

If you use both Nautilus and the Terminal to work with files, you can open a specific directory in Nautilus from the Terminal as well as open a Terminal window to a specific directory from Nautilus.

如果同时使用Nautilus和Terminal来处理文件,则可以从Terminal在Nautilus中打开特定目录,也可以从Nautilus打开到特定目录的Terminal窗口

翻译自: https://www.howtogeek.com/270817/how-to-open-the-terminal-to-a-specific-directory-in-linux/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值