mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
TSL&PRF
This commit is contained in:
parent
2a68820ef9
commit
d3248dac00
@ -1,417 +0,0 @@
|
||||
[#]: subject: "19 Absolute Simple Things About Linux Terminal Every Ubuntu User Should Know"
|
||||
[#]: via: "https://itsfoss.com/basic-terminal-tips-ubuntu/"
|
||||
[#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/"
|
||||
[#]: collector: "lujun9972"
|
||||
[#]: translator: "wxy"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
19 Absolute Simple Things About Linux Terminal Every Ubuntu User Should Know
|
||||
======
|
||||
|
||||
Terminal often intimidates new users. However, once you get to know it, you gradually start liking it. Well, that happens with most Linux users.
|
||||
|
||||
Even if you are using Ubuntu as a desktop system, you may have to enter the terminal at times. New users are often clueless about many things. Some knowledge of basic Linux commands always helps in such cases but this article is not about that.
|
||||
|
||||
This article focuses on explaining small, basic and often ignored things about using the terminal. This should help new Ubuntu desktop users to know the terminal and use it with slightly more efficiency.
|
||||
|
||||
![][1]
|
||||
|
||||
The terminal you see is just one of the [various terminal applications][2] available. After all terminal is just a GUI tool that gives you access to a shell where you can run the commands.
|
||||
|
||||
Different terminal applications (properly called terminal emulators) look different, have slightly different functions and features (like different keyboard shortcuts, color combination, fonts etc).
|
||||
|
||||
This article is specifically focused on the default Ubuntu terminal which is an implementation of the GNOME terminal.
|
||||
|
||||
### 1\. Open the terminal with keyboard shortcut
|
||||
|
||||
You can [open the terminal in Ubuntu][3] by looking for it in the system menu. However, my favorite way is to use the Ctrl+Alt+T [keyboard shortcut in Ubuntu][4].
|
||||
|
||||
```
|
||||
|
||||
Ctrl+Alt+T
|
||||
|
||||
```
|
||||
|
||||
### 2\. Terminal vs shell vs prompt vs command line
|
||||
|
||||
Before you see anything else, you should know the difference between different terminologies that are often (incorrectly) used interchangeably.
|
||||
|
||||
![Terminal, prompt and command][5]
|
||||
|
||||
Terminal is the graphical application that runs a shell by default.
|
||||
|
||||
Shell is difficult to visualize separately from the terminal. The terminal is running a shell, usually Bash shell by default in Ubuntu. Like terminals, there are various shells as well. Bash is the most popular of them all and default shell on most Linux distributions.
|
||||
|
||||
The commands you type is interpreted by the shell. Often people think that screen they see in the terminal is the shell. That’s fine for understanding.
|
||||
|
||||
Prompt is what you see before the space where you type the commands. There is no set standard for the prompt. In some old terminals, you would just have a blinking cursor to the place where you can type the commands. In Ubuntu terminal, prompt gives you some information which you’ll see in detail in the later sections of this article.
|
||||
|
||||
Command line is not something specific to Linux. Every operating system has a command line interface. Many programming languages have command line interface. It’s a term used for the interface where you can run and execute commands.
|
||||
|
||||
There is this video by Luke Smith that explains it in detail with examples. I won’t go in details here anymore to keep things on the track.
|
||||
|
||||
[Subscribe to It’s FOSS YouTube channel for interesting Linux videos][6]
|
||||
|
||||
### 3\. Understanding the prompt
|
||||
|
||||
You know it by now. What you see before the space where you type the command is called prompt. It is configurable and looks different in different distributions, terminal applications and shells.
|
||||
|
||||
Ubuntu terminal has configured the prompt to show you a few things. You can get the following information at a glance:
|
||||
|
||||
* User name
|
||||
* Hostname (name of the computer)
|
||||
* Current working directory
|
||||
|
||||
|
||||
|
||||
A few more things you may wonder about.
|
||||
|
||||
The colon (:) in the prompt is a separator to distinguish between hostname and the current location.
|
||||
|
||||
Tilde (~) means the home directory of the present user.
|
||||
|
||||
For normal users, the prompt ends with dollar ($) symbol. For the root user, it ends with pound or hash (#) symbol. And hence the joke that pound is stronger than dollar.
|
||||
|
||||
![][7]
|
||||
|
||||
Did you notice that when I switched to the root user, the command prompt looked different without any colors? This is another reminder that prompt is not a standard and is configured explicitly. For normal users, Ubuntu has a different configuration of the prompt than the root.
|
||||
|
||||
Simple information like this helps indirectly. In a multi-user environment, you can easily figure out which user you are using right now and if it is a root user. The displayed location is also helpful.
|
||||
|
||||
![][8]
|
||||
|
||||
### 4\. Directory and files
|
||||
|
||||
Two terms you hear the most in Linux are directory and files.
|
||||
|
||||
You probably know what a file is but you may get confused with the term ‘directory’. Directory is nothing but folder. It keeps files and folders inside it.
|
||||
|
||||
You can go inside the directories but you cannot enter files. You can read files of course.
|
||||
|
||||
![][9]
|
||||
|
||||
You can use the term ‘folder’ for directory and it should be fine. However, it is better to use ‘directory’ because this is what you’ll see referenced in various tutorials, documents etc. You’ll even find commands like rmdir, mkdir hinting that they deal with directories.
|
||||
|
||||
Additional note: Everything is a file in Linux. Even directory is a special kind of file that has the memory address of files and directories inside it. I have explained it in my [article on hard links][10]. You may refer to that if you want to know more on this topic.
|
||||
|
||||
### 5\. Path: Absolute and relative
|
||||
|
||||
The [directory structure in Linux resembles][11] the root of a tree. Everything starts at root and spreads out from there.
|
||||
|
||||
If you have to access a file or directory, you need to tell how to reach its location by providing its ‘path’. This path that is composed of directory names and separators (/). If a path starts with / (i.e., root), it is an absolute path otherwise it is a relative path.
|
||||
|
||||
![Path][12]
|
||||
|
||||
The absolute path starts from the root and can be easily referenced from anywhere in the system. The relative path depends upon your current location in the directory structure.
|
||||
|
||||
![absolute vs relative path][13]
|
||||
|
||||
If you are in the location /home/abhishek which has a directory named scripts containing a file my_script.sh and you want the path for this file, its absolute path will be:
|
||||
|
||||
```
|
||||
|
||||
/home/abhishek/scripts/my_script.sh
|
||||
|
||||
```
|
||||
|
||||
Its relative path will be:
|
||||
|
||||
```
|
||||
|
||||
scripts/my_script.sh
|
||||
|
||||
```
|
||||
|
||||
If you change the location, the absolute path of the file remains the same. However, the relative path changes because it is relative to your current path.
|
||||
|
||||
![Real examples of how relative path changes with location but absolute path remains the same][14]
|
||||
|
||||
### 6\. . and ..
|
||||
|
||||
You may often come across . and .. notation while using the Linux terminal.
|
||||
|
||||
Single dot (.) means the current directory.
|
||||
|
||||
Double dots (..) mean the parent directory (one directory above the current location).
|
||||
|
||||
You’ll often use the double dot (..) in relative path or for changing directory. Single dot (.) is also used in relative path but more importantly, you can use it in the commands for specifying the current locations.
|
||||
|
||||
![Use of . and ..][15]
|
||||
|
||||
### 7\. Understand the command structure
|
||||
|
||||
A typical Linux command consists of a command name followed by options and arguments.
|
||||
|
||||
```
|
||||
|
||||
command [options] argument
|
||||
|
||||
```
|
||||
|
||||
Option, as the name suggests, are optional. When used, they might change the output based on their properties.
|
||||
|
||||
For example, the cat command is used for viewing files. You can add the option -n and it will display line numbers as well.
|
||||
|
||||
Options are not standardized. Usually, they are used as single letter with single dash (-). They may also have two dashes (–) and a word.
|
||||
|
||||
Same options may have different meaning in a different command. If you use -n with head command, you specify the number of lines you want to see, not the lines with numbers.
|
||||
|
||||
![Same option -n has different use in cat and head commands][16]
|
||||
|
||||
In command documentations, _**if you see something between brackets ([]), it indicates that the contents of the bracket are optional.**_
|
||||
|
||||
Similarly, arguments are also not standardized. Some commands expect filenames as argument and some may expect directory name or a regular expression.
|
||||
|
||||
### 8\. Getting help
|
||||
|
||||
As you start using commands, you may remember some of the options of frequently used commands but it is simply not possible for you to remember all the options of any command.
|
||||
|
||||
Why? Because a single command may have more than ten or twenty options.
|
||||
|
||||
So, what do you do when you cannot recall all the options? You take help. And with help, I do not mean asking a question in It’s FOSS [Linux forum][17]. I ask to use the help option of the command.
|
||||
|
||||
Every standard Linux command has a quick help page that can be accessed with -h or –help or both.
|
||||
|
||||
```
|
||||
|
||||
command_name -h
|
||||
|
||||
```
|
||||
|
||||
It gives you a quick glimpse of the command syntax, common options with their meaning and in some cases, command examples.
|
||||
|
||||
![The help page of the cat command][18]
|
||||
|
||||
If you need more help, you can refer to the [manpage][19] i.e. manual of a command:
|
||||
|
||||
```
|
||||
|
||||
man command_name
|
||||
|
||||
```
|
||||
|
||||
It goes in all of details and could be overwhelming to read and understand. Alternatively, you can always search on the internet for ‘examples of xyz commands in Linux’.
|
||||
|
||||
### 9\. Linux is case-sensitive
|
||||
|
||||
Linux is case-sensitive. Everything you type in the terminal is case-sensitive. If you do not take that into account, you’ll often run into [bash: command not found][20] or file not found errors.
|
||||
|
||||
In the home directory, you have all the folders name starting with the upper case. If you have to switch to the Documents directory, you have to keep the first letter as D and not d. Otherwise, the terminal will complain.
|
||||
|
||||
![Linux is case sensitive][21]
|
||||
|
||||
You can have two separate files named file.txt and File.txt because for Linux, file and File are not the same.
|
||||
|
||||
### 10\. Running shell scripts
|
||||
|
||||
You can [run a shell script][22] by specifying the shell:
|
||||
|
||||
```
|
||||
|
||||
bash script.sh
|
||||
|
||||
```
|
||||
|
||||
Or you can execute the shell script like this:
|
||||
|
||||
```
|
||||
|
||||
./script.sh
|
||||
|
||||
```
|
||||
|
||||
The second one will only work when the file has execute permission. More on [Linux file permission here][23].
|
||||
|
||||
![Running bash script][24]
|
||||
|
||||
### 11\. Use tab completion instead of typing it all
|
||||
|
||||
The Ubuntu terminal is pre-configured with tab completion. This means that if you start writing something in the terminal and then hit tab, it tries to automatically complete it or provide options if there are more than one possible matches.
|
||||
|
||||
It works for both commands as well arguments and filenames.
|
||||
|
||||
![Tab completion example][25]
|
||||
|
||||
This saves a great ton of time because you don’t have to write everything completely.
|
||||
|
||||
### 12\. Ctrl+C and Ctrl+V is not for copy pasting in terminal
|
||||
|
||||
Ctrl+C, Ctrl+V might be the ‘universal’ keyboard shortcuts for copy paste but it doesn’t work in the Linux terminal.
|
||||
|
||||
Linux inherits a lot of stuff from UNIX and in UNIX, Ctrl+C was used for stopping a running process.
|
||||
|
||||
Since the Ctrl+C was already taken for stopping a command or process, it cannot be used for copy-paste anymore.
|
||||
|
||||
### 13\. You can surely copy paste in the terminal
|
||||
|
||||
Don’t worry. You can still [copy paste in the terminal][26]. Again, there is no fixed rule for the copy-paste keyboard shortcuts because it depends on the terminal application you are using or the configuration you have in place.
|
||||
|
||||
In Ubuntu terminal, the default keyboard shortcut for copy is Ctrl+Shift+C and for paste, it is Ctrl+Shift+V.
|
||||
|
||||
You can use Ctrl+C to copy text and commands from outside the terminal (like a web browser) and paste it using Ctrl+Shift+V. Similarly, you can highlight the text and use Ctrl+Shift+C to copy the text from the terminal and paste it to an editor or other applications using Ctrl+V.
|
||||
|
||||
### 14\. Avoid using Ctrl+S in the terminal
|
||||
|
||||
Another common mistake beginners do is to use the ‘universal’ Ctrl+S keyboard shortcut for save. If you use Ctrl+S in the terminal, your terminal ‘freezes’.
|
||||
|
||||
This is coming from the legacy computing where there was no scope of scrolling back up. Hence, if there were lots of output lines, Ctrl+S was used for stopping the screen so that text on the screen could be read.
|
||||
|
||||
You can unfreeze your terminal with Ctrl+Q. But again, avoid using Ctrl+S in the terminal.
|
||||
|
||||
### 15\. Pay attention to $ and <> in command examples
|
||||
|
||||
If you are referring to some online tutorial or documentation, you’ll see some command examples with text inside <>. This indicates that you need to replace the content along with < and > with a suitable value.
|
||||
|
||||
For example, if you see a command like this:
|
||||
|
||||
```
|
||||
|
||||
grep -i <search_term> <file_name>
|
||||
|
||||
```
|
||||
|
||||
You should replace the <search_term> and <file_name> with the respective actual values.
|
||||
|
||||
It is and indication that the command is just an example and you have to complete it with actual values.
|
||||
|
||||
Another thing to note here is that some tutorials show command examples that start with $ like this:
|
||||
|
||||
![dollar symbol at the beginning of command][27]
|
||||
|
||||
This is a way for them to indicate that it is command (not command output). But many new Linux users copy the preceding $ along with the actual command and when they paste it in the terminal, it throws error obviously.
|
||||
|
||||
So, when you are copying some command, don’t copy the $ if it is there at the beginning. You should also avoid copying random commands for random websites specially when you do not understand what it does.
|
||||
|
||||
Since you are reading about copying commands, when you see commands in multiple lines together, you should copy one line at a time and run them on by one:
|
||||
|
||||
![Avoid copying multiple commands together][28]
|
||||
|
||||
The next section tells you how to run multiple commands in one go.
|
||||
|
||||
### 16\. You can run multiple commands at once
|
||||
|
||||
You can [run multiple commands at once][29] without user intervention. You might have seen it already as an Ubuntu user in the form of this command:
|
||||
|
||||
```
|
||||
|
||||
sudo apt update && sudo apt upgrade
|
||||
|
||||
```
|
||||
|
||||
There are three different ways to combine commands in the terminal:
|
||||
|
||||
; | Command 1 ; Command 2 | Run command 1 first and then command 2
|
||||
---|---|---
|
||||
&& | Command 1 && Command 2 | Run command 2 only if command 1 ends sucessfully
|
||||
| |
|
||||
|
||||
### 17\. Stop a running Linux command
|
||||
|
||||
If a Linux command is running in the foreground, i.e., it is showing output or you cannot enter any other command, you can stop it using the Ctrl+C keys.
|
||||
|
||||
I discussed it previously. It comes from the legacy computing days of UNIX.
|
||||
|
||||
So, the next time you see a command like top or ping running continuously and you want the terminal control back, just use these two keys:
|
||||
|
||||
```
|
||||
|
||||
Ctrl+C
|
||||
|
||||
```
|
||||
|
||||
![Stop a running program in Linux with Ctrl+C][30]
|
||||
|
||||
### 18\. Clear the terminal
|
||||
|
||||
When I find that my screen is too cluttered with different kind of output, I clear the terminal screen before starting some other work. It just a habit but I find it helpful.
|
||||
|
||||
To clear the terminal, use the command
|
||||
|
||||
```
|
||||
|
||||
clear
|
||||
|
||||
```
|
||||
|
||||
You may also use Ctrl+L [terminal shortcut][31].
|
||||
|
||||
### 19\. Exiting the terminal
|
||||
|
||||
In a few cases, I have seen people closing the terminal application to exit the session. You could do that but the proper way to exit a terminal is to use the exit command:
|
||||
|
||||
```
|
||||
|
||||
exit
|
||||
|
||||
```
|
||||
|
||||
You may also use the keyboard shortcut Ctrl+D for Ubuntu terminal.
|
||||
|
||||
### Conclusion
|
||||
|
||||
There are so many additional stuff you can do in the terminal even if you are new to the entire terminal thing. You can:
|
||||
|
||||
* [Run funny Linux commands][32]
|
||||
* [Browse the internet in terminal][33]
|
||||
* [Play games in terminal][34]
|
||||
|
||||
|
||||
|
||||
And if you are looking for more, [take a look at][35] [these Linux command tips and use the terminal like a pro][35].
|
||||
|
||||
Honestly speaking, there is way too much to talk about. It is difficult to determine what should be considered as absolute basics and what should be left out. For example, I wanted to avoid including the information about paths because it needs detailed explanation but going too much in detail on a single could be overwhelming.
|
||||
|
||||
I have passed the stage where small things used to baffle me in the terminal. If you are new to the Linux terminal or if you remember the struggle from your initial Linux days, feel free to suggest any additions to the list. I might update the list with your input.
|
||||
|
||||
And if you learned something new, please do mention it in the comments. I would like to see if this article was worth the effort :)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/basic-terminal-tips-ubuntu/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/abhishek/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/12/ubuntu-terminal-basic-tips.png?resize=800%2C450&ssl=1
|
||||
[2]: https://itsfoss.com/linux-terminal-emulators/
|
||||
[3]: https://itsfoss.com/open-terminal-ubuntu/
|
||||
[4]: https://itsfoss.com/ubuntu-shortcuts/
|
||||
[5]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/12/linux-terminal-introduction.png?resize=800%2C450&ssl=1
|
||||
[6]: https://www.youtube.com/c/itsfoss?sub_confirmation=1
|
||||
[7]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/12/understanding-prompt-ubuntu-terminal.png?resize=800%2C346&ssl=1
|
||||
[8]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/12/learning-prompt-ubuntu-terminal.png?resize=800%2C346&ssl=1
|
||||
[9]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/12/directory-files-linux.png?resize=800%2C346&ssl=1
|
||||
[10]: https://linuxhandbook.com/hard-link/
|
||||
[11]: https://linuxhandbook.com/linux-directory-structure/
|
||||
[12]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/12/path-linux.webp?resize=800%2C250&ssl=1
|
||||
[13]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/12/absolute-vs-relative-path-linux.webp?resize=800%2C500&ssl=1
|
||||
[14]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/12/absolute-relative-path-real-examples-800x346.png?resize=800%2C346&ssl=1
|
||||
[15]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/12/use-of-single-and-double-dot-in-Linux.png?resize=732%2C272&ssl=1
|
||||
[16]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/12/command-structure-example.png?resize=732%2C462&ssl=1
|
||||
[17]: https://itsfoss.community/
|
||||
[18]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/12/cat-command-help-page.png?resize=732%2C614&ssl=1
|
||||
[19]: https://itsfoss.com/linux-man-page-guide/
|
||||
[20]: https://itsfoss.com/bash-command-not-found/
|
||||
[21]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/12/linux-is-case-sensitive.png?resize=732%2C253&ssl=1
|
||||
[22]: https://itsfoss.com/run-shell-script-linux/
|
||||
[23]: https://linuxhandbook.com/linux-file-permissions/
|
||||
[24]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/12/run-bash-script.png?resize=732%2C272&ssl=1
|
||||
[25]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/12/tab-completion-ubuntu.png?resize=732%2C272&ssl=1
|
||||
[26]: https://itsfoss.com/copy-paste-linux-terminal/
|
||||
[27]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/12/dollar-symbol-at-the-beginning-of-command.png?resize=765%2C256&ssl=1
|
||||
[28]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/12/avoid-copying-multiple-commands.png?resize=743%2C242&ssl=1
|
||||
[29]: https://itsfoss.com/run-multiple-commands-linux/
|
||||
[30]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/12/stop-running-program-linux.png?resize=800%2C385&ssl=1
|
||||
[31]: https://linuxhandbook.com/linux-shortcuts/
|
||||
[32]: https://itsfoss.com/funny-linux-commands/
|
||||
[33]: https://itsfoss.com/terminal-web-browsers/
|
||||
[34]: https://itsfoss.com/best-command-line-games-linux/
|
||||
[35]: https://itsfoss.com/linux-command-tricks/
|
@ -0,0 +1,377 @@
|
||||
[#]: subject: "19 Absolute Simple Things About Linux Terminal Every Ubuntu User Should Know"
|
||||
[#]: via: "https://itsfoss.com/basic-terminal-tips-ubuntu/"
|
||||
[#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/"
|
||||
[#]: collector: "lujun9972"
|
||||
[#]: translator: "wxy"
|
||||
[#]: reviewer: "wxy"
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
关于 Ubnutu Linux 终端的必知必会的 19 件超简单的事情
|
||||
======
|
||||
|
||||
终端常常让新用户感到害怕。然而,一旦你了解了它,你就会逐渐开始喜欢上它。好吧,这事往往发生在大多数 Linux 用户身上。
|
||||
|
||||
即使你使用 Ubuntu 作为桌面系统,你可能有时也要进入终端。新用户往往对很多事情毫无头绪,在这种情况下,一些基本的 Linux 命令的知识总是有帮助的,但这篇文章不是关于这个。
|
||||
|
||||
这篇文章的重点是解释关于使用终端的一些小的、基本的、经常被忽视的东西。这应该可以帮助 Ubuntu 桌面的新用户了解终端,并以更高的效率使用它。
|
||||
|
||||
![][1]
|
||||
|
||||
你看到的“<ruby>终端<rt>terminal</rt></ruby>”只是 [各种终端应用程序][2] 中的一个。毕竟终端只是一个 GUI 工具,它可以让你进入一个可以运行命令的 Shell 。
|
||||
|
||||
不同的终端应用程序(正确地应该被称为“<ruby>终端仿真器</rt></ruby>”)看起来有些稍微不同的功能和特点(如不同的键盘快捷键、颜色组合、字体等)。
|
||||
|
||||
本文特别关注 Ubuntu 的默认终端,它是 “GNOME 终端”的一个实现。
|
||||
|
||||
### 1、用键盘快捷方式打开终端
|
||||
|
||||
你可以 [在 Ubuntu 中打开终端][3],在系统菜单中寻找到它。然而,我最喜欢的方式是使用 [Ubuntu 中的键盘快捷键][4]:`Ctrl+Alt+T` 。
|
||||
|
||||
### 2、终端、Shell、提示符和命令行
|
||||
|
||||
在你看其他内容之前,你应该知道这些不同术语之间的区别,这些术语经常被(不正确地)互换使用。
|
||||
|
||||
![终端、提示符和命令][5]
|
||||
|
||||
“终端”是图形化的应用程序,默认情况下运行 Shell。
|
||||
|
||||
Shell 很难与终端分开进行可视化。终端运行着一个 Shell,在 Ubuntu 中通常默认为 Bash shell。和终端一样,也有各种 Shell。Bash 是其中最受欢迎的,也是大多数 Linux 发行版上的默认 Shell。
|
||||
|
||||
你输入的命令是由 Shell 解释的。通常人们认为他们在终端看到的屏幕就是 Shell。这适合理解这个概念。
|
||||
|
||||
“提示符”是你在输入命令的空格前看到的东西。对于提示符没有固定的标准。在一些旧的终端中,只是在你可以输入命令的地方有一个闪烁的光标而已。在 Ubuntu 终端中,提示符给了你一些信息,你会在本文后面的章节中看到这些信息的细节。
|
||||
|
||||
“命令行”不是 Linux 特有的东西。很多操作系统都有一个命令行界面。许多编程语言也都有命令行界面。它是一个术语,用来指你可以运行和执行命令的界面。
|
||||
|
||||
Luke Smith 的 [这个视频](https://www.youtube.com/embed/hMSByvFHOro) 用例子详细解释了它。言归正传,我在这里就不多说了。
|
||||
|
||||
### 3、了解提示符
|
||||
|
||||
你现在已经知道了。你在输入命令的空格前看到的东西叫做“提示符”。它是可配置的,在不同的发行版、终端应用程序和 Shell 中提示符看起来是不同的。
|
||||
|
||||
Ubuntu 终端对提示符进行了配置,让你看到一些东西。你可以一目了然地得到以下信息:
|
||||
|
||||
* 用户名
|
||||
* 主机名(计算机的名称)
|
||||
* 当前工作目录
|
||||
|
||||
还有一些你可能想知道的惯例。
|
||||
|
||||
提示符中的冒号(`:`)是一个分隔符,常用来区分主机名和当前位置。
|
||||
|
||||
波浪号(`~`)表示当前用户的主目录。
|
||||
|
||||
对于普通用户,提示符以美元(`$`)符号结束。对于 root 用户来说,它以英镑或哈希(`#`)符号结束。因此有一个笑话说,英镑比美元强。
|
||||
|
||||
![][7]
|
||||
|
||||
你是否注意到,当我切换到 root 用户时,命令提示符看起来不一样,没有任何颜色?这又一次提醒了我,提示符不是一个标准,是要明确配置的。对于普通用户来说,Ubuntu 对提示符的配置与 root 用户不同。
|
||||
|
||||
像这样的简单信息间接地帮助了我们。在一个多用户环境中,你可以很容易地弄清楚你现在使用的是哪个用户,以及它是否是 root 用户。其显示的路径位置也是有帮助的。
|
||||
|
||||
![][8]
|
||||
|
||||
### 4、目录和文件
|
||||
|
||||
在 Linux 中你听到最多的两个术语是目录和文件。
|
||||
|
||||
你可能知道什么是文件,但你可能会对“<ruby>目录<rt>directory</rt></ruby>”这个术语感到困惑。目录就是“<ruby>文件夹<rt>folder</rt></ruby>”。它把文件和文件夹放在里面。
|
||||
|
||||
你可以进入目录,但你不能进入文件。当然,你可以读取文件。
|
||||
|
||||
![][9]
|
||||
|
||||
你可以用“文件夹”这个词来表示目录,应该没有问题。然而,最好使用“目录”,因为你会在各种教程、文件等中看到引用这个词。你甚至会发现像 `rmdir`、`mkdir` 这样的命令,暗示它们是处理目录的。
|
||||
|
||||
补充说明:在 Linux 中,所有东西都是文件。甚至目录也是一种特殊的文件,里面有文件和目录的地址。我已经在我的关于 [硬链接的文章][10] 中解释了这一点。如果你想了解更多关于这个主题的信息,可以参考一下。
|
||||
|
||||
### 5、路径:绝对路径和相对路径
|
||||
|
||||
[Linux 中的目录结构][11] 类似于一棵树的根。所有的东西都从根部开始,并从那里向外扩散。
|
||||
|
||||
如果你要访问一个文件或目录,你需要通过提供它的“路径”来说明如何到达它的位置。这个路径是由目录名和分隔符(`/`)组成的。如果一个路径以 `/`(即根)开头,它就是一个绝对路径,否则就是一个相对路径。
|
||||
|
||||
![路径][12]
|
||||
|
||||
绝对路径从根开始,可以很容易地从系统的任何地方引用。相对路径则取决于你在目录结构中的当前位置。
|
||||
|
||||
![绝对路径与相对路径][13]
|
||||
|
||||
如果你在 `/home/abhishek` 这个位置,有一个名为 `scripts` 的目录,里面有一个文件 `my_script.sh`,你想知道这个文件的路径,它的绝对路径将是:
|
||||
|
||||
```
|
||||
/home/abhishek/scripts/my_script.sh
|
||||
```
|
||||
|
||||
它的相对路径将是:
|
||||
|
||||
```
|
||||
scripts/my_script.sh
|
||||
```
|
||||
|
||||
如果你改变所在位置,文件的绝对路径保持不变。但是,相对路径会改变,因为它是相对于你当前的路径而言的。
|
||||
|
||||
![相对路径随位置变化但绝对路径保持不变的真实例子][14]
|
||||
|
||||
### 6、 . 和 ..
|
||||
|
||||
在使用 Linux 终端时,你可能经常会遇到 `.`和 `..` 符号。
|
||||
|
||||
单点(`.`)表示当前目录。
|
||||
|
||||
双点(`..`)表示父目录(比当前位置高一个目录)。
|
||||
|
||||
你经常在相对路径中使用双点(`..`),或者用于改变目录。单点(`.`)也用于相对路径中,但更重要的是,你可以在指定当前位置的命令中使用它。
|
||||
|
||||
![ . 和 .. 的使用][15]
|
||||
|
||||
### 7、理解命令的结构
|
||||
|
||||
一个典型的 Linux 命令由一个命令名和选项及参数组成。
|
||||
|
||||
```
|
||||
命令名 [选项] 参数
|
||||
```
|
||||
|
||||
“选项”,顾名思义,是可选的。当使用时,它们可能会根据其属性来改变输出。
|
||||
|
||||
例如,`cat` 命令是用来查看文件的。你可以添加选项 `-n`,它也会显示行数。
|
||||
|
||||
选项不是标准化的。通常情况下,它们是由单字母和单破折号(`-`)组成的。它们也可能是两个破折号(`--`)和一个单词的形式。
|
||||
|
||||
同样的选项在不同的命令中可能有不同的含义。如果你在 `head` 命令中使用 `-n`,表明你想看行数,而不是行号。
|
||||
|
||||
![同样的选项 -n 在 cat 和 head 命令中有不同的用途][16]
|
||||
|
||||
在命令文档中,**如果你看到方括号(`[]`)之间有什么东西,它表示括号中的内容是可选的**。
|
||||
|
||||
同样地,“参数”也没有标准化。有些命令希望用文件名作为参数,有些则希望用目录名或正则表达式。
|
||||
|
||||
### 8、获得帮助
|
||||
|
||||
当你开始使用命令时,你可能会记住一些经常使用的命令的选项,但你根本不可能记住所有命令的所有选项。
|
||||
|
||||
为什么呢?因为一条命令可能有十多个或二十多个选项。
|
||||
|
||||
那么,当你无法记住所有的选项时,你该怎么办呢?你需要“帮助”。我所说的帮助,并不是指在 [Linux 论坛][17] 上提问。我指的是使用命令的帮助选项。
|
||||
|
||||
每个标准的 Linux 命令都有一个快速帮助页面,可以用 `-h` 或 `—help` 来访问。
|
||||
|
||||
```
|
||||
命令名 -h
|
||||
```
|
||||
|
||||
它可以让你快速了解命令的语法、常用选项及其含义,在某些情况下还有命令的例子。
|
||||
|
||||
![cat 命令的帮助页][18]
|
||||
|
||||
如果你需要更多的帮助,你可以参考 [手册页][19],即命令的手册。
|
||||
|
||||
```
|
||||
man 命令名
|
||||
```
|
||||
|
||||
它涉及到所有的细节,阅读和理解起来可能会让人难以承受。另外,你也可以在网上搜索 “Linux 中 xyz 命令的例子”。
|
||||
|
||||
### 9、Linux 是区分大小写的
|
||||
|
||||
Linux 是区分大小写的。你在终端中输入的所有东西都是区分大小写的。如果你不考虑这一点,你会经常遇到 “[bash: command not found][20]” 或 “file not found” 的错误。
|
||||
|
||||
在主目录中,你的所有文件夹名称以大写字母开头的。如果你要切换到 `Documents` 目录,你必须把第一个字母保持为 `D`,而不是 `d`。
|
||||
|
||||
![Linux 是区分大小写的][21]
|
||||
|
||||
你可以有两个分别名为 `file.txt` 和 `File.txt` 的文件,因为对于 Linux 来说,`file` 和 `File` 是不一样的。
|
||||
|
||||
### 10、运行 Shell 脚本
|
||||
|
||||
你可以通过指定 Shell 来 [运行一个 Shell 脚本][22]:
|
||||
|
||||
```
|
||||
bash script.sh
|
||||
```
|
||||
|
||||
或者你可以像这样执行 Shell 脚本。
|
||||
|
||||
```
|
||||
./script.sh
|
||||
```
|
||||
|
||||
第二种方法只有在文件有执行权限时才会起作用。更多关于 [Linux 文件权限参考这里][23]。
|
||||
|
||||
![运行bash脚本][24]
|
||||
|
||||
### 11、使用制表符补完而不是全部输入
|
||||
|
||||
Ubuntu 的终端已经预先配置了制表符补完功能。这意味着如果你开始在终端上输入,然后点击 `tab` ,它会尝试自动完成它,或者在有多个可能的匹配时提供选项。
|
||||
|
||||
它既适用于命令,也适用于参数和文件名。
|
||||
|
||||
![Tab 完成示例][25]
|
||||
|
||||
这可以节省大量的时间,因为你不需要把所有的东西都写完整。
|
||||
|
||||
### 12、Ctrl+C 和 Ctrl+V 不是用来在终端复制粘贴的。
|
||||
|
||||
`Ctrl+C`、`Ctrl+V` 可能是复制粘贴的“通用”键盘快捷键,但它在 Linux 终端中不行。
|
||||
|
||||
Linux 继承了 UNIX 的很多东西,在 UNIX 中,`Ctrl+C` 被用来停止一个正在运行的进程。
|
||||
|
||||
由于 `Ctrl+C` 已经被用于停止一个命令或进程,所以它不能再用于复制粘贴。
|
||||
|
||||
### 13、你当然可以在终端复制粘贴
|
||||
|
||||
别担心。你仍然可以 [在终端中复制粘贴][26]。同样,复制-粘贴的键盘快捷键没有固定的规则,因为它取决于你使用的终端程序或你的配置。
|
||||
|
||||
在 Ubuntu 终端中,复制的默认键盘快捷键是 `Ctrl+Shift+C`,粘贴则是 `Ctrl+Shift+V`。
|
||||
|
||||
你可以使用 `Ctrl+C` 从终端外(如网页浏览器)复制文本和命令,并使用 `Ctrl+Shift+V` 将其粘贴。同样,你可以高亮显示文本,用 `Ctrl+Shift+C` 从终端复制文本,用 `Ctrl+V` 粘贴到编辑器或其他应用程序。
|
||||
|
||||
### 14、避免在终端中使用 Ctrl+S
|
||||
|
||||
另一个初学者常犯的错误是使用“通用”的 `Ctrl+S` 键盘快捷键来保存。如果你在终端中使用 `Ctrl+S`,你的终端会被“冻结”。
|
||||
|
||||
这来自于传统的计算机,在那里没有向上滚动的滚动条。因此,如果有大量的输出行,`Ctrl+S` 被用来停止屏幕,以便可以阅读屏幕上的文字。
|
||||
|
||||
你可以用 `Ctrl+Q` 来解除终端的冻结。但还是要避免在终端中使用 `Ctrl+S`。
|
||||
|
||||
### 15、注意命令例子中的 $ 和 <>
|
||||
|
||||
如果你参考一些在线教程或文档,你会看到一些命令例子中的文本在 `<>` 内。这表明你需要用一个合适的值来替换与 `<` 和 `>` 一起的内容。
|
||||
|
||||
例如,如果你看到一个这样的命令:
|
||||
|
||||
```
|
||||
grep -i <搜索内容> <文件名>
|
||||
```
|
||||
|
||||
你应该把 `<搜索内容>` 和 `<文件名>` 换成各自的实际值。(LCTT 译注:不要输入 `<` 和 `>`)
|
||||
|
||||
这表明该命令只是一个例子,你必须用实际值来完成它。
|
||||
|
||||
这里需要注意的另一件事是,有些教程显示的命令例子是以 `$` 开头的,比如这样:
|
||||
|
||||
![命令开头的美元符号][27]
|
||||
|
||||
这是表明它们是命令(而不是命令输出)的一种方式。但是,许多新的 Linux 用户把前面的 `$` 和实际的命令一起复制,当他们把它粘贴到终端时,显然会出现错误。
|
||||
|
||||
所以,当你复制一些命令时,如果开头有 `$`,就不要复制它。你也应该避免复制随机网站的随机命令,特别是当你不了解它的作用时。
|
||||
|
||||
既然你正在阅读关于复制命令的文章,当你看到多行的命令在一起时,你应该一次复制一行,然后逐一运行。
|
||||
|
||||
![避免将多个命令复制在一起][28]
|
||||
|
||||
下一节将告诉你如何一次性运行多个命令。
|
||||
|
||||
### 16、你可以同时运行多个命令
|
||||
|
||||
你可以 [一次运行多个命令][29] 而不需要用户干预。作为 Ubuntu 用户,你可能已经在这个命令的形式中看到了它:
|
||||
|
||||
```
|
||||
sudo apt update && sudo apt upgrade
|
||||
```
|
||||
|
||||
在终端中,有三种不同的方法来组合命令:
|
||||
|
||||
`;` | `命令 1 ; 命令 2` | 先运行命令 1,再运行命令 2
|
||||
---|---|---
|
||||
`&&` | `命令 1 && 命令 2` | 只有命令 1 成功结束才运行命令 2
|
||||
`||` | `命令 1 || 命令 2` | 只有命令 1 失败时才运行命令 2
|
||||
|
||||
### 17、停止一个正在运行的 Linux 命令
|
||||
|
||||
如果一个 Linux 命令在前台运行,也就是说,它正在显示输出,或者说你不能输入任何其他命令,你可以用 `Ctrl+C` 键停止它。
|
||||
|
||||
我以前讨论过它。它来自于 UNIX 的传统计算时代。
|
||||
|
||||
所以,下次你看到像 `top` 或 `ping` 这样的命令在持续运行,而你想恢复终端控制,只需使用这两个键:`Ctrl+C`。
|
||||
|
||||
![在 Linux 中用 Ctrl+C 停止一个正在运行的程序][30]
|
||||
|
||||
### 18、清除终端
|
||||
|
||||
当我发现我的屏幕被不同类型的输出弄得太杂乱时,我会在开始其他工作之前清除终端屏幕。这只是一种习惯,但我发现它很有帮助。
|
||||
|
||||
要清除终端,请使用以下命令:
|
||||
|
||||
```
|
||||
clear
|
||||
```
|
||||
|
||||
你也可以使用 [终端快捷键][31] `Ctrl+L`。
|
||||
|
||||
### 19、退出终端
|
||||
|
||||
在少数情况下,我看到有人关闭终端程序来退出会话。你可以这样做,但退出终端的正确方法是使用退出命令:
|
||||
|
||||
```
|
||||
exit
|
||||
```
|
||||
|
||||
你也可以使用 Ubuntu 终端的键盘快捷键 `Ctrl+D`。
|
||||
|
||||
### 总结
|
||||
|
||||
即使你对终端完全陌生,你也可以在终端中做很多额外的事情。你可以:
|
||||
|
||||
* [运行有趣的 Linux 命令][32]
|
||||
* [在终端中浏览互联网][33]
|
||||
* [在终端中玩游戏][34]
|
||||
|
||||
如果你想了解更多,[看看这些 Linux 命令技巧,可以像专家一样使用终端][35]。
|
||||
|
||||
说实话,要谈的东西太多了。很难确定哪些应该被认为是绝对的基础知识,哪些应该被排除在外。例如,我想避免包括关于路径的信息,因为它需要详细的解释,但在一个地方讲得太详细可能会让人不知所措。
|
||||
|
||||
我已经过了在终端中的小东西曾经让我困惑的阶段。如果你是 Linux 终端的新手,或者你还记得你最初使用 Linux 时的挣扎,请随时提出建议对列表进行补充。我可能会根据你的意见更新这个列表。
|
||||
|
||||
如果你学到了新的东西,请在评论中提及。我想看看这篇文章是否值得一读 😁
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/basic-terminal-tips-ubuntu/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[wxy](https://github.com/wxy)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/abhishek/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/12/ubuntu-terminal-basic-tips.png?resize=800%2C450&ssl=1
|
||||
[2]: https://itsfoss.com/linux-terminal-emulators/
|
||||
[3]: https://itsfoss.com/open-terminal-ubuntu/
|
||||
[4]: https://itsfoss.com/ubuntu-shortcuts/
|
||||
[5]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/12/linux-terminal-introduction.png?resize=800%2C450&ssl=1
|
||||
[6]: https://www.youtube.com/c/itsfoss?sub_confirmation=1
|
||||
[7]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/12/understanding-prompt-ubuntu-terminal.png?resize=800%2C346&ssl=1
|
||||
[8]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/12/learning-prompt-ubuntu-terminal.png?resize=800%2C346&ssl=1
|
||||
[9]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/12/directory-files-linux.png?resize=800%2C346&ssl=1
|
||||
[10]: https://linuxhandbook.com/hard-link/
|
||||
[11]: https://linuxhandbook.com/linux-directory-structure/
|
||||
[12]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/12/path-linux.webp?resize=800%2C250&ssl=1
|
||||
[13]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/12/absolute-vs-relative-path-linux.webp?resize=800%2C500&ssl=1
|
||||
[14]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/12/absolute-relative-path-real-examples-800x346.png?resize=800%2C346&ssl=1
|
||||
[15]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/12/use-of-single-and-double-dot-in-Linux.png?resize=732%2C272&ssl=1
|
||||
[16]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/12/command-structure-example.png?resize=732%2C462&ssl=1
|
||||
[17]: https://itsfoss.community/
|
||||
[18]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/12/cat-command-help-page.png?resize=732%2C614&ssl=1
|
||||
[19]: https://itsfoss.com/linux-man-page-guide/
|
||||
[20]: https://itsfoss.com/bash-command-not-found/
|
||||
[21]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/12/linux-is-case-sensitive.png?resize=732%2C253&ssl=1
|
||||
[22]: https://itsfoss.com/run-shell-script-linux/
|
||||
[23]: https://linuxhandbook.com/linux-file-permissions/
|
||||
[24]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/12/run-bash-script.png?resize=732%2C272&ssl=1
|
||||
[25]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/12/tab-completion-ubuntu.png?resize=732%2C272&ssl=1
|
||||
[26]: https://itsfoss.com/copy-paste-linux-terminal/
|
||||
[27]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/12/dollar-symbol-at-the-beginning-of-command.png?resize=765%2C256&ssl=1
|
||||
[28]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/12/avoid-copying-multiple-commands.png?resize=743%2C242&ssl=1
|
||||
[29]: https://itsfoss.com/run-multiple-commands-linux/
|
||||
[30]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/12/stop-running-program-linux.png?resize=800%2C385&ssl=1
|
||||
[31]: https://linuxhandbook.com/linux-shortcuts/
|
||||
[32]: https://itsfoss.com/funny-linux-commands/
|
||||
[33]: https://itsfoss.com/terminal-web-browsers/
|
||||
[34]: https://itsfoss.com/best-command-line-games-linux/
|
||||
[35]: https://itsfoss.com/linux-command-tricks/
|
Loading…
Reference in New Issue
Block a user