Skip to content
quboliu
Go back

Repost · Bilingual

The Art of Command Line命令行的艺术

Area: 工具与工作流Edit page
本站评估:阅读价值 68/100 · LLM 文字含量 0% · 无新增正文

阅读价值:覆盖面广,命令行实用技巧密集;本站基本照录已有中文版,部分工具和平台语境老化。

固定量尺的编辑评价;审阅于 2026-09-27。数值不表示价值倍数。

LLM 文字含量:0% · 无新增正文。正文为第三方原文,未发现本站新增的实质正文。分母为零,按本站展示约定放在 0% 点位;这不表示原作者未使用 LLM。

参与环节:未记录

统计范围:无本站新增正文;转载说明不计入分母

比例只针对本站新增自然语言正文;第三方原文、引文、代码和图示不计入。

项目的其他语言版本

Čeština ∙ Deutsch ∙ Ελληνικά ∙ English ∙ Español ∙ Français ∙ Indonesia ∙ Italiano ∙ 日本語 ∙ 한국어 ∙ polski ∙ Português ∙ Română ∙ Русский ∙ Slovenščina ∙ Українська ∙ 简体中文 ∙ 繁體中文

Note: I’m planning to revise this and looking for a new co-author to help with expanding this into a more comprehensive guide. While it’s very popular, it could be broader and a bit deeper. If you like to write and are close to being an expert on this material and willing to consider helping, please drop me a note at josh (0x40) holloway.com. –jlevy, Holloway. Thank you!

作者说明:我打算修订本文,想找一位合著者一起把它扩展成覆盖更广、讲解更深入的指南。如果你喜欢写作,对这方面也相当熟悉,愿意考虑加入,请写信给 josh (0x40) holloway.com。谢谢!——jlevy,Holloway

项目讨论区(Gitter)

curl -s 'https://raw.githubusercontent.com/jlevy/the-art-of-command-line/master/README.md' | egrep -o '\w+' | tr -d '`' | cowsay -W50

Fluency on the command line is a skill often neglected or considered arcane, but it improves your flexibility and productivity as an engineer in both obvious and subtle ways. This is a selection of notes and tips on using the command-line that we’ve found useful when working on Linux. Some tips are elementary, and some are fairly specific, sophisticated, or obscure. This page is not long, but if you can use and recall all the items here, you know a lot.

熟练使用命令行是一种常常被忽视,或被认为难以掌握的技能,但实际上,它会提高你作为工程师的灵活性以及生产力。本文是一份我在 Linux 上工作时,发现的一些命令行使用技巧的摘要。有些技巧非常基础,而另一些则相当复杂,甚至晦涩难懂。这篇文章并不长,但当你能够熟练掌握这里列出的所有技巧时,你就学会了很多关于命令行的东西了。

This work is the result of many authors and translators. Some of this originally appeared on Quora, but it has since moved to GitHub, where people more talented than the original author have made numerous improvements. Please submit a question if you have a question related to the command line. Please contribute if you see an error or something that could be better!

这篇文章是许多作者和译者共同的成果。 这里的部分内容 首次 出现 于 Quora, 但已经迁移到了 GitHub,并由众多高手做出了许多改进。 如果你在本文中发现了错误或者存在可以改善的地方,请贡献你的一份力量。

Meta前言

Scope:

涵盖范围:

Notes:

注意事项:

Basics基础

Everyday use日常使用

      find . -name '*.py' | xargs grep some_function
      cat hosts | xargs -I{} ssh root@{} hostname
      set -euo pipefail
      trap "echo 'error: Script failed: see failed command above'" ERR
      # do something in current dir
      (cd /some/other/dir && other-command)
      # continue in original dir
      diff /etc/hosts <(ssh somehost cat /etc/hosts)
{
      # Your code here
}
{
      # 在这里写代码
}
cat <<EOF
input
on multiple lines
EOF
      TCPKeepAlive=yes
      ServerAliveInterval=15
      ServerAliveCountMax=6
      Compression=yes
      ControlMaster auto
      ControlPath /tmp/%r@%h:%p
      ControlPersist yes
      stat -c '%A %a %n' /etc/timezone
>>> 2+3
5

Processing files and data文件及数据处理

      perl -pi.bak -e 's/old-string/new-string/g' my-files-*.txt
      # Full rename of filenames, directories, and contents foo -> bar:
      repren --full --preserve-case --from foo --to bar .
      # Recover backup files whatever.bak -> whatever:
      repren --renames --from '(.*)\.bak' --to '\1' *.bak
      # Same as above, using rename, if available:
      rename 's/\.bak$//' *.bak
      # 将文件、目录和内容全部重命名 foo -> bar:
      repren --full --preserve-case --from foo --to bar .
      # 还原所有备份文件 whatever.bak -> whatever:
      repren --renames --from '(.*)\.bak' --to '\1' *.bak
      # 用 rename 实现上述功能(若可用):
      rename 's/\.bak$//' *.bak
mkdir empty && rsync -r --delete empty/ some-dir && rmdir some-dir
      # Displays hex codes or actual names of characters (useful for debugging):
      uconv -f utf-8 -t utf-8 -x '::Any-Hex;' < input.txt
      uconv -f utf-8 -t utf-8 -x '::Any-Name;' < input.txt
      # Lowercase and removes all accents (by expanding and dropping them):
      uconv -f utf-8 -t utf-8 -x '::Any-Lower; ::Any-NFD; [:Nonspacing Mark:] >; ::Any-NFC;' < input.txt > output.txt
      uconv -f utf-8 -t utf-8 -x '::Any-Lower; ::Any-NFD; [:Nonspacing Mark:] >; ::Any-NFC; ' < input.txt > output.txt
   getfacl -R /some/path > permissions.txt
   setfacl --restore=permissions.txt

System debugging系统调试

One-liners单行脚本

A few examples of piecing together commands:

一些命令组合的例子:

      sort a b | uniq > c   # c is a union b
      sort a b | uniq -d > c   # c is a intersect b
      sort a b b | uniq -u > c   # c is set difference a - b
      sort a b | uniq > c   # c 是 a 并 b
      sort a b | uniq -d > c   # c 是 a 交 b
      sort a b b | uniq -u > c   # c 是 a - b
      diff <(jq --sort-keys . < file1.json) <(jq --sort-keys . < file2.json) | colordiff | less -R
      awk '{ x += $3 } END { print x }' myfile
      find . -type f -ls
      egrep -o 'acct_id=[0-9]+' access.log | cut -d= -f2 | sort | uniq -c | sort -rn
      function taocl() {
        curl -s https://raw.githubusercontent.com/jlevy/the-art-of-command-line/master/README.md |
          sed '/cowsay[.]png/d' |
          pandoc -f markdown -t html |
          xmlstarlet fo --html --dropdtd |
          xmlstarlet sel -t -v "(html/body/ul/li[count(p)>0])[$RANDOM mod last()+1]" |
          xmlstarlet unesc | fmt -80 | iconv -t US
      }
      function taocl() {
        curl -s https://raw.githubusercontent.com/jlevy/the-art-of-command-line/master/README-zh.md|
          pandoc -f markdown -t html |
          iconv -f 'utf-8' -t 'unicode' |
          xmlstarlet fo --html --dropdtd |
          xmlstarlet sel -t -v "(html/body/ul/li[count(p)>0])[$RANDOM mod last()+1]" |
          xmlstarlet unesc | fmt -80
      }

Obscure but useful冷门但有用

macOS only仅限 OS X 系统

These are items relevant only on macOS.

以下是仅限于 OS X 系统的技巧。

Windows only仅限 Windows 系统

These items are relevant only on Windows.

以下是仅限于 Windows 系统的技巧。

Ways to obtain Unix tools under Windows在 Windows 下获取 Unix 工具

Useful Windows command-line tools实用 Windows 命令行工具

Cygwin tips and tricksCygwin 技巧

More resources更多资源

Disclaimer免责声明

With the exception of very small tasks, code is written so others can read it. With power comes responsibility. The fact you can do something in Bash doesn’t necessarily mean you should! ;)

除去特别小的工作,你编写的代码应当方便他人阅读。能力往往伴随着责任,你 有能力 在 Bash 中玩一些奇技淫巧并不意味着你应该去做!;)

License授权条款

Creative Commons License


Edit page
Share this post:

Comments


Previous Post
Consistency Models|一致性模型
Next Post
What I learned from the book Designing Data-Intensive Applications?|我从《设计数据密集型应用》中学到了什么