UNIX初学者的10个省时的终端提示

tech2023-07-10  114

The terminal is the preferred choice of working space for many web developers these days. And why not? With the right set of commands, the terminal can definitely get things done faster. However, beginners make certain time-consuming mistakes. Time is money and in this post, we’ll focus on a few areas that can help you complete your actions quickly and efficiently.

如今,终端是许多Web开发人员首选的工作空间选择。 那么为何不? 使用正确的命令集,终端绝对可以更快地完成工作。 但是,初学者会犯一些耗时的错误。 时间就是金钱,在这篇文章中,我们将重点关注可以帮助您快速有效地完成操作的几个方面。

1.使用制表符补全 (1. Use Tab Completion)

This is perhaps the most common mistake I have seen beginners make. Suppose you need to open a file named “my_long_file_name.py” in a text editor. You type the whole name of the file and hit enter — only to realize you misspelled it. So you start typing it again. That’s not how it should work.

这也许是我初学者看到的最常见的错误。 假设您需要在文本编辑器中打开一个名为“ my_long_file_name.py ”的文件。 您键入文件的全名,然后按Enter键-只是意识到您拼写错误。 因此,您再次开始输入。 那不是它应该如何工作的。

Once you have typed 2-3 characters, hit tab and the rest of the name would be competed for you. If you have similar file names (like “my_long_file_name.py” and “my_long_file_name.py.bkp”), the common part would be completed for you and you would be shown both options, as shown below.

输入2-3个字符后,请点击Tab键,其余名称将为您竞争。 如果您具有相似的文件名(例如“ my_long_file_name.py ”和“ my_long_file_name.py.bkp ”),则公共部分将为您完成,并且将显示两个选项,如下所示。

The same applies for commands too. Type in part of the command and hit tab to complete the command. If there are similar commands, you would be shown the option just like in case of the files.

命令也一样。 输入命令的一部分,然后单击选项卡以完成命令。 如果有类似的命令,则将像显示文件一样显示该选项。

There is no downsides of over tabbing, so go ahead and start using it frequently, if you haven’t already.

没有过度制表的弊端,所以如果您还没有的话,请继续并频繁使用它。

2.使用!! 前缀到最后一个命令 (2. Use !! to prefix to the last command)

In a previous article, I mentioned sudo !! runs the previous command with administrator privileges. What I didn’t mention is that all !! does is replace itself with the previous command.

在上一篇文章中 ,我提到了sudo !! 使用管理员权限运行前面的命令。 我没有提到的是全部!! 所做的就是用先前的命令替换自身。

To perform the same task, hit the ‘up’ arrow, followed by the home key (or Fn + <-) to reach the beginning of the command. It really comes down to your personal choice.

要执行相同的任务,请点击“向上”箭头,然后按下home键(或Fn + <- )以到达命令的开头。 这确实取决于您的个人选择。

In addition, !:n selects the nth argument of the last command (but not the rest of the command), whereas !$ selects the last argument of the last command.

此外, !:n选择最后一个命令的第n个参数(但不选择其余命令),而!$选择最后一个命令的最后一个参数。

3.剪切和粘贴线条 (3. Cut and paste lines)

If you are typing something long and realized it needs to be put somewhere else, you could cut whatever is there in your current line by pressing Ctrl/Cmd+U. You could then paste it somewhere else by pressing Ctrl/Cmd+Y. They are useful shortcuts, if you don’t want to get messy by selecting text to perform these actions.

如果您输入的内容很长,并且意识到需要将其放置在其他位置,则可以通过按Ctrl / Cmd + U剪切当前行中的所有内容。 然后,您可以通过按Ctrl / Cmd + Y将其粘贴到其他位置。 如果您不想通过选择文本来执行这些操作而感到混乱,它们是有用的快捷方式。

4.清除屏幕 (4. Clear screen)

If you want to clear the output of the previous commands from the terminal’s view, you can run —

如果您想从终端机的视图中清除先前命令的输出,则可以运行-

clear

There is a shortcut for this, however. Just hit Ctrl/Cmd+L and all the text above your current line is cleared. This shortcut can be used when you are halfway writing a command, without losing it, unlike the clear command.

但是,这有一个捷径。 只需Ctrl / Cmd + L ,您当前行上方的所有文本都会被清除。 与clear命令不同,此快捷方式可在您半途编写命令时使用,而不会丢失。

5. cd到主目录 (5. cd to home directory)

No matter what your working directory is (run pwd to get your absolute working directory), you can switch back to your home directory (/home/username/) any time by running the following —

无论您的工作目录是什么(运行pwd以获得绝对工作目录),都可以通过运行以下命令随时切换回主目录( /home/username/ )-

cd

Alternately, you can run the following too:

或者,您也可以运行以下命令:

cd ~

Which brings us to your next tip…

这将带给您下一个提示...

6.主目录的相对路径 (6. Relative path to your home directory)

You probably know that your home directory is /home/username/, but while working in the terminal, you can use ~ as a relative path to your home directory. For instance:

您可能知道您的主目录是/home/username/ ,但是在终端中工作时,可以将~用~目录的相对路径。 例如:

cd ~

points to:

指着:

cd /home/username/

Here’s a look at some other examples.

这是其他一些例子。

vi ~/.bash_history cd ~/git.slides/ scp -r ./ donny@192.168.121.147:~/

The commands listed above are equivalent to the following:

上面列出的命令等效于以下命令:

vi /home/username/.bash_history cd /home/username/git.slides/ scp -r ./ donny@192.168.121.147:/home/donny/

7.使用别名 (7. Using an alias)

I’m sure you must have run long commands that are essential to your work. Commands like the following:

我确定您必须运行对您的工作必不可少的长命令。 如下命令:

python manage.py runserver --settings=project.development

To avoid typing such long commands every time you need them, you can use an alias.

为了避免每次需要时都键入如此长的命令,可以使用别名。

alias start-dj="python manage.py runserver --settings=project.development"

Next time you run start-dj, what actually gets executed is this — python manage.py runserver --settings=project.development.

下次运行start-dj ,实际执行的是python manage.py runserver --settings=project.development 。

8.使用.bashrc (8. Use the .bashrc)

Using an alias is great, but what’s sad is that they get removed from the memory once you close the terminal. To save them, you need to store them in your .bashrc file. By default, it is stored in your home directory (/home/username/.bashrc or ~/.bashrc). Just paste the alias commands in the .bashrc file and restart the terminal for them to take effect.

使用别名很棒,但是令人遗憾的是,一旦关闭终端,它们就会从内存中删除。 要保存它们,您需要将它们存储在.bashrc文件中。 默认情况下,它存储在您的主目录( /home/username/.bashrc或~/.bashrc )中。 只需将别名命令粘贴到.bashrc文件中,然后重新启动终端即可使它们生效。

If you don’t want to restart the terminal, save the .bashrc file and run the following for the new aliases to take effect.

如果您不想重新启动终端,请保存.bashrc文件,然后运行以下命令使新别名生效。

. ~/.bashrc

Developers can get pretty innovative when customizing commands using aliases. There is a wide variety of custom commands to make your work easier, but I wouldn’t mention any of them because they vary greatly from person to person. Perhaps, you will develop your own shortcuts in due time.

使用别名自定义命令时,开发人员会获得相当新颖的效果。 有各种各样的自定义命令可以使您的工作更轻松,但是我不会提及其中的任何一个,因为它们因人而异。 也许,您会在适当的时候开发自己的快捷方式。

9.随时随地创建文件 (9. Create a file on the go)

You can dynamically create a file and with the contents as the output of a function pretty easily.

您可以轻松地动态创建文件并将内容作为函数的输出。

[command] > sample_file

The file sample_file is created and is replaced if it already exists (without any confirmation). Therefore, you must be careful while doing this.

将创建文件sample_file ,如果文件已经存在(没有任何确认),则将其替换。 因此,在执行此操作时必须小心。

This command is great for taking database dumps. For example:

该命令非常适合进行数据库转储。 例如:

mysqldump -u [username] -p -D [database] > db_dump

A consequence of this is that you can easily create empty files by giving an empty command (which of course, has an empty output).

这样的结果是,您可以通过给出一个空命令(当然它具有一个空输出)来轻松创建空文件。

> sample_file

The command above will create an empty file with the name sample_file.

上面的命令将创建一个名为sample_file的空文件。

10.使用文件内容作为命令的参数 (10. Use file contents as arguments to a command)

Just like we can store the output of a command into a file, we can use the contents of a file as the input for a command. For example, the following will run the command with the inputs of sample_file as arguments.

就像我们可以将命令的输出存储到文件中一样,我们可以将文件的内容用作命令的输入。 例如,以下将使用sample_file的输入作为参数来运行命令。

[command] < file

A use case of this is to restore an SQL dump.

一个用例是还原SQL转储。

mysql -u [username] -p -D [database_name] < db_dump

This will first login into MySQL and then run each line in db_dump in the MySQL shell.

这将首先登录MySQL,然后在MySQL Shell中运行db_dump中的每一行。

奖金 (Bonus)

Now that you have completed your work on the terminal, you can type exit and hit enter, or you could just press Ctrl/Cmd + D to do the same — your choice.

现在,您已经在终端上完成了工作,可以键入exit并按Enter,或者可以按Ctrl / Cmd + D进行相同的操作-您的选择。

With this, we come to the end of our list of time savers. Do you have any interesting UNIX tips or aliases you want to share? Feel free to comment below.

这样,我们就可以节省时间了。 您是否有任何有趣的UNIX技巧或别名要共享? 请在下面发表评论。

翻译自: https://www.sitepoint.com/10-time-saving-terminal-tips-unix-beginners/

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)