Efficient Linux at the Command Line

Boost Your Command-Line Skills








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 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?”


  • 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.


  • 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 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