Efficient Linux at the Command Line

Boost Your Command-Line Skills

Linux
Author

Shitao5

Published

2024-03-24

Modified

2024-04-05

Progress

Learning Progress: 23%.

Learning Source

1 Core Concepts

1.1 Combining Commands

  • The Linux command line is different. Instead of big applications with tons of features, Linux supplies thousands of small commands with very few features.

    Linux命令行不同于此。与具有大量功能的大型应用程序不同,Linux提供了数千个功能很少的小型命令。

  • Linux makes it easy to combine commands so their individual features work together to accomplish your goal. This way of working yields a very different mindset about computing. Instead of asking “Which app should I launch?” to achieve some result, the question becomes “Which commands should I combine?”

    Linux使得将命令组合在一起,使它们的各个功能共同完成你的目标变得很容易。这种工作方式产生了一种非常不同的计算思维。与其问“我应该启动哪个应用程序?”以实现某个结果,问题变成了“我应该组合哪些命令?”

  • The vertical bar (|) between the commands is the Linux pipe symbol. It connects the first command’s stdout to the next command’s stdin. Any command line containing pipes is called a pipeline. Commands generally are not aware that they’re part of a pipeline. ls believes it’s writing to the display, when in fact its output has been redirected to less. And less believes it’s reading from the keyboard when it’s actually reading the output of ls.

    命令之间的竖线(|)是Linux管道符号。它将第一个命令的标准输出连接到下一个命令的标准输入。任何包含管道的命令行都称为管道线。通常命令并不知道它们是管道线的一部分。ls认为它正在向显示器写入,而实际上它的输出已被重定向到lessless认为它正在从键盘读取,而实际上它正在读取ls的输出。

  • Unlike virtually every other Linux command, ls is aware of whether stdout is the screen or whether it’s been redirected (to a pipe or otherwise). The reason is user-friendliness. When stdout is the screen, ls arranges its output in multiple columns for convenient reading. When stdout is redirected, however, ls produces a single column.

    与几乎所有其他 Linux 命令不同,ls 能够感知到 stdout 是屏幕还是已被重定向(到管道或其他地方)。原因是为了用户友好性。当 stdout 是屏幕时,ls 将其输出排列成多列,方便阅读。然而,当 stdout 被重定向时,ls 则会生成单列。

1.2 Introducing the Shell

  • The wildcard is handled entirely by the shell.

    通配符完全由shell处理。

  • Shell pattern matching applies only to file and directory paths. It doesn’t work for usernames, hostnames, and other types of arguments that certain commands accept.

    Shell 模式匹配仅适用于文件和目录路径。它不适用于用户名、主机名和某些命令接受的其他类型的参数。

  • A variable is a name that stands in for a value. The shell also has names that stand in for commands. They’re called aliases.

    变量是代表值的名称。Shell 还有代表命令的名称,它们被称为别名。

    $ alias g=grep                 A command with no arguments
    $ alias ll="ls -l"             A command with arguments: quotes are required
  • Aliases take precedence over commands of the same name.

    别名优先于同名的命令。

  • Output redirection has a partner, input redirection, that redirects stdin to come from a file instead of the keyboard. Use the symbol < followed by a filename to redirect stdin.

    输出重定向有一个伴侣,即输入重定向,它将 stdin 重定向到来自文件而不是键盘。使用符号 < 后跟文件名来重定向 stdin。

  • A leading backslash before an alias escapes the alias, causing the shell to look for a command of the same name, ignoring any shadowing.

    在别名前加上反斜杠可以转义该别名,这会导致 shell 寻找同名的命令,忽略任何可能的覆盖。

    $ alias less="less -c"        Define an alias
    $ less myfile                 Run the alias, which invokes less -c
    $ \less myfile                Run the standard less command, not the alias
  • When the shell searches for a command by name, it checks if that name is an alias before checking the search path. That’s why an alias can shadow (take precedence over) a command of the same name.

    当 Shell 按名称搜索一个命令时,它会在检查搜索路径之前检查该名称是否为一个别名。这就是为什么一个别名可以遮蔽(优先于)一个同名的命令。

1.3 Rerunning Commands

  • The shell appends commands to the history, verbatim, unevaluated. The one exception to this rule is history expansion. Its expressions are always evaluated before they’re added to the command history.

    Shell 将命令原样、未经评估地添加到历史中。这条规则的唯一例外是历史扩展。它的表达式在添加到命令历史之前总是先被评估。

1.4 Cruising the Filesystems

Back to top