From e8672335288a6fc9dda749216508f3a7d3f25d68 Mon Sep 17 00:00:00 2001 From: Michael Barlow Date: Tue, 16 Jun 2015 10:00:09 +1000 Subject: [PATCH 01/53] Added a suggestion for GNU readline Added a suggestion to set the editing mode for bash to vi. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 3a094e2..334f220 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,8 @@ Scope: - In bash, use **ctrl-w** to delete the last word, and **ctrl-u** to delete the whole line. Use **alt-Left** and **alt-Right** to move by word, and **ctrl-k** to kill to the end of the line. See `man readline` for all the default keybindings in bash. There are a lot. For example **alt-.** cycles through previous arguments, and **alt-*** expands a glob. +- Alternatively, add `set editing-mode vi` to your `~/.bashrc` to enable vim-keybindings for bash (and any program using GNU readline). + - To go back to the previous working directory: `cd -` - If you are halfway through typing a command but change your mind, hit **alt-#** to add a `#` at the beginning and enter it as a comment (or use **ctrl-a**, **#**, **enter**). You can then return to it later via command history. From e0e6936584516eca8e201597b0169b56c84d8108 Mon Sep 17 00:00:00 2001 From: Corey Richardson Date: Mon, 15 Jun 2015 20:02:43 -0400 Subject: [PATCH 02/53] netstat is long deprecated on linux. Use ss instead. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3a094e2..8641cc4 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Scope: - Use `nohup` or `disown` if you want a background process to keep running forever. -- Check what processes are listening via `netstat -lntp`. +- Check what processes are listening via `ss -plat`. - See also `lsof` for open sockets and files. From f2761fe47cc9b4b4a3709dfe667a7272696569e2 Mon Sep 17 00:00:00 2001 From: Michael Barlow Date: Tue, 16 Jun 2015 12:13:48 +1000 Subject: [PATCH 03/53] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 334f220..47ffbb2 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Scope: - In bash, use **ctrl-w** to delete the last word, and **ctrl-u** to delete the whole line. Use **alt-Left** and **alt-Right** to move by word, and **ctrl-k** to kill to the end of the line. See `man readline` for all the default keybindings in bash. There are a lot. For example **alt-.** cycles through previous arguments, and **alt-*** expands a glob. -- Alternatively, add `set editing-mode vi` to your `~/.bashrc` to enable vim-keybindings for bash (and any program using GNU readline). +- Alternatively, add `set -o vi` to your `~/.bashrc` to enable vim-keybindings for bash. - To go back to the previous working directory: `cd -` From f16b0bfd940794ba5f37790800f104d16164a5bb Mon Sep 17 00:00:00 2001 From: Eduardo Rolim Date: Tue, 16 Jun 2015 11:45:58 -0300 Subject: [PATCH 04/53] Add new commands. Add wc simple description of main selectors and a usage; Add tee description; Add socat command; Add slurm command; Add rsync command; --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 0044b6a..23f5414 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,10 @@ Scope: - Know about `cut`, `paste`, and `join` to manipulate text files. Many people use `cut` but forget about `join`. +- Know about `wc` to count newlines (`-l`), characters (`-m`), words (`-w`) and bytes (`-c`). + +- Know about `tee` to copy from stdin to a file and also to stdout, as in `ls -al | tee file.txt`. + - Know that locale affects a lot of command line tools in subtle ways, including sorting order (collation) and performance. Most Linux installations will set `LANG` or other locale variables to a local setting like US English. But be aware sorting will change if you change locale. And know i18n routines can make sort or other commands run *many times* slower. In some situations (such as the set operations or uniqueness operations below) you can safely ignore slow i18n routines entirely and use traditional byte-based sort order, using `export LC_ALL=C`. - Know basic `awk` and `sed` for simple data munging. For example, summing all numbers in the third column of a text file: `awk '{ x += $3 } END { print x }'`. This is probably 3X faster and 3X shorter than equivalent Python. @@ -309,6 +313,10 @@ A few examples of piecing together commands: - `nc`: network debugging and data transfer +- `socat`: socket relay and tcp port forwarder (similar to `netcat`) + +- `slurm`: network trafic visualization + - `dd`: moving data between files or devices - `file`: identify type of a file @@ -345,6 +353,8 @@ A few examples of piecing together commands: - `cssh`: visual concurrent shell +- `rsync`: sync files and folders over SSH + - `wireshark` and `tshark`: packet capture and network debugging - `host` and `dig`: DNS lookups From 732f8956c54f5018c354ff66ad9fca583f3cd3b7 Mon Sep 17 00:00:00 2001 From: Chelsea Voss Date: Tue, 16 Jun 2015 09:50:31 -0700 Subject: [PATCH 05/53] Add tree --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e118df2..5ee90a9 100644 --- a/README.md +++ b/README.md @@ -385,6 +385,8 @@ A few examples of piecing together commands: - `fortune`, `ddate`, and `sl`: um, well, it depends on whether you consider steam locomotives and Zippy quotations "useful" +- `tree`: display directories and subdirectories as a nesting tree; like `ls` but recursive + ## More resources From d54f4f0fa4e8b151d7eed9cd291e06db8483fec6 Mon Sep 17 00:00:00 2001 From: Adam McDaniel Date: Tue, 16 Jun 2015 15:16:00 -0600 Subject: [PATCH 06/53] Add 'grep . *' oneliner trick for high-level scan of a directory --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index d247fe5..e97ed0c 100644 --- a/README.md +++ b/README.md @@ -238,6 +238,12 @@ A few examples of piecing together commands: cat a b b | sort | uniq -u > c # c is set difference a - b ``` +- Use `grep . *` to visually examine all contents of all files in a directory. Useful for directories filled with config settings, like /sys /proc /etc. +```sh + cd /proc/sys/net/ipv4 + grep . * +``` + - Summing all numbers in the third column of a text file (this is probably 3X faster and 3X less code than equivalent Python): ```sh awk '{ x += $3 } END { print x }' myfile From 6273401328dc4040d2e308ae9623a7c033a982e8 Mon Sep 17 00:00:00 2001 From: Colas Date: Wed, 17 Jun 2015 18:31:30 +0200 Subject: [PATCH 07/53] Added "ctrl-l" command that I use everyday --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 5840e2f..d035772 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,8 @@ Scope: - In Bash, use **ctrl-r** to search through command history. +- In Bash, use **ctrl-l** to clear the screen. + - In Bash, use **ctrl-w** to delete the last word, and **ctrl-u** to delete the whole line. Use **alt-b** and **alt-f** to move by word, and **ctrl-k** to kill to the end of the line. See `man readline` for all the default keybindings in Bash. There are a lot. For example **alt-.** cycles through previous arguments, and **alt-*** expands a glob. - To go back to the previous working directory: `cd -` From 4bafc6d011b5e6330f2a2fbf2025f56b3d8def44 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Wed, 17 Jun 2015 09:54:33 -0700 Subject: [PATCH 08/53] Brevity for #58 --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index d035772..47772a4 100644 --- a/README.md +++ b/README.md @@ -55,9 +55,7 @@ Scope: - In Bash, use **ctrl-r** to search through command history. -- In Bash, use **ctrl-l** to clear the screen. - -- In Bash, use **ctrl-w** to delete the last word, and **ctrl-u** to delete the whole line. Use **alt-b** and **alt-f** to move by word, and **ctrl-k** to kill to the end of the line. See `man readline` for all the default keybindings in Bash. There are a lot. For example **alt-.** cycles through previous arguments, and **alt-*** expands a glob. +- In Bash, use **ctrl-w** to delete the last word, and **ctrl-u** to delete the whole line. Use **alt-b** and **alt-f** to move by word, **ctrl-k** to kill to the end of the line, **ctrl-l** to clear the screen. See `man readline` for all the default keybindings in Bash. There are a lot. For example **alt-.** cycles through previous arguments, and **alt-*** expands a glob. - To go back to the previous working directory: `cd -` From adeff6dafc725028951ee4fa5e31852d3952f012 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Wed, 17 Jun 2015 10:42:26 -0700 Subject: [PATCH 09/53] Clarify philosophy. Mention tab. Fixes #59. --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 47772a4..0f3249f 100644 --- a/README.md +++ b/README.md @@ -18,14 +18,14 @@ Much of this [originally](http://www.quora.com/What-are-some-lesser-known-but-useful-Unix-commands) [appeared](http://www.quora.com/What-are-the-most-useful-Swiss-army-knife-one-liners-on-Unix) on [Quora](http://www.quora.com/What-are-some-time-saving-tips-that-every-Linux-user-should-know), -but given the interest there, it seems it's worth using Github, where people more talented than I can readily suggest improvements. If you see an error or something that could be better, please submit an issue or PR! +but given the interest there, it seems it's worth using Github, where people more talented than I can readily suggest improvements. If you see an error or something that could be better, please submit an issue or PR! (But only after reviewing the scope/philosophy bullets below.) Scope: -- The goals are breadth and brevity. Every tip is essential in some situation or significantly saves time over alternatives. +- The goals are breadth (everything important), specificity (give concrete examples of the most common case), and brevity (avoid things that aren't essential or digressions you can easily look up elsewhere). Every tip is essential in some situation or significantly saves time over alternatives. - This is written for Linux. Many but not all items apply equally to MacOS (or even Cygwin). - The focus is on interactive Bash, though many tips apply to other shells and to general Bash scripting. -- Descriptions are intentionally minimal, with the expectation you'll use `man`, `apt-get`/`yum`/`dnf` to install, and Google for more background. +- To keep this to one page, include content by reference whenever possible. Descriptions are intentionally minimal, with the expectation you'll use `man`, `apt-get`/`yum`/`dnf` to install, and Google for more background. ## Basics @@ -53,7 +53,7 @@ Scope: ## Everyday use -- In Bash, use **ctrl-r** to search through command history. +- In Bash, use **Tab** to complete file names and **ctrl-r** to search through command history. - In Bash, use **ctrl-w** to delete the last word, and **ctrl-u** to delete the whole line. Use **alt-b** and **alt-f** to move by word, **ctrl-k** to kill to the end of the line, **ctrl-l** to clear the screen. See `man readline` for all the default keybindings in Bash. There are a lot. For example **alt-.** cycles through previous arguments, and **alt-*** expands a glob. From 06773e4bf86859900d0681763f645b5d42facd5f Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Wed, 17 Jun 2015 10:45:42 -0700 Subject: [PATCH 10/53] Fix to tab note #59. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0f3249f..4edd958 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Scope: ## Everyday use -- In Bash, use **Tab** to complete file names and **ctrl-r** to search through command history. +- In Bash, use **Tab** to complete arguments and **ctrl-r** to search through command history. - In Bash, use **ctrl-w** to delete the last word, and **ctrl-u** to delete the whole line. Use **alt-b** and **alt-f** to move by word, **ctrl-k** to kill to the end of the line, **ctrl-l** to clear the screen. See `man readline` for all the default keybindings in Bash. There are a lot. For example **alt-.** cycles through previous arguments, and **alt-*** expands a glob. From b4900c408e0ae79d6a92705e00ac335b96ed1565 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Wed, 17 Jun 2015 15:30:51 -0700 Subject: [PATCH 11/53] Brevity on #51 --- README.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 76cedc9..d1dd29f 100644 --- a/README.md +++ b/README.md @@ -242,11 +242,8 @@ A few examples of piecing together commands: cat a b b | sort | uniq -u > c # c is set difference a - b ``` -- Use `grep . *` to visually examine all contents of all files in a directory. Useful for directories filled with config settings, like /sys /proc /etc. -```sh - cd /proc/sys/net/ipv4 - grep . * -``` +- Use `grep . *` to visually examine all contents of all files in a directory, e.g. for directories filled with config settings, like `/sys`, `/proc`, `/etc`. + - Summing all numbers in the third column of a text file (this is probably 3X faster and 3X less code than equivalent Python): ```sh From f65245236ff0b58b88ae44b6793523933d75c884 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Wed, 17 Jun 2015 15:50:38 -0700 Subject: [PATCH 12/53] Use && within example. Fixes #57 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d1dd29f..8c61a4b 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ Scope: - In Bash scripts, subshells (written with parentheses) are convenient ways to group commands. A common example is to temporarily move to a different working directory, e.g. ```bash # do something in current dir - (cd /some/other/dir; other-command) + (cd /some/other/dir && other-command) # continue in original dir ``` From a44c628ad2132ac0de89f15e8070cfb753e6444d Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Wed, 17 Jun 2015 20:20:18 -0700 Subject: [PATCH 13/53] Add note about sort -u. Addresses #27 and #63 while preserving clarity. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8c61a4b..0864b97 100644 --- a/README.md +++ b/README.md @@ -235,7 +235,7 @@ Scope: A few examples of piecing together commands: -- It is remarkably helpful sometimes that you can do set intersection, union, and difference of text files via `sort`/`uniq`. Suppose `a` and `b` are text files that are already uniqued. This is fast, and works on files of arbitrary size, up to many gigabytes. (Sort is not limited by memory, though you may need to use the `-T` option if `/tmp` is on a small root partition.) See also the note about `LC_ALL` above. +- It is remarkably helpful sometimes that you can do set intersection, union, and difference of text files via `sort`/`uniq`. Suppose `a` and `b` are text files that are already uniqued. This is fast, and works on files of arbitrary size, up to many gigabytes. (Sort is not limited by memory, though you may need to use the `-T` option if `/tmp` is on a small root partition.) See also the note about `LC_ALL` above and `sort`'s `-u` option (left out for clarity below). ```sh cat a b | sort | uniq > c # c is a union b cat a b | sort | uniq -d > c # c is a intersect b From fd55b34e31f4a4fa253fb5de27f73820d24674b6 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Wed, 17 Jun 2015 20:34:09 -0700 Subject: [PATCH 14/53] Update README.md --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0864b97..238ac9e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # The Art of Command Line +- [Meta](#meta) - [Basics](#basics) - [Everyday use](#everyday-use) - [Processing files and data](#processing-files-and-data) @@ -20,12 +21,19 @@ Much of this on [Quora](http://www.quora.com/What-are-some-time-saving-tips-that-every-Linux-user-should-know), but given the interest there, it seems it's worth using Github, where people more talented than I can readily suggest improvements. If you see an error or something that could be better, please submit an issue or PR! (But only after reviewing the scope/philosophy bullets below.) +## Meta + Scope: - The goals are breadth (everything important), specificity (give concrete examples of the most common case), and brevity (avoid things that aren't essential or digressions you can easily look up elsewhere). Every tip is essential in some situation or significantly saves time over alternatives. - This is written for Linux. Many but not all items apply equally to MacOS (or even Cygwin). - The focus is on interactive Bash, though many tips apply to other shells and to general Bash scripting. -- To keep this to one page, include content by reference whenever possible. Descriptions are intentionally minimal, with the expectation you'll use `man`, `apt-get`/`yum`/`dnf` to install, and Google for more background. +- To keep this to one page, include content by reference whenever possible. +- +Notes: + +- This is for both beginners and the experienced. In both cases, you're smart enough to look up more detail elsewhere once you know the idea or command to Google. +- Check out [Explainshell](http://explainshell.com/) to get a helpful breakdown of what the command, pipes, and options do. ## Basics From 30946a293bf560e432c56b49493ab765e59ca377 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Wed, 17 Jun 2015 20:38:59 -0700 Subject: [PATCH 15/53] New meta section and explainshell link. Fixes #54. --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 238ac9e..04197c5 100644 --- a/README.md +++ b/README.md @@ -19,20 +19,20 @@ Much of this [originally](http://www.quora.com/What-are-some-lesser-known-but-useful-Unix-commands) [appeared](http://www.quora.com/What-are-the-most-useful-Swiss-army-knife-one-liners-on-Unix) on [Quora](http://www.quora.com/What-are-some-time-saving-tips-that-every-Linux-user-should-know), -but given the interest there, it seems it's worth using Github, where people more talented than I can readily suggest improvements. If you see an error or something that could be better, please submit an issue or PR! (But only after reviewing the scope/philosophy bullets below.) +but given the interest there, it seems it's worth using Github, where people more talented than I can readily suggest improvements. If you see an error or something that could be better, please submit an issue or PR! (Of course please review the meta section and existing PRs/issues first.) + ## Meta Scope: -- The goals are breadth (everything important), specificity (give concrete examples of the most common case), and brevity (avoid things that aren't essential or digressions you can easily look up elsewhere). Every tip is essential in some situation or significantly saves time over alternatives. +- This guide is both for beginners and the experienced. The goals are *breadth* (everything important), *specificity* (give concrete examples of the most common case), and *brevity* (avoid things that aren't essential or digressions you can easily look up elsewhere). Every tip is essential in some situation or significantly saves time over alternatives. - This is written for Linux. Many but not all items apply equally to MacOS (or even Cygwin). - The focus is on interactive Bash, though many tips apply to other shells and to general Bash scripting. -- To keep this to one page, include content by reference whenever possible. -- + Notes: -- This is for both beginners and the experienced. In both cases, you're smart enough to look up more detail elsewhere once you know the idea or command to Google. +- To keep this to one page, content is implicitly included by reference. You're smart enough to look up more detail elsewhere once you know the idea or command to Google. - Check out [Explainshell](http://explainshell.com/) to get a helpful breakdown of what the command, pipes, and options do. From 8e14e43ef2b2dba93a80fed552b0a3e2f89acad3 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Wed, 17 Jun 2015 21:16:23 -0700 Subject: [PATCH 16/53] Missed in last commit. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 04197c5..43a1393 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,8 @@ Scope: Notes: -- To keep this to one page, content is implicitly included by reference. You're smart enough to look up more detail elsewhere once you know the idea or command to Google. -- Check out [Explainshell](http://explainshell.com/) to get a helpful breakdown of what the command, pipes, and options do. +- To keep this to one page, content is implicitly included by reference. You're smart enough to look up more detail elsewhere once you know the idea or command to Google. Use `apt-get`/`yum`/`dnf`/`brew` (as appropriate) to install new programs. +- Use [Explainshell](http://explainshell.com/) to get a helpful breakdown of what commands, options, pipes etc. do. ## Basics From 288eaa8b1b606710e90ecd6565e0625cc8c09e0a Mon Sep 17 00:00:00 2001 From: kesu Date: Thu, 18 Jun 2015 17:15:18 +0000 Subject: [PATCH 17/53] Added Python 3 version of simple web server --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 43a1393..fccdf72 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ Notes: - For interaction with files based on the output of another command (like `git`), use `fpp` ([PathPicker](https://github.com/facebook/PathPicker)). - For a simple web server for all files in the current directory (and subdirs), available to anyone on your network, use: -`python -m SimpleHTTPServer 7777` (for port 7777 and Python 2). +`python -m SimpleHTTPServer 7777` (for port 7777 and Python 2) and `python -m http.server 7777` (for port 7777 and Python 3). ## Processing files and data From 54d5c0260311602c5383b6b90c52af3f21b26def Mon Sep 17 00:00:00 2001 From: vitzli Date: Fri, 19 Jun 2015 17:22:31 +0600 Subject: [PATCH 18/53] Add units program --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 43a1393..946b5f0 100644 --- a/README.md +++ b/README.md @@ -402,6 +402,7 @@ A few examples of piecing together commands: - `fortune`, `ddate`, and `sl`: um, well, it depends on whether you consider steam locomotives and Zippy quotations "useful" +- `units`: unit conversion and calculation program, converts furlongs per fortnights to twips per blinks, `/usr/share/units/definitions.units` worth reading by itself. ## More resources From 1abac1882ff60f30a61247c198be47381adcfba3 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Fri, 19 Jun 2015 22:59:51 -0700 Subject: [PATCH 19/53] List netstat and ss. Update #10 to list both. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f5e0a24..b38a5b9 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ Notes: - Use `nohup` or `disown` if you want a background process to keep running forever. -- Check what processes are listening via `ss -plat`. +- Check what processes are listening via `netstat -lntp` or `ss -plat` (for TCP; add `-u` for for UDP). - See also `lsof` for open sockets and files. From 345a791b0db3384b825329bc7b07ac33a099b197 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Fri, 19 Jun 2015 23:06:28 -0700 Subject: [PATCH 20/53] Include fdisk and mkfs. As suggested in #10. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b38a5b9..e9e1241 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Notes: - Know `ssh`, and the basics of passwordless authentication, via `ssh-agent`, `ssh-add`, etc. -- Basic file management: `ls` and `ls -l` (in particular, learn what every column in `ls -l` means), `less`, `head`, `tail` and `tail -f` (or even better, `less +F`), `ln` and `ln -s` (learn the differences and advantages of hard versus soft links), `chown`, `chmod`, `du` (for a quick summary of disk usage: `du -sk *`), `df`, `mount`. +- Basic file management: `ls` and `ls -l` (in particular, learn what every column in `ls -l` means), `less`, `head`, `tail` and `tail -f` (or even better, `less +F`), `ln` and `ln -s` (learn the differences and advantages of hard versus soft links), `chown`, `chmod`, `du` (for a quick summary of disk usage: `du -sk *`). For filesystem management, `df`, `mount`, `fdisk`, `mkfs`. - Basic network management: `ip` or `ifconfig`, `dig`. From b4b45fcd3d43b3e058b82fa814396323eb17879b Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Fri, 19 Jun 2015 23:14:53 -0700 Subject: [PATCH 21/53] Remove screen from obscure section. As #72 mentions, neither are really obscure, and are covered earlier in the doc anyway. Fixes #72. --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index e9e1241..c206f97 100644 --- a/README.md +++ b/README.md @@ -294,8 +294,6 @@ A few examples of piecing together commands: - `m4`: simple macro processor -- `screen`: powerful terminal multiplexing and session persistence - - `yes`: print a string a lot - `cal`: nice calendar From 23c653b99f2d0eb1fa2893e9007a33a4d8265005 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Fri, 19 Jun 2015 23:48:44 -0700 Subject: [PATCH 22/53] General mention of man and help. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 468986f..a902971 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,8 @@ Notes: - Learn at least one text-based editor well. Ideally Vim (`vi`), as there's really no competition for random editing in a terminal (even if you use Emacs, a big IDE, or a modern hipster editor most of the time). +- Know how to read documentation with `man` (for the inquisitive, `man man` lists the section numbers, e.g. 1 is "regular" commands, 5 is files/conventions, and 8 are for administration). Know that some commands are not executables, but Bash builtins, and that you can get help on them with `help` and `help -d`. + - Learn about redirection of output and input using `>` and `<` and pipes using `|`. Learn about stdout and stderr. - Learn about file glob expansion with `*` (and perhaps `?` and `{`...`}`) and quoting and the difference between double `"` and single `'` quotes. (See more on variable expansion below.) From 92748231fe1c60eaa816cb5a35134841648e66d7 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Fri, 19 Jun 2015 23:52:51 -0700 Subject: [PATCH 23/53] Better sequencing. --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3546069..be36215 100644 --- a/README.md +++ b/README.md @@ -330,12 +330,12 @@ A few examples of piecing together commands: - `nc`: network debugging and data transfer -- `ngrep`: grep for the network layer - - `dd`: moving data between files or devices - `file`: identify type of a file +- `tree`: display directories and subdirectories as a nesting tree; like `ls` but recursive + - `stat`: file info - `tac`: print files in reverse @@ -350,7 +350,7 @@ A few examples of piecing together commands: - `tr`: character translation or manipulation -- `iconv `or uconv: conversion for text encodings +- `iconv` or `uconv`: conversion for text encodings - `split `and `csplit`: splitting files @@ -370,6 +370,8 @@ A few examples of piecing together commands: - `wireshark` and `tshark`: packet capture and network debugging +- `ngrep`: grep for the network layer + - `host` and `dig`: DNS lookups - `lsof`: process file descriptor and socket info @@ -404,8 +406,6 @@ A few examples of piecing together commands: - `fortune`, `ddate`, and `sl`: um, well, it depends on whether you consider steam locomotives and Zippy quotations "useful" -- `tree`: display directories and subdirectories as a nesting tree; like `ls` but recursive - ## More resources From 68cbe90ff14ddf4c5e569cbdf7e4653f94dc3753 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Sat, 20 Jun 2015 00:10:40 -0700 Subject: [PATCH 24/53] Note about history and history abbreviations. Fixes #30. Fixes #39. --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index be36215..1034022 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,9 @@ Notes: - In Bash, use **ctrl-w** to delete the last word, and **ctrl-u** to delete the whole line. Use **alt-b** and **alt-f** to move by word, **ctrl-k** to kill to the end of the line, **ctrl-l** to clear the screen. See `man readline` for all the default keybindings in Bash. There are a lot. For example **alt-.** cycles through previous arguments, and **alt-*** expands a glob. -- Alternatively, add `set -o vi` to your `~/.bashrc` to enable vim-keybindings for bash. +- Alternatively, if you love vi-style key-bindings, use `set -o vi`. + +- To see recent commands, `history`. There are also many abbreviations such as `!$` (last argument) and `!!` last command, though these are often easily replaced with **ctrl-r* and **alt-.**. - To go back to the previous working directory: `cd -` From d6928402d9a4895396ce1809c06c7f2ff255cd96 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Sat, 20 Jun 2015 00:19:56 -0700 Subject: [PATCH 25/53] Apropos. Fixes #65. Fixes #73. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1034022..70a83d2 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Notes: - Learn at least one text-based editor well. Ideally Vim (`vi`), as there's really no competition for random editing in a terminal (even if you use Emacs, a big IDE, or a modern hipster editor most of the time). -- Know how to read documentation with `man` (for the inquisitive, `man man` lists the section numbers, e.g. 1 is "regular" commands, 5 is files/conventions, and 8 are for administration). Know that some commands are not executables, but Bash builtins, and that you can get help on them with `help` and `help -d`. +- Know how to read documentation with `man` (for the inquisitive, `man man` lists the section numbers, e.g. 1 is "regular" commands, 5 is files/conventions, and 8 are for administration). Find man pages with `apropos`. Know that some commands are not executables, but Bash builtins, and that you can get help on them with `help` and `help -d`. - Learn about redirection of output and input using `>` and `<` and pipes using `|`. Learn about stdout and stderr. From 2c6fc6677b146076d65e803f45cf63d266619c28 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Sat, 20 Jun 2015 00:40:23 -0700 Subject: [PATCH 26/53] Clarify ctrl-u. Fixes #25. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 70a83d2..05471df 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ Notes: - In Bash, use **Tab** to complete arguments and **ctrl-r** to search through command history. -- In Bash, use **ctrl-w** to delete the last word, and **ctrl-u** to delete the whole line. Use **alt-b** and **alt-f** to move by word, **ctrl-k** to kill to the end of the line, **ctrl-l** to clear the screen. See `man readline` for all the default keybindings in Bash. There are a lot. For example **alt-.** cycles through previous arguments, and **alt-*** expands a glob. +- In Bash, use **ctrl-w** to delete the last word, and **ctrl-u** to delete all the way back to the start of the line. Use **alt-b** and **alt-f** to move by word, **ctrl-k** to kill to the end of the line, **ctrl-l** to clear the screen. See `man readline` for all the default keybindings in Bash. There are a lot. For example **alt-.** cycles through previous arguments, and **alt-*** expands a glob. - Alternatively, if you love vi-style key-bindings, use `set -o vi`. From e6e0b70bd733e448fdda0d6bffb13704e7f2dfb2 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Sat, 20 Jun 2015 09:12:15 -0700 Subject: [PATCH 27/53] Tweak units contribution. Update to #80 --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 093fe9a..7836a3c 100644 --- a/README.md +++ b/README.md @@ -364,6 +364,8 @@ A few examples of piecing together commands: - `split `and `csplit`: splitting files +- `units`: unit conversions and calculations; converts furlongs per fortnight to twips per blink (see also `/usr/share/units/definitions.units`) + - `7z`: high-ratio file compression - `ldd`: dynamic library info @@ -418,7 +420,7 @@ A few examples of piecing together commands: - `fortune`, `ddate`, and `sl`: um, well, it depends on whether you consider steam locomotives and Zippy quotations "useful" -- `units`: unit conversion and calculation program, converts furlongs per fortnights to twips per blinks, `/usr/share/units/definitions.units` worth reading by itself. + ## More resources From f1a99aa976fdd032c916a12c98b42da385414069 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Sat, 20 Jun 2015 09:53:07 -0700 Subject: [PATCH 28/53] Add wiki-style license. --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7836a3c..1e02c48 100644 --- a/README.md +++ b/README.md @@ -421,7 +421,6 @@ A few examples of piecing together commands: - `fortune`, `ddate`, and `sl`: um, well, it depends on whether you consider steam locomotives and Zippy quotations "useful" - ## More resources - [awesome-shell](https://github.com/alebcay/awesome-shell): A curated list of shell tools and resources. @@ -431,3 +430,10 @@ A few examples of piecing together commands: ## 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! ;) + + +## License + +[![Creative Commons License](https://i.creativecommons.org/l/by-sa/4.0/88x31.png)](http://creativecommons.org/licenses/by-sa/4.0/) + +This work is licensed under a [Creative Commons Attribution-ShareAlike 4.0 International Licene](http://creativecommons.org/licenses/by-sa/4.0/). From eab7f29c2852196b1e0bf60404cab13dbcbf0a78 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Sat, 20 Jun 2015 10:10:06 -0700 Subject: [PATCH 29/53] Add diff, patch, and diffstat. Fixes #36. --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 1e02c48..8097227 100644 --- a/README.md +++ b/README.md @@ -196,10 +196,14 @@ Notes: - If you ever need to write a tab literal in a command line in Bash (e.g. for the -t argument to sort), press **ctrl-v** **[Tab]** or write `$'\t'` (the latter is better as you can copy/paste it). +- The standard tools for patching source code are `diff` and `patch`. See also `diffstat` for summary statistics of a diff. Note `diff -r` works for entire directories. Use `diff -r tree1 tree2 | diffstat` for a summary of changes. + - For binary files, use `hd` for simple hex dumps and `bvi` for binary editing. - Also for binary files, `strings` (plus `grep`, etc.) lets you find bits of text. +- For binary diffs (delta compression), use `xdelta3`. + - To convert text encodings, try `iconv`. Or `uconv` for more advanced use; it supports some advanced Unicode things. For example, this command lowercases and removes all accents (by expanding and dropping them): ```sh uconv -f utf-8 -t utf-8 -x '::Any-Lower; ::Any-NFD; [:Nonspacing Mark:] >; ::Any-NFC; ' < input.txt > output.txt From 80ba0c126dbc9338176cb6c0e0362ad9a7228f26 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Sat, 20 Jun 2015 12:33:11 -0700 Subject: [PATCH 30/53] Clarify scope to include non-standard packages. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8097227..edad6fd 100644 --- a/README.md +++ b/README.md @@ -29,10 +29,11 @@ Scope: - This guide is both for beginners and the experienced. The goals are *breadth* (everything important), *specificity* (give concrete examples of the most common case), and *brevity* (avoid things that aren't essential or digressions you can easily look up elsewhere). Every tip is essential in some situation or significantly saves time over alternatives. - This is written for Linux. Many but not all items apply equally to MacOS (or even Cygwin). - The focus is on interactive Bash, though many tips apply to other shells and to general Bash scripting. +- It includes both "standard" Unix commands as well as ones that require special package installs -- so long as they are important enough to merit inclusion. Notes: -- To keep this to one page, content is implicitly included by reference. You're smart enough to look up more detail elsewhere once you know the idea or command to Google. Use `apt-get`/`yum`/`dnf`/`brew` (as appropriate) to install new programs. +- To keep this to one page, content is implicitly included by reference. You're smart enough to look up more detail elsewhere once you know the idea or command to Google. Use `apt-get`/`yum`/`dnf`/`pip`/`brew` (as appropriate) to install new programs. - Use [Explainshell](http://explainshell.com/) to get a helpful breakdown of what commands, options, pipes etc. do. From 6534752694c2cee24460b4ffdbfd727fae4aa0b5 Mon Sep 17 00:00:00 2001 From: "Wael M. Nasreddine" Date: Sun, 21 Jun 2015 00:31:43 -0700 Subject: [PATCH 31/53] remove a duplicator `for` --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index edad6fd..39bef86 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ Notes: - Use `nohup` or `disown` if you want a background process to keep running forever. -- Check what processes are listening via `netstat -lntp` or `ss -plat` (for TCP; add `-u` for for UDP). +- Check what processes are listening via `netstat -lntp` or `ss -plat` (for TCP; add `-u` for UDP). - See also `lsof` for open sockets and files. From 3a59ba819d18fde8efab3ae53ab95ea921726033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E6=A5=9A=E6=9D=B0?= Date: Sun, 21 Jun 2015 17:57:30 +0800 Subject: [PATCH 32/53] =?UTF-8?q?=E7=B2=97=E7=95=A5=E5=9C=B0=E7=BF=BB?= =?UTF-8?q?=E8=AF=91=E8=87=B3Everyday=20use=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README-zh.md | 430 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 430 insertions(+) create mode 100644 README-zh.md diff --git a/README-zh.md b/README-zh.md new file mode 100644 index 0000000..17b3cea --- /dev/null +++ b/README-zh.md @@ -0,0 +1,430 @@ +# 命令行的艺术 + +- [Meta](#meta) +- [Basics](#basics) +- [Everyday use](#everyday-use) +- [Processing files and data](#processing-files-and-data) +- [System debugging](#system-debugging) +- [One-liners](#one-liners) +- [Obscure but useful](#obscure-but-useful) +- [More resources](#more-resources) +- [Disclaimer](#disclaimer) + + +![curl -s 'https://raw.githubusercontent.com/jlevy/the-art-of-command-line/master/README.md' | egrep -o '`\w+`' | tr -d '`' | cowsay -W50](cowsay.png) + +熟练使用命令行是一种常常被忽视或被认为晦涩难懂,但实际上,它可以提高你作为工程师的灵活性以及生产力。本文是一份我在Linux上工作时发现的一些关于命令行的使用的小技巧的摘要。这些小技巧有基础的、相当复杂的甚至晦涩难懂的。这篇文章并不长,但当你能够熟练掌握这里列出的所有技巧时,你就学会了很多关于命令行的东西了。 + +这里的大部分内容 +[首次](http://www.quora.com/What-are-some-lesser-known-but-useful-Unix-commands) +[出现](http://www.quora.com/What-are-the-most-useful-Swiss-army-knife-one-liners-on-Unix) +于 [Quora](http://www.quora.com/What-are-some-time-saving-tips-that-every-Linux-user-should-know), +但考虑到这里的人们都具有学习的天赋且乐于接受别人的建议,使用Github来做这件事是更佳的选择。如果你在本文中发现了错误或者存在可以改善的地方,请果断提交Issue或Pull Request!(当然在提交前请看一下meta节和已有的issue/PR)。 + + +## Meta + +Scope: + +- 这篇文章对刚接触命令行的新手以及具有命令行使用经验的人都有用处。The goals are *breadth* (everything important), *specificity* (give concrete examples of the most common case), and *brevity* (avoid things that aren't essential or digressions you can easily look up elsewhere). 每个小技巧在某个特定情境下都是基本的或能够显著地节约时间。 +- 本文为Linux所写,但很多内容(并非所有的)同样适用于MacOS甚至Cygwin。 +- 本文关注于交互式Bash,尽管很多技巧适用于其他shell或Bash脚本。 + +Notes: + +- To keep this to one page, content is implicitly included by reference. You're smart enough to look up more detail elsewhere once you know the idea or command to Google. 使用 `apt-get`/`yum`/`dnf`/`brew` 来安装新程序。 +- 使用 [Explainshell](http://explainshell.com/) 去获取相关命令、参数、管道等内容的解释。 + + +## Basics + +- 学习Bash的基础知识。具体来说, 输入 `man bash` 并至少全文浏览一遍; 它很简单并且不长。其他的shell可能很好用,但Bash功能强大且几乎所有情况下都是可用的 ( *只*学习 zsh, fish或其他的shell的话, 在你自己的电脑上会显得很方便, 但在很多情况下会限制你, 比如当你需要在服务器上工作时)。 + +- 学习并掌握至少一个基于文本的编辑器。通常 Vim (`vi`) 会是你最好的选择。 + +- 学会如何使用`man`命令去阅读文档。学会使用`apropos`去查找文档。了解有些命令并不对应可执行文件,而是Bash内置的,可以使用`help`和`help -d`命令获取帮助信息。 + +- 学会使用`>`和`<`来重定向输出和输入,学会使用`|`来重定向管道。了解标准输出stdout和标准错误stderr。 + +- 学会使用通配符`*` ( 若能`?`和`{`...`}`更好) 和引用以及引用中`'`和`"`的区别。 + +- 熟悉Bash任务管理工具: `&`, **ctrl-z**, **ctrl-c**, `jobs`, `fg`, `bg`, `kill` 等。 + +- 了解`ssh`, 以及基本的无密码认证, `ssh-agent`, `ssh-add`等。 + +- 学会基本的文件管理: `ls` 和 `ls -l` (了解`ls -l`中每一列代表的意义), `less`, `head`, `tail`和`tail -f` (甚至 `less +F`), `ln` and `ln -s` (了解软连接和硬连接的区别), `chown`, `chmod`, `du` (硬盘使用情况概述: `du -sk *`)。 关于文件系统的管理,学习 `df`, `mount`, `fdisk`, `mkfs`。 + +- 学习基本的网络管理: `ip` 或 `ifconfig`, `dig`。 + +- 熟悉正则表达式,以及`grep`/`egrep`里不同参数的作用,例如`-i`, `-o`, `-A`,和 `-B`。 + +- 学会使用`apt-get`, `yum`, 或`dnf` (取决于你使用的Linux发行版)来查找或安装包。确保你的环境中有 `pip` 来安装基于Python的命令韩工具 (部分程序使用`pip`来安装会很简单)。 + + +## Everyday use + +- 在Bash中,可以使用**Tab**自动补全参数,使用**ctrl-r**搜索命令行历史。 + +- 在Bash中,使用**ctrl-w**删除你键入的最后一个单词,使用**ctrl-u**删除整行,使用**alt-b**和**alt-f**按单词移动, 使用**ctrl-k**从光标处删除到行尾,使用**ctrl-l**清屏。键入`man readline`查看Bash中的默认快捷键,内容很多。例如**alt-.** 循环地移向前一个参数, 以及**alt-***展开通配符。 + +- 你喜欢的话,可以键入`set -o vi`来使用vi风格的快捷键。 + +- 键入`history`查看命令行历史记录。其中有许多缩写, 例如`!$` (最后键入的参数)和`!!`(最后键入的命令),尽管通常被 **ctrl-r*和**alt-.**取代。 + +- 回到上一个工作路径: `cd -` + +- 如果你输入命令的时候改变了注意,按下**alt-#**在行首添加`#`(将你输入的命令视为注释),并回车。这样做的话,之后你可以很方便的利用命令行历史回到你刚才输入到一半的命令。 + +- 使用`xargs` ( 或`parallel`)。他们非常给力。注意到你可以控制每行参数个数(`-L`)和最大并行数 (`-P`)。如果你不确定它们是否会按你想的那样工作,先使用`xargs echo`查看一下。此外, 使用`-I{}`会很方便。例如: +```bash + find . -name '*.py' | xargs grep some_function + cat hosts | xargs -I{} ssh root@{} hostname +``` + +- `pstree -p`有助于展示进程树。 + +- 使用`pgrep`和`pkill`根据名字查找进程或发送信号。 + +- 了解你可以发往进程的信号的种类。比如,使用`kill -STOP [pid]`停止一个进程。使用`man 7 signal`查看详细列表。 + +- 使用`nohup`或`disown`使一个后台进程持续运行。 + +- 使用`netstat -lntp`或`ss -plat`检查哪些进程在监听端口(默认是检查TCP端口; 使用参数`-u`检查UDP端口)。 + +- 有关打开套接字和文件,请参阅`lsof`。 + +- 在Bash脚本中,使用`set -x`去调试输出,尽可能的使用严格模式,使用`set -e`令脚本在发生错误时退出而不是继续运行,使用`set -o pipefail`严谨地对待错误(尽管问题可能很微妙)。当牵扯到很多脚本时, 使用`trap`。 + +- 在Bash脚本中,子shell(使用括号`(...)`)是一种便捷的方式去组织参数。一个常见的例子是临时地移动工作路径,代码如下: +```bash + # do something in current dir + (cd /some/other/dir && other-command) + # continue in original dir +``` + +- 在Bash中, 注意到其中有许多形式的扩展。检查变量是否存在: `${name:?error message}`。例如, 当Bash脚本需要一个参数时, 可以使用这样的代码`input_file=${1:?usage: $0 input_file}`。数学表达式: `i=$(( (i + 1) % 5 ))`。序列: `{1..10}`。 截断字符串: `${var%suffix}`和`${var#prefix}`。例如,假设`var=foo.pdf`, 那么`echo ${var%.pdf}.txt`将输出`foo.txt`。 + +- 通过使用`<(some command)`可以将输出视为文件。例如, 对比本地文件`/etc/hosts`和一个远程文件: +```sh + diff /etc/hosts <(ssh somehost cat /etc/hosts) +``` + +- 了解Bash中的"here documents", 例如`cat <logfile 2>&1`。通常,为了保证命令不会在标准输入里残留一个打开了的文件句柄导致你当前所在的终端无法操作,添加` foo: + rename 's/\.bak$//' *.bak + # Full rename of filenames, directories, and contents foo -> bar: + repren --full --preserve-case --from foo --to bar . +``` + +- Use `shuf` to shuffle or select random lines from a file. + +- Know `sort`'s options. Know how keys work (`-t` and `-k`). In particular, watch out that you need to write `-k1,1` to sort by only the first field; `-k1` means sort according to the whole line. + +- Stable sort (`sort -s`) can be useful. For example, to sort first by field 2, then secondarily by field 1, you can use `sort -k1,1 | sort -s -k2,2` + +- If you ever need to write a tab literal in a command line in Bash (e.g. for the -t argument to sort), press **ctrl-v** **[Tab]** or write `$'\t'` (the latter is better as you can copy/paste it). + +- For binary files, use `hd` for simple hex dumps and `bvi` for binary editing. + +- Also for binary files, `strings` (plus `grep`, etc.) lets you find bits of text. + +- To convert text encodings, try `iconv`. Or `uconv` for more advanced use; it supports some advanced Unicode things. For example, this command lowercases and removes all accents (by expanding and dropping them): +```sh + uconv -f utf-8 -t utf-8 -x '::Any-Lower; ::Any-NFD; [:Nonspacing Mark:] >; ::Any-NFC; ' < input.txt > output.txt +``` + +- To split files into pieces, see `split` (to split by size) and `csplit` (to split by a pattern). + +- Use `zless`, `zmore`, `zcat`, and `zgrep` to operate on compressed files. + + +## System debugging + +- For web debugging, `curl` and `curl -I` are handy, or their `wget` equivalents, or the more modern [`httpie`](https://github.com/jakubroztocil/httpie). + +- To know disk/cpu/network status, use `iostat`, `netstat`, `top` (or the better `htop`), and (especially) `dstat`. Good for getting a quick idea of what's happening on a system. + +- For a more in-depth system overview, use [`glances`](https://github.com/nicolargo/glances). It presents you with several system level statistics in one terminal window. Very helpful for quickly checking on various subsystems. + +- To know memory status, run and understand the output of `free` and `vmstat`. In particular, be aware the "cached" value is memory held by the Linux kernel as file cache, so effectively counts toward the "free" value. + +- Java system debugging is a different kettle of fish, but a simple trick on Oracle's and some other JVMs is that you can run `kill -3 ` and a full stack trace and heap summary (including generational garbage collection details, which can be highly informative) will be dumped to stderr/logs. + +- Use `mtr` as a better traceroute, to identify network issues. + +- For looking at why a disk is full, `ncdu` saves time over the usual commands like `du -sh *`. + +- To find which socket or process is using bandwidth, try `iftop` or `nethogs`. + +- The `ab` tool (comes with Apache) is helpful for quick-and-dirty checking of web server performance. For more complex load testing, try `siege`. + +- For more serious network debugging, `wireshark`, `tshark`, or `ngrep`. + +- Know about `strace` and `ltrace`. These can be helpful if a program is failing, hanging, or crashing, and you don't know why, or if you want to get a general idea of performance. Note the profiling option (`-c`), and the ability to attach to a running process (`-p`). + +- Know about `ldd` to check shared libraries etc. + +- Know how to connect to a running process with `gdb` and get its stack traces. + +- Use `/proc`. It's amazingly helpful sometimes when debugging live problems. Examples: `/proc/cpuinfo`, `/proc/xxx/cwd`, `/proc/xxx/exe`, `/proc/xxx/fd/`, `/proc/xxx/smaps`. + +- When debugging why something went wrong in the past, `sar` can be very helpful. It shows historic statistics on CPU, memory, network, etc. + +- For deeper systems and performance analyses, look at `stap` ([SystemTap](https://sourceware.org/systemtap/wiki)), [`perf`](http://en.wikipedia.org/wiki/Perf_(Linux)), and [`sysdig`](https://github.com/draios/sysdig). + +- Confirm what Linux distribution you're using (works on most distros): `lsb_release -a` + +- Use `dmesg` whenever something's acting really funny (it could be hardware or driver issues). + + +## One-liners + +A few examples of piecing together commands: + +- It is remarkably helpful sometimes that you can do set intersection, union, and difference of text files via `sort`/`uniq`. Suppose `a` and `b` are text files that are already uniqued. This is fast, and works on files of arbitrary size, up to many gigabytes. (Sort is not limited by memory, though you may need to use the `-T` option if `/tmp` is on a small root partition.) See also the note about `LC_ALL` above and `sort`'s `-u` option (left out for clarity below). +```sh + cat a b | sort | uniq > c # c is a union b + cat a b | sort | uniq -d > c # c is a intersect b + cat a b b | sort | uniq -u > c # c is set difference a - b +``` + +- Use `grep . *` to visually examine all contents of all files in a directory, e.g. for directories filled with config settings, like `/sys`, `/proc`, `/etc`. + + +- Summing all numbers in the third column of a text file (this is probably 3X faster and 3X less code than equivalent Python): +```sh + awk '{ x += $3 } END { print x }' myfile +``` + +- If want to see sizes/dates on a tree of files, this is like a recursive `ls -l` but is easier to read than `ls -lR`: +```sh + find . -type f -ls +``` + +- Use `xargs` or `parallel` whenever you can. Note you can control how many items execute per line (`-L`) as well as parallelism (`-P`). If you're not sure if it'll do the right thing, use xargs echo first. Also, `-I{}` is handy. Examples: +```sh + find . -name '*.py' | xargs grep some_function + cat hosts | xargs -I{} ssh root@{} hostname +``` + +- Say you have a text file, like a web server log, and a certain value that appears on some lines, such as an `acct_id` parameter that is present in the URL. If you want a tally of how many requests for each `acct_id`: +```sh + cat access.log | egrep -o 'acct_id=[0-9]+' | cut -d= -f2 | sort | uniq -c | sort -rn +``` + +- Run this function to get a random tip from this document (parses Markdown and extracts an item): +```sh + function taocl() { + curl -s https://raw.githubusercontent.com/jlevy/the-art-of-command-line/master/README.md | + 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 + } +``` + + +## Obscure but useful + +- `expr`: perform arithmetic or boolean operations or evaluate regular expressions + +- `m4`: simple macro processor + +- `yes`: print a string a lot + +- `cal`: nice calendar + +- `env`: run a command (useful in scripts) + +- `look`: find English words (or lines in a file) beginning with a string + +- `cut `and `paste` and `join`: data manipulation + +- `fmt`: format text paragraphs + +- `pr`: format text into pages/columns + +- `fold`: wrap lines of text + +- `column`: format text into columns or tables + +- `expand` and `unexpand`: convert between tabs and spaces + +- `nl`: add line numbers + +- `seq`: print numbers + +- `bc`: calculator + +- `factor`: factor integers + +- `gpg`: encrypt and sign files + +- `toe`: table of terminfo entries + +- `nc`: network debugging and data transfer + +- `socat`: socket relay and tcp port forwarder (similar to `netcat`) + +- `slurm`: network trafic visualization + +- `dd`: moving data between files or devices + +- `file`: identify type of a file + +- `tree`: display directories and subdirectories as a nesting tree; like `ls` but recursive + +- `stat`: file info + +- `tac`: print files in reverse + +- `shuf`: random selection of lines from a file + +- `comm`: compare sorted files line by line + +- `hd` and `bvi`: dump or edit binary files + +- `strings`: extract text from binary files + +- `tr`: character translation or manipulation + +- `iconv` or `uconv`: conversion for text encodings + +- `split `and `csplit`: splitting files + +- `7z`: high-ratio file compression + +- `ldd`: dynamic library info + +- `nm`: symbols from object files + +- `ab`: benchmarking web servers + +- `strace`: system call debugging + +- `mtr`: better traceroute for network debugging + +- `cssh`: visual concurrent shell + +- `rsync`: sync files and folders over SSH + +- `wireshark` and `tshark`: packet capture and network debugging + +- `ngrep`: grep for the network layer + +- `host` and `dig`: DNS lookups + +- `lsof`: process file descriptor and socket info + +- `dstat`: useful system stats + +- [`glances`](https://github.com/nicolargo/glances): high level, multi-subsystem overview + +- `iostat`: CPU and disk usage stats + +- `htop`: improved version of top + +- `last`: login history + +- `w`: who's logged on + +- `id`: user/group identity info + +- `sar`: historic system stats + +- `iftop` or `nethogs`: network utilization by socket or process + +- `ss`: socket statistics + +- `dmesg`: boot and system error messages + +- `hdparm`: SATA/ATA disk manipulation/performance + +- `lsb_release`: Linux distribution info + +- `lshw`: hardware information + +- `fortune`, `ddate`, and `sl`: um, well, it depends on whether you consider steam locomotives and Zippy quotations "useful" + + +## More resources + +- [awesome-shell](https://github.com/alebcay/awesome-shell): A curated list of shell tools and resources. +- [Strict mode](http://redsymbol.net/articles/unofficial-bash-strict-mode/) for writing better shell scripts. + + +## 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! ;) From ecdf3a95f947ee2193fb29a31322d3ee91180a7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E6=A5=9A=E6=9D=B0?= Date: Sun, 21 Jun 2015 18:04:33 +0800 Subject: [PATCH 33/53] =?UTF-8?q?=E6=9B=B4=E6=96=B0Everyday=20use=E4=B9=8B?= =?UTF-8?q?=E5=89=8D=E7=9A=84=E7=BF=BB=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README-zh.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README-zh.md b/README-zh.md index 17b3cea..335c4b3 100644 --- a/README-zh.md +++ b/README-zh.md @@ -29,10 +29,11 @@ Scope: - 这篇文章对刚接触命令行的新手以及具有命令行使用经验的人都有用处。The goals are *breadth* (everything important), *specificity* (give concrete examples of the most common case), and *brevity* (avoid things that aren't essential or digressions you can easily look up elsewhere). 每个小技巧在某个特定情境下都是基本的或能够显著地节约时间。 - 本文为Linux所写,但很多内容(并非所有的)同样适用于MacOS甚至Cygwin。 - 本文关注于交互式Bash,尽管很多技巧适用于其他shell或Bash脚本。 +- 本文包括了"标准的"Unix命令和需要安装特定包的命令,只要它们足够重要。 Notes: -- To keep this to one page, content is implicitly included by reference. You're smart enough to look up more detail elsewhere once you know the idea or command to Google. 使用 `apt-get`/`yum`/`dnf`/`brew` 来安装新程序。 +- To keep this to one page, content is implicitly included by reference. You're smart enough to look up more detail elsewhere once you know the idea or command to Google. 使用 `apt-get`/`yum`/`dnf`/`pip`/`brew` 来安装新程序。 - 使用 [Explainshell](http://explainshell.com/) 去获取相关命令、参数、管道等内容的解释。 From 45a6d3788843dfea533da34293b0887e82951fcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E6=A5=9A=E6=9D=B0?= Date: Sun, 21 Jun 2015 18:55:05 +0800 Subject: [PATCH 34/53] =?UTF-8?q?=E7=BF=BB=E8=AF=91=E5=AE=8CProcessing=20f?= =?UTF-8?q?iles=20and=20data=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README-zh.md | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/README-zh.md b/README-zh.md index 335c4b3..1f39e9e 100644 --- a/README-zh.md +++ b/README-zh.md @@ -148,40 +148,40 @@ Notes: ## Processing files and data -- To locate a file by name in the current directory, `find . -iname '*something*'` (or similar). To find a file anywhere by name, use `locate something` (but bear in mind `updatedb` may not have indexed recently created files). +- 在当前路径下通过文件名定位一个文件,`find . -iname '*something*'`(或类似的)。在所有路径下通过文件名查找文件,使用 `locate something` (但请记住`updatedb`可能没有对最近新建的文件建立索引)。 -- For general searching through source or data files (more advanced than `grep -r`), use [`ag`](https://github.com/ggreer/the_silver_searcher). +- 使用[`ag`](https://github.com/ggreer/the_silver_searcher)在源或文件里检索(比`grep -r`更好)。 -- To convert HTML to text: `lynx -dump -stdin` +- 将HTML转为文本: `lynx -dump -stdin` -- For Markdown, HTML, and all kinds of document conversion, try [`pandoc`](http://pandoc.org/). +- Markdown, HTML, 以及所有文档格式之间的转换, 试试 [`pandoc`](http://pandoc.org/)。 -- If you must handle XML, `xmlstarlet` is old but good. +- 如果你不得不处理XML, `xmlstarlet`宝刀未老。 -- For JSON, use `jq`. +- 使用`jq`处理json。 -- For Excel or CSV files, [csvkit](https://github.com/onyxfish/csvkit) provides `in2csv`, `csvcut`, `csvjoin`, `csvgrep`, etc. +- Excel或CSV文件的处理, [csvkit](https://github.com/onyxfish/csvkit)提供了`in2csv`, `csvcut`, `csvjoin`, `csvgrep`等工具。 -- For Amazon S3, [`s3cmd`](https://github.com/s3tools/s3cmd) is convenient and [`s4cmd`](https://github.com/bloomreach/s4cmd) is faster. Amazon's [`aws`](https://github.com/aws/aws-cli) is essential for other AWS-related tasks. +- 关于Amazon S3, [`s3cmd`](https://github.com/s3tools/s3cmd)很方便而[`s4cmd`](https://github.com/bloomreach/s4cmd)更快。Amazon官方的[`aws`](https://github.com/aws/aws-cli)是其他AWS相关工作的基础。 -- Know about `sort` and `uniq`, including uniq's `-u` and `-d` options -- see one-liners below. +- 了解如何使用`sort`和`uniq`,包括uniq的`-u`参数和`-d`参数,详见后文one-liners。 -- Know about `cut`, `paste`, and `join` to manipulate text files. Many people use `cut` but forget about `join`. +- 了解如何使用`cut`,`paste`和`join`来更改文件。大部分人都会使用`cut`但忘了`join`。 -- Know about `wc` to count newlines (`-l`), characters (`-m`), words (`-w`) and bytes (`-c`). +- 了解如何运用`wc`去计算新行数(`-l`), 字符数(`-m`),单词数(`-w`)以及字节数(`-c`)。 -- Know about `tee` to copy from stdin to a file and also to stdout, as in `ls -al | tee file.txt`. +- 了解如何使用`tee`将标准输入复制到文件甚至标准输出,例如`ls -al | tee file.txt`。 -- Know that locale affects a lot of command line tools in subtle ways, including sorting order (collation) and performance. Most Linux installations will set `LANG` or other locale variables to a local setting like US English. But be aware sorting will change if you change locale. And know i18n routines can make sort or other commands run *many times* slower. In some situations (such as the set operations or uniqueness operations below) you can safely ignore slow i18n routines entirely and use traditional byte-based sort order, using `export LC_ALL=C`. +- 了解语言环境对许多命令行工具的微妙影响,包括排序的顺序和性能。大多数Linux的安装过程会将`LANG`或其他有关的变量设置为符合本地的设置。意识到当你改变语言环境时,排序的结果可能会改变。明白国际化可能会时sort或其他命令运行效率下降*许多倍*。某些情况下(例如集合运算)你可以放心的使用`export LC_ALL=C`来忽略掉国际化并使用基于字节的顺序。 -- Know basic `awk` and `sed` for simple data munging. For example, summing all numbers in the third column of a text file: `awk '{ x += $3 } END { print x }'`. This is probably 3X faster and 3X shorter than equivalent Python. +- 了解`awk`和`sed`关于数据的简单处理的用法。例如, 将文本文件中第三列的所有数字求和: `awk '{ x += $3 } END { print x }'`. 这可能比同等作用的Python代码块三倍且代码量少三倍。 -- To replace all occurrences of a string in place, in one or more files: +- 替换一个或多个文件中出现的字符串: ```sh perl -pi.bak -e 's/old-string/new-string/g' my-files-*.txt ``` -- To rename many files at once according to a pattern, use `rename`. For complex renames, [`repren`](https://github.com/jlevy/repren) may help. +- 依据某种模式批量重命名多个文件,使用`rename`。对于复杂的重命名规则,[`repren`](https://github.com/jlevy/repren)或许有帮助。 ```sh # Recover backup files foo.bak -> foo: rename 's/\.bak$//' *.bak @@ -189,26 +189,26 @@ Notes: repren --full --preserve-case --from foo --to bar . ``` -- Use `shuf` to shuffle or select random lines from a file. +- 使用`shuf`从一个文件中随机选取行。 -- Know `sort`'s options. Know how keys work (`-t` and `-k`). In particular, watch out that you need to write `-k1,1` to sort by only the first field; `-k1` means sort according to the whole line. +- 了解`sort`的参数。明白键的工作原理(`-t`和`-k`)。例如,注意到你需要`-k1,1`来仅按第一个域来排序,而`-k1`意味着按整行排序。 -- Stable sort (`sort -s`) can be useful. For example, to sort first by field 2, then secondarily by field 1, you can use `sort -k1,1 | sort -s -k2,2` +- 稳定排序(`sort -s`)在某些情况下很有用。例如,以第二个域为主关键字,第一个域为次关键字进行排序,你可以使用`sort -k1,1 | sort -s -k2,2` -- If you ever need to write a tab literal in a command line in Bash (e.g. for the -t argument to sort), press **ctrl-v** **[Tab]** or write `$'\t'` (the latter is better as you can copy/paste it). +- 如果你想在Bash命令行中写tab制表符,按下**ctrl-v** **[Tab]** 或键入`$'\t'`(后者可能更好,因为你可以复制粘贴它)。 -- For binary files, use `hd` for simple hex dumps and `bvi` for binary editing. +- 对于二进制文件,使用`hd`使其以十六进制显示以及使用`bvi`来编辑二进制。 -- Also for binary files, `strings` (plus `grep`, etc.) lets you find bits of text. +- 同样对于二进制文件,使用`strings`(包括`grep`等等)允许你查找一些文本。 - To convert text encodings, try `iconv`. Or `uconv` for more advanced use; it supports some advanced Unicode things. For example, this command lowercases and removes all accents (by expanding and dropping them): ```sh uconv -f utf-8 -t utf-8 -x '::Any-Lower; ::Any-NFD; [:Nonspacing Mark:] >; ::Any-NFC; ' < input.txt > output.txt ``` -- To split files into pieces, see `split` (to split by size) and `csplit` (to split by a pattern). +- 拆分文件,查看`split`(按大小拆分)和`csplit`(按模式拆分)。 -- Use `zless`, `zmore`, `zcat`, and `zgrep` to operate on compressed files. +- 使用`zless`, `zmore`, `zcat`和`zgrep`对压缩过的文件进行操作。 ## System debugging From affb5a969fc7a157eb29585aa155fd77610cff10 Mon Sep 17 00:00:00 2001 From: Armour <497052684@qq.com> Date: Sun, 21 Jun 2015 19:20:56 +0800 Subject: [PATCH 35/53] translated some sentences in META topic --- README-zh.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README-zh.md b/README-zh.md index 1f39e9e..5a4126f 100644 --- a/README-zh.md +++ b/README-zh.md @@ -26,14 +26,14 @@ Scope: -- 这篇文章对刚接触命令行的新手以及具有命令行使用经验的人都有用处。The goals are *breadth* (everything important), *specificity* (give concrete examples of the most common case), and *brevity* (avoid things that aren't essential or digressions you can easily look up elsewhere). 每个小技巧在某个特定情境下都是基本的或能够显著地节约时间。 +- 这篇文章对刚接触命令行的新手以及具有命令行使用经验的人都有用处。本文致力于做到覆盖面广(尽量包括一切重要的内容), 具体(给出最常见的具体的例子)以及简洁(避免一些不必要的东西以及一些偏题的可以在其他地方翻阅到文献的东西)。 每个小技巧在某个特定情境下都是基本的或能够显著地节约时间。 - 本文为Linux所写,但很多内容(并非所有的)同样适用于MacOS甚至Cygwin。 - 本文关注于交互式Bash,尽管很多技巧适用于其他shell或Bash脚本。 - 本文包括了"标准的"Unix命令和需要安装特定包的命令,只要它们足够重要。 Notes: -- To keep this to one page, content is implicitly included by reference. You're smart enough to look up more detail elsewhere once you know the idea or command to Google. 使用 `apt-get`/`yum`/`dnf`/`pip`/`brew` 来安装新程序。 +- 为了能在一页内展示尽量多的东西, 一些具体的信息会被间接的包含在引用页里。聪明机智的你如果掌握了使用 Google 搜索引擎的基本思路与命令, 那么你将可以查阅到更多地详细信息。使用 `apt-get`/`yum`/`dnf`/`pip`/`brew` 来安装新程序。 - 使用 [Explainshell](http://explainshell.com/) 去获取相关命令、参数、管道等内容的解释。 From 339a88d57e7444898cf11da1fd1e90536f72fa60 Mon Sep 17 00:00:00 2001 From: Armour <497052684@qq.com> Date: Sun, 21 Jun 2015 19:24:48 +0800 Subject: [PATCH 36/53] fixed typo --- README-zh.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README-zh.md b/README-zh.md index 5a4126f..e43ac96 100644 --- a/README-zh.md +++ b/README-zh.md @@ -33,7 +33,7 @@ Scope: Notes: -- 为了能在一页内展示尽量多的东西, 一些具体的信息会被间接的包含在引用页里。聪明机智的你如果掌握了使用 Google 搜索引擎的基本思路与命令, 那么你将可以查阅到更多地详细信息。使用 `apt-get`/`yum`/`dnf`/`pip`/`brew` 来安装新程序。 +- 为了能在一页内展示尽量多的东西, 一些具体的信息会被间接的包含在引用页里。聪明机智的你如果掌握了使用 Google 搜索引擎的基本思路与命令, 那么你将可以查阅到更多的详细信息。使用 `apt-get`/`yum`/`dnf`/`pip`/`brew` 来安装新程序。 - 使用 [Explainshell](http://explainshell.com/) 去获取相关命令、参数、管道等内容的解释。 From f5a2ba954306cf0d58faf2e6d070b36b891486d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E6=A5=9A=E6=9D=B0?= Date: Sun, 21 Jun 2015 21:37:39 +0800 Subject: [PATCH 37/53] =?UTF-8?q?=E5=AE=8C=E6=88=90System=20debugging?= =?UTF-8?q?=E7=BF=BB=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README-zh.md | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/README-zh.md b/README-zh.md index e43ac96..490cd89 100644 --- a/README-zh.md +++ b/README-zh.md @@ -201,7 +201,7 @@ Notes: - 同样对于二进制文件,使用`strings`(包括`grep`等等)允许你查找一些文本。 -- To convert text encodings, try `iconv`. Or `uconv` for more advanced use; it supports some advanced Unicode things. For example, this command lowercases and removes all accents (by expanding and dropping them): +- 使用`iconv`更改文本编码。而更高级的用法,可以使用`uconv`,它支持一些高级的Unicode功能。例如,这条命令将所有元音字母转为小写并移除了: ```sh uconv -f utf-8 -t utf-8 -x '::Any-Lower; ::Any-NFD; [:Nonspacing Mark:] >; ::Any-NFC; ' < input.txt > output.txt ``` @@ -213,41 +213,41 @@ Notes: ## System debugging -- For web debugging, `curl` and `curl -I` are handy, or their `wget` equivalents, or the more modern [`httpie`](https://github.com/jakubroztocil/httpie). +- `curl`和`curl -I`可以便捷地被应用于web调试中,它们的好兄弟`wget`也可以,或者是更潮流的[`httpie`](https://github.com/jakubroztocil/httpie)。 -- To know disk/cpu/network status, use `iostat`, `netstat`, `top` (or the better `htop`), and (especially) `dstat`. Good for getting a quick idea of what's happening on a system. +- 使用`iostat`、`netstat`、`top` (`htop`更佳) 和`dstat`去获取硬盘、cpu和网络的状态。熟练掌握这些工具可以使你快速的对系统的当前状态有一个大概的认识。 -- For a more in-depth system overview, use [`glances`](https://github.com/nicolargo/glances). It presents you with several system level statistics in one terminal window. Very helpful for quickly checking on various subsystems. +- 若要对系统有一个深度的总体认识, 使用[`glances`](https://github.com/nicolargo/glances)。它在一个终端窗口中向你提供一些系统级的数据。这对于快速的检查各个子系统非常有帮助。 -- To know memory status, run and understand the output of `free` and `vmstat`. In particular, be aware the "cached" value is memory held by the Linux kernel as file cache, so effectively counts toward the "free" value. +- 若要了解内存状态,运行并理解`free`和`vmstat`的输出。尤其注意"cached"的值,它指的是Linux内核用来作为文件缓存的内存大小,因此它与空闲内存无关。 -- Java system debugging is a different kettle of fish, but a simple trick on Oracle's and some other JVMs is that you can run `kill -3 ` and a full stack trace and heap summary (including generational garbage collection details, which can be highly informative) will be dumped to stderr/logs. +- Java系统调试则是一件截然不同的事,一个可以用于Oracle的JVM或其他JVM上的调试的小技巧是你可以运行`kill -3 `同时一个完整的栈轨迹和堆概述(包括GC的细节)会被保存到标准输出/日志文件。 -- Use `mtr` as a better traceroute, to identify network issues. +- 使用`mtr`去跟踪路由,用于确定网络问题。 -- For looking at why a disk is full, `ncdu` saves time over the usual commands like `du -sh *`. +- 用`ncdu`来查看磁盘使用情况,它比常用的命令,如`du -sh *`,更节省时间。 -- To find which socket or process is using bandwidth, try `iftop` or `nethogs`. +- 查找正在使用带宽的套接字连接或进程,使用`iftop`或`nethogs`。 -- The `ab` tool (comes with Apache) is helpful for quick-and-dirty checking of web server performance. For more complex load testing, try `siege`. +- `ab`工具(捆绑于Apache)可以简单粗暴地检查web服务器的性能。对于更复杂的负载测试,使用`siege`。 -- For more serious network debugging, `wireshark`, `tshark`, or `ngrep`. +- `wireshark`,`tshark`和`ngrep`可用于复杂的网络调试。 -- Know about `strace` and `ltrace`. These can be helpful if a program is failing, hanging, or crashing, and you don't know why, or if you want to get a general idea of performance. Note the profiling option (`-c`), and the ability to attach to a running process (`-p`). +- 了解`strace`和`ltrace`。这俩工具在你的程序运行失败、挂起甚至崩溃,而你却不知道为什么或你想对性能有个总体的认识的时候是非常有用的。注意profile参数(`-c`)和附加到一个运行的进程参数 (`-p`)。 -- Know about `ldd` to check shared libraries etc. +- 了解使用`ldd`来检查共享库。 -- Know how to connect to a running process with `gdb` and get its stack traces. +- 了解如何运用`gdb`连接到一个运行着的进程并获取它的堆栈轨迹。 -- Use `/proc`. It's amazingly helpful sometimes when debugging live problems. Examples: `/proc/cpuinfo`, `/proc/xxx/cwd`, `/proc/xxx/exe`, `/proc/xxx/fd/`, `/proc/xxx/smaps`. +- 学会使用`/proc`。它在调试正在出现的问题的时候有时会效果惊人。比如: `/proc/cpuinfo`, `/proc/xxx/cwd`, `/proc/xxx/exe`, `/proc/xxx/fd/`, `/proc/xxx/smaps`。 -- When debugging why something went wrong in the past, `sar` can be very helpful. It shows historic statistics on CPU, memory, network, etc. +- 当调试一些之前出现的问题的时候,`sar`非常有用。它展示了cpu、内存以及网络等的历史数据。 -- For deeper systems and performance analyses, look at `stap` ([SystemTap](https://sourceware.org/systemtap/wiki)), [`perf`](http://en.wikipedia.org/wiki/Perf_(Linux)), and [`sysdig`](https://github.com/draios/sysdig). +- 关于更深层次的系统分析以及性能分析,看看`stap` ([SystemTap](https://sourceware.org/systemtap/wiki)), [`perf`](http://en.wikipedia.org/wiki/Perf_(Linux)), 以及[`sysdig`](https://github.com/draios/sysdig)。 -- Confirm what Linux distribution you're using (works on most distros): `lsb_release -a` +- 查看你当前使用的Linux发型版(大部分发行版有效): `lsb_release -a` -- Use `dmesg` whenever something's acting really funny (it could be hardware or driver issues). +- 无论什么东西工作得很欢乐时试试`dmesg`(可能是硬件或驱动问题)。 ## One-liners From 36b2ea3333b4aa8074f5d51928c19ce1cfdeee94 Mon Sep 17 00:00:00 2001 From: Chris Rhodes Date: Sun, 21 Jun 2015 23:54:34 +1000 Subject: [PATCH 38/53] Adding link to Tmux site Adding link to the tmux site --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 39bef86..08063d3 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ Notes: - Use `man ascii` for a good ASCII table, with hex and decimal values. For general encoding info, `man unicode`, `man utf-8`, and `man latin1` are helpful. -- Use `screen` or `tmux` to multiplex the screen, especially useful on remote ssh sessions and to detach and re-attach to a session. A more minimal alternative for session persistence only is `dtach`. +- Use `screen` or [`tmux`](https://tmux.github.io/) to multiplex the screen, especially useful on remote ssh sessions and to detach and re-attach to a session. A more minimal alternative for session persistence only is `dtach`. - In ssh, knowing how to port tunnel with `-L` or `-D` (and occasionally `-R`) is useful, e.g. to access web sites from a remote server. From 786c4c10d00c38b4f0d4aca3ee3ddbda911edf63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E6=A5=9A=E6=9D=B0?= Date: Sun, 21 Jun 2015 23:27:07 +0800 Subject: [PATCH 39/53] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=88=9D=E6=AD=A5?= =?UTF-8?q?=E7=BF=BB=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README-zh.md | 200 +++++++++++++++++++++++++++------------------------ 1 file changed, 107 insertions(+), 93 deletions(-) diff --git a/README-zh.md b/README-zh.md index 490cd89..2cb6fb5 100644 --- a/README-zh.md +++ b/README-zh.md @@ -1,14 +1,15 @@ # 命令行的艺术 -- [Meta](#meta) -- [Basics](#basics) -- [Everyday use](#everyday-use) -- [Processing files and data](#processing-files-and-data) -- [System debugging](#system-debugging) -- [One-liners](#one-liners) -- [Obscure but useful](#obscure-but-useful) -- [More resources](#more-resources) -- [Disclaimer](#disclaimer) +- [必读](#必读) +- [基础](#基础) +- [日常使用](#日常使用) +- [文件及数据处理](#文件及数据处理) +- [系统调试](#系统调试) +- [一行代码](#一行代码) +- [冷门但有用](#冷门但有用) +- [更多资源](#更多资源) +- [免责声明](#免责声明) +- [授权条款](#授权条款) ![curl -s 'https://raw.githubusercontent.com/jlevy/the-art-of-command-line/master/README.md' | egrep -o '`\w+`' | tr -d '`' | cowsay -W50](cowsay.png) @@ -19,25 +20,25 @@ [首次](http://www.quora.com/What-are-some-lesser-known-but-useful-Unix-commands) [出现](http://www.quora.com/What-are-the-most-useful-Swiss-army-knife-one-liners-on-Unix) 于 [Quora](http://www.quora.com/What-are-some-time-saving-tips-that-every-Linux-user-should-know), -但考虑到这里的人们都具有学习的天赋且乐于接受别人的建议,使用Github来做这件事是更佳的选择。如果你在本文中发现了错误或者存在可以改善的地方,请果断提交Issue或Pull Request!(当然在提交前请看一下meta节和已有的issue/PR)。 +但考虑到这里的人们都具有学习的天赋且乐于接受别人的建议,使用Github来做这件事是更佳的选择。如果你在本文中发现了错误或者存在可以改善的地方,请果断提交Issue或Pull Request!(当然在提交前请看一下必读节和已有的issue/PR)。 -## Meta +## 必读 -Scope: +涵盖范围: - 这篇文章对刚接触命令行的新手以及具有命令行使用经验的人都有用处。本文致力于做到覆盖面广(尽量包括一切重要的内容), 具体(给出最常见的具体的例子)以及简洁(避免一些不必要的东西以及一些偏题的可以在其他地方翻阅到文献的东西)。 每个小技巧在某个特定情境下都是基本的或能够显著地节约时间。 - 本文为Linux所写,但很多内容(并非所有的)同样适用于MacOS甚至Cygwin。 - 本文关注于交互式Bash,尽管很多技巧适用于其他shell或Bash脚本。 - 本文包括了"标准的"Unix命令和需要安装特定包的命令,只要它们足够重要。 -Notes: +注意事项: - 为了能在一页内展示尽量多的东西, 一些具体的信息会被间接的包含在引用页里。聪明机智的你如果掌握了使用 Google 搜索引擎的基本思路与命令, 那么你将可以查阅到更多的详细信息。使用 `apt-get`/`yum`/`dnf`/`pip`/`brew` 来安装新程序。 - 使用 [Explainshell](http://explainshell.com/) 去获取相关命令、参数、管道等内容的解释。 -## Basics +## 基础 - 学习Bash的基础知识。具体来说, 输入 `man bash` 并至少全文浏览一遍; 它很简单并且不长。其他的shell可能很好用,但Bash功能强大且几乎所有情况下都是可用的 ( *只*学习 zsh, fish或其他的shell的话, 在你自己的电脑上会显得很方便, 但在很多情况下会限制你, 比如当你需要在服务器上工作时)。 @@ -62,7 +63,7 @@ Notes: - 学会使用`apt-get`, `yum`, 或`dnf` (取决于你使用的Linux发行版)来查找或安装包。确保你的环境中有 `pip` 来安装基于Python的命令韩工具 (部分程序使用`pip`来安装会很简单)。 -## Everyday use +## 日常使用 - 在Bash中,可以使用**Tab**自动补全参数,使用**ctrl-r**搜索命令行历史。 @@ -146,7 +147,7 @@ Notes: `python -m SimpleHTTPServer 7777` (使用端口7777和Python 2)或`python -m http.server 7777` (使用端口7777和Python 3)。 -## Processing files and data +## 文件及数据处理 - 在当前路径下通过文件名定位一个文件,`find . -iname '*something*'`(或类似的)。在所有路径下通过文件名查找文件,使用 `locate something` (但请记住`updatedb`可能没有对最近新建的文件建立索引)。 @@ -197,10 +198,14 @@ Notes: - 如果你想在Bash命令行中写tab制表符,按下**ctrl-v** **[Tab]** 或键入`$'\t'`(后者可能更好,因为你可以复制粘贴它)。 +- 标准的源代码对比及合并工具是`diff`和`patch`。使用`diffstat`查看变更总览数据。注意到`diff -r`对整个文件夹有效。使用`diff -r tree1 tree2 | diffstat`查看变更总览数据。 + - 对于二进制文件,使用`hd`使其以十六进制显示以及使用`bvi`来编辑二进制。 - 同样对于二进制文件,使用`strings`(包括`grep`等等)允许你查找一些文本。 +- 二进制文件对比(Delta压缩),使用`xdelta3`。 + - 使用`iconv`更改文本编码。而更高级的用法,可以使用`uconv`,它支持一些高级的Unicode功能。例如,这条命令将所有元音字母转为小写并移除了: ```sh uconv -f utf-8 -t utf-8 -x '::Any-Lower; ::Any-NFD; [:Nonspacing Mark:] >; ::Any-NFC; ' < input.txt > output.txt @@ -211,7 +216,7 @@ Notes: - 使用`zless`, `zmore`, `zcat`和`zgrep`对压缩过的文件进行操作。 -## System debugging +## 系统调试 - `curl`和`curl -I`可以便捷地被应用于web调试中,它们的好兄弟`wget`也可以,或者是更潮流的[`httpie`](https://github.com/jakubroztocil/httpie)。 @@ -250,42 +255,41 @@ Notes: - 无论什么东西工作得很欢乐时试试`dmesg`(可能是硬件或驱动问题)。 -## One-liners +## 一行代码 -A few examples of piecing together commands: +一些命令组合的例子: -- It is remarkably helpful sometimes that you can do set intersection, union, and difference of text files via `sort`/`uniq`. Suppose `a` and `b` are text files that are already uniqued. This is fast, and works on files of arbitrary size, up to many gigabytes. (Sort is not limited by memory, though you may need to use the `-T` option if `/tmp` is on a small root partition.) See also the note about `LC_ALL` above and `sort`'s `-u` option (left out for clarity below). +- 当你需要对文本文件做集合交、并、差运算时,结合使用`sort`/`uniq`很有帮助。假设`a`与`b`是两内容不同的文件。这种方式效率很高, 并且在小文件和上G的文件上都能运用 (`sort`不被内存大小约束,尽管在`/tmp`在一个小的根分区上时你可能需要`-T`参数),参阅前文中关于`LC_ALL`和`sort`的`-u`参数的部分。 ```sh cat a b | sort | uniq > c # c is a union b cat a b | sort | uniq -d > c # c is a intersect b cat a b b | sort | uniq -u > c # c is set difference a - b ``` -- Use `grep . *` to visually examine all contents of all files in a directory, e.g. for directories filled with config settings, like `/sys`, `/proc`, `/etc`. +- 使用`grep . *`来阅读检查目录下所有文件的内容,例如检查一个充满配置文件的目录比如`/sys`、`/proc`、`/etc`。 - -- Summing all numbers in the third column of a text file (this is probably 3X faster and 3X less code than equivalent Python): +- 计算文本文件第三列中所有数的和(可能比同等作用的Python代码块三倍且代码量少三倍): ```sh awk '{ x += $3 } END { print x }' myfile ``` -- If want to see sizes/dates on a tree of files, this is like a recursive `ls -l` but is easier to read than `ls -lR`: +- 如果你想在文件树上查看大小\日期,这可能看起来像递归版的`ls -l`但比`ls -lR`更易于理解: ```sh find . -type f -ls ``` -- Use `xargs` or `parallel` whenever you can. Note you can control how many items execute per line (`-L`) as well as parallelism (`-P`). If you're not sure if it'll do the right thing, use xargs echo first. Also, `-I{}` is handy. Examples: +- 尽可能的使用`xargs`或`parallel`。。注意到你可以控制每行参数个数(`-L`)和最大并行数 (`-P`)。如果你不确定它们是否会按你想的那样工作,先使用`xargs echo`查看一下。此外, 使用`-I{}`会很方便。例如: ```sh find . -name '*.py' | xargs grep some_function cat hosts | xargs -I{} ssh root@{} hostname ``` -- Say you have a text file, like a web server log, and a certain value that appears on some lines, such as an `acct_id` parameter that is present in the URL. If you want a tally of how many requests for each `acct_id`: +- 假设你有一个类似于web服务器日志文件的文本文件,并且一个确定的值只会出现在某些行上,假设一个`acct_id`参数在URI中。如果你想计算出每个`acct_id`值有多少次请求,使用如下代码: ```sh cat access.log | egrep -o 'acct_id=[0-9]+' | cut -d= -f2 | sort | uniq -c | sort -rn ``` -- Run this function to get a random tip from this document (parses Markdown and extracts an item): +- 运行这个函数从这篇文档中随机获取一条小技巧(解析Markdown文件并抽取项目): ```sh function taocl() { curl -s https://raw.githubusercontent.com/jlevy/the-art-of-command-line/master/README.md | @@ -297,135 +301,145 @@ A few examples of piecing together commands: ``` -## Obscure but useful +## 冷门但有用 -- `expr`: perform arithmetic or boolean operations or evaluate regular expressions +- `expr`: 计算表达式或正则匹配 -- `m4`: simple macro processor +- `m4`: 简单地宏处理器 -- `yes`: print a string a lot +- `yes`: 多次打印字符串 -- `cal`: nice calendar +- `cal`: 漂亮的日历 -- `env`: run a command (useful in scripts) +- `env`: 执行一个命令(脚本文件中很有用) -- `look`: find English words (or lines in a file) beginning with a string +- `look`: 查找以特定字符串开头的单词 -- `cut `and `paste` and `join`: data manipulation +- `cut`、`paste`和`join`: 数据修改 -- `fmt`: format text paragraphs +- `fmt`: 格式化文本段落 -- `pr`: format text into pages/columns +- `pr`: 将文本格式化成页/列形式 -- `fold`: wrap lines of text +- `fold`: 包裹文本中的几行 -- `column`: format text into columns or tables +- `column`: 将文本格式化成多列或表格 -- `expand` and `unexpand`: convert between tabs and spaces +- `expand`和`unexpand`: 制表符与空格之间转换 -- `nl`: add line numbers +- `nl`: 添加行号 -- `seq`: print numbers +- `seq`: 打印数字 -- `bc`: calculator +- `bc`: 计算器 -- `factor`: factor integers +- `factor`: 分解因数 -- `gpg`: encrypt and sign files +- `gpg`: 加密并签名文件 -- `toe`: table of terminfo entries +- `toe`: terminfo entries列表 -- `nc`: network debugging and data transfer +- `nc`: 网络调试及数据传输 -- `socat`: socket relay and tcp port forwarder (similar to `netcat`) +- `socat`: 套接字代理,与`netcat`类似 -- `slurm`: network trafic visualization +- `slurm`: 网络可视化 -- `dd`: moving data between files or devices +- `dd`: 文件或设备间传输数据 -- `file`: identify type of a file +- `file`: 确定文件类型 -- `tree`: display directories and subdirectories as a nesting tree; like `ls` but recursive +- `tree`: 以树的形式显示路径和文件,类似于递归的`ls` -- `stat`: file info +- `stat`: 文件信息 -- `tac`: print files in reverse +- `tac`: 反向输出文件 -- `shuf`: random selection of lines from a file +- `shuf`: 文件中随机选取几行 -- `comm`: compare sorted files line by line +- `comm`: 一行一行的比较排序过的文件 -- `hd` and `bvi`: dump or edit binary files +- `hd`和`bvi`: 保存或编辑二进制文件 -- `strings`: extract text from binary files +- `strings`: 从二进制文件中抽取文本 -- `tr`: character translation or manipulation +- `tr`: 转换字母 -- `iconv` or `uconv`: conversion for text encodings +- `iconv`或`uconv`: 简易的文件编码 -- `split `and `csplit`: splitting files +- `split `和`csplit`: 分割文件 -- `7z`: high-ratio file compression +- `units`: 将一种计量单位转换为另一种等效的计量单位(参阅`/usr/share/units/definitions.units`) -- `ldd`: dynamic library info +- `7z`: 高比例的文件压缩 -- `nm`: symbols from object files +- `ldd`: 动态库信息 -- `ab`: benchmarking web servers +- `nm`: 提取obj文件中的符号 -- `strace`: system call debugging +- `ab`: 性能分析web服务器 -- `mtr`: better traceroute for network debugging +- `strace`: 系统调用调试 -- `cssh`: visual concurrent shell +- `mtr`: 更好的网络调试跟踪工具 -- `rsync`: sync files and folders over SSH +- `cssh`: 可视化的并发shell -- `wireshark` and `tshark`: packet capture and network debugging +- `rsync`: 通过ssh同步文件和文件夹 -- `ngrep`: grep for the network layer +- `wireshark`和`tshark`:抓包和网络调试工具 -- `host` and `dig`: DNS lookups +- `ngrep`: 网络层的grep -- `lsof`: process file descriptor and socket info +- `host`和`dig`: DNS查找 -- `dstat`: useful system stats +- `lsof`: 列出当前系统打开文件的工具以及查看端口信息 -- [`glances`](https://github.com/nicolargo/glances): high level, multi-subsystem overview +- `dstat`: 系统状态查看 -- `iostat`: CPU and disk usage stats +- [`glances`](https://github.com/nicolargo/glances): 高层次的多子系统总览 -- `htop`: improved version of top +- `iostat`: CPU和硬盘状态 -- `last`: login history +- `htop`: top的加强版 -- `w`: who's logged on +- `last`: 登入记录 -- `id`: user/group identity info +- `w`: 查看处于登录状态的用户 -- `sar`: historic system stats +- `id`: 用户/组ID信息 -- `iftop` or `nethogs`: network utilization by socket or process +- `sar`: 系统历史数据 -- `ss`: socket statistics +- `iftop`或`nethogs`: 套接字及进程的网络利用 -- `dmesg`: boot and system error messages +- `ss`: 套接字数据 -- `hdparm`: SATA/ATA disk manipulation/performance +- `dmesg`: 引导及系统错误信息 -- `lsb_release`: Linux distribution info +- `hdparm`: SATA/ATA磁盘更改及性能分析 -- `lshw`: hardware information +- `lsb_release`: Linux发行版信息 -- `fortune`, `ddate`, and `sl`: um, well, it depends on whether you consider steam locomotives and Zippy quotations "useful" +- `lshw`: 硬件信息 +- `fortune`,`ddate`和`sl`: 额,这主要取决于你是否认为蒸汽火车和莫名其妙的名人名言是否"有用" -## More resources -- [awesome-shell](https://github.com/alebcay/awesome-shell): A curated list of shell tools and resources. -- [Strict mode](http://redsymbol.net/articles/unofficial-bash-strict-mode/) for writing better shell scripts. +## 更多资源 +- [awesome-shell](https://github.com/alebcay/awesome-shell): 一份精心组织的命令行工具及资源的列表。 +- [Strict mode](http://redsymbol.net/articles/unofficial-bash-strict-mode/): 为了编写更好的脚本文件。 -## 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! ;) +## 免责声明 + +除去特别微小的任务,记录下这些代码以便他人查看。责任往往伴随着能力,*可以*做并不意味着应该做。 + + +## 授权条款 + +[![Creative Commons License](https://i.creativecommons.org/l/by-sa/4.0/88x31.png)](http://creativecommons.org/licenses/by-sa/4.0/) + +本文使用授权协议 [Creative Commons Attribution-ShareAlike 4.0 International Licene](http://creativecommons.org/licenses/by-sa/4.0/)。 + From 7e5caf48ddf2eb688cb674b7f4c2c4581ac6d367 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E6=A5=9A=E6=9D=B0?= Date: Sun, 21 Jun 2015 23:34:19 +0800 Subject: [PATCH 40/53] =?UTF-8?q?=E6=94=B9=E5=96=84=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README-zh.md | 130 +++++++++++++++++++++++++-------------------------- 1 file changed, 65 insertions(+), 65 deletions(-) diff --git a/README-zh.md b/README-zh.md index 2cb6fb5..c7977d2 100644 --- a/README-zh.md +++ b/README-zh.md @@ -14,23 +14,23 @@ ![curl -s 'https://raw.githubusercontent.com/jlevy/the-art-of-command-line/master/README.md' | egrep -o '`\w+`' | tr -d '`' | cowsay -W50](cowsay.png) -熟练使用命令行是一种常常被忽视或被认为晦涩难懂,但实际上,它可以提高你作为工程师的灵活性以及生产力。本文是一份我在Linux上工作时发现的一些关于命令行的使用的小技巧的摘要。这些小技巧有基础的、相当复杂的甚至晦涩难懂的。这篇文章并不长,但当你能够熟练掌握这里列出的所有技巧时,你就学会了很多关于命令行的东西了。 +熟练使用命令行是一种常常被忽视或被认为晦涩难懂,但实际上,它可以提高你作为工程师的灵活性以及生产力。本文是一份我在Linux上工作时发现的一些关于命令行的使用的小技巧的摘要。这些小技巧有基础的、相当复杂的甚至晦涩难懂的。这篇文章并不长,但当你能够熟练掌握这里列出的所有技巧时,你就学会了很多关于命令行的东西了。 这里的大部分内容 [首次](http://www.quora.com/What-are-some-lesser-known-but-useful-Unix-commands) [出现](http://www.quora.com/What-are-the-most-useful-Swiss-army-knife-one-liners-on-Unix) 于 [Quora](http://www.quora.com/What-are-some-time-saving-tips-that-every-Linux-user-should-know), -但考虑到这里的人们都具有学习的天赋且乐于接受别人的建议,使用Github来做这件事是更佳的选择。如果你在本文中发现了错误或者存在可以改善的地方,请果断提交Issue或Pull Request!(当然在提交前请看一下必读节和已有的issue/PR)。 +但考虑到这里的人们都具有学习的天赋且乐于接受别人的建议,使用Github来做这件事是更佳的选择。如果你在本文中发现了错误或者存在可以改善的地方,请果断提交Issue或Pull Request!(当然在提交前请看一下必读节和已有的issue/PR)。 ## 必读 涵盖范围: -- 这篇文章对刚接触命令行的新手以及具有命令行使用经验的人都有用处。本文致力于做到覆盖面广(尽量包括一切重要的内容), 具体(给出最常见的具体的例子)以及简洁(避免一些不必要的东西以及一些偏题的可以在其他地方翻阅到文献的东西)。 每个小技巧在某个特定情境下都是基本的或能够显著地节约时间。 -- 本文为Linux所写,但很多内容(并非所有的)同样适用于MacOS甚至Cygwin。 -- 本文关注于交互式Bash,尽管很多技巧适用于其他shell或Bash脚本。 -- 本文包括了"标准的"Unix命令和需要安装特定包的命令,只要它们足够重要。 +- 这篇文章对刚接触命令行的新手以及具有命令行使用经验的人都有用处。本文致力于做到覆盖面广(尽量包括一切重要的内容), 具体(给出最常见的具体的例子)以及简洁(避免一些不必要的东西以及一些偏题的可以在其他地方翻阅到文献的东西)。 每个小技巧在某个特定情境下都是基本的或能够显著地节约时间。 +- 本文为Linux所写,但很多内容(并非所有的)同样适用于MacOS甚至Cygwin。 +- 本文关注于交互式Bash,尽管很多技巧适用于其他shell或Bash脚本。 +- 本文包括了"标准的"Unix命令和需要安装特定包的命令,只要它们足够重要。 注意事项: @@ -40,13 +40,13 @@ ## 基础 -- 学习Bash的基础知识。具体来说, 输入 `man bash` 并至少全文浏览一遍; 它很简单并且不长。其他的shell可能很好用,但Bash功能强大且几乎所有情况下都是可用的 ( *只*学习 zsh, fish或其他的shell的话, 在你自己的电脑上会显得很方便, 但在很多情况下会限制你, 比如当你需要在服务器上工作时)。 +- 学习Bash的基础知识。具体来说, 输入 `man bash` 并至少全文浏览一遍; 它很简单并且不长。其他的shell可能很好用,但Bash功能强大且几乎所有情况下都是可用的 ( *只*学习 zsh, fish或其他的shell的话, 在你自己的电脑上会显得很方便, 但在很多情况下会限制你, 比如当你需要在服务器上工作时)。 - 学习并掌握至少一个基于文本的编辑器。通常 Vim (`vi`) 会是你最好的选择。 -- 学会如何使用`man`命令去阅读文档。学会使用`apropos`去查找文档。了解有些命令并不对应可执行文件,而是Bash内置的,可以使用`help`和`help -d`命令获取帮助信息。 +- 学会如何使用`man`命令去阅读文档。学会使用`apropos`去查找文档。了解有些命令并不对应可执行文件,而是Bash内置的,可以使用`help`和`help -d`命令获取帮助信息。 -- 学会使用`>`和`<`来重定向输出和输入,学会使用`|`来重定向管道。了解标准输出stdout和标准错误stderr。 +- 学会使用`>`和`<`来重定向输出和输入,学会使用`|`来重定向管道。了解标准输出stdout和标准错误stderr。 - 学会使用通配符`*` ( 若能`?`和`{`...`}`更好) 和引用以及引用中`'`和`"`的区别。 @@ -58,26 +58,26 @@ - 学习基本的网络管理: `ip` 或 `ifconfig`, `dig`。 -- 熟悉正则表达式,以及`grep`/`egrep`里不同参数的作用,例如`-i`, `-o`, `-A`,和 `-B`。 +- 熟悉正则表达式,以及`grep`/`egrep`里不同参数的作用,例如`-i`, `-o`, `-A`,和 `-B`。 - 学会使用`apt-get`, `yum`, 或`dnf` (取决于你使用的Linux发行版)来查找或安装包。确保你的环境中有 `pip` 来安装基于Python的命令韩工具 (部分程序使用`pip`来安装会很简单)。 ## 日常使用 -- 在Bash中,可以使用**Tab**自动补全参数,使用**ctrl-r**搜索命令行历史。 +- 在Bash中,可以使用**Tab**自动补全参数,使用**ctrl-r**搜索命令行历史。 -- 在Bash中,使用**ctrl-w**删除你键入的最后一个单词,使用**ctrl-u**删除整行,使用**alt-b**和**alt-f**按单词移动, 使用**ctrl-k**从光标处删除到行尾,使用**ctrl-l**清屏。键入`man readline`查看Bash中的默认快捷键,内容很多。例如**alt-.** 循环地移向前一个参数, 以及**alt-***展开通配符。 +- 在Bash中,使用**ctrl-w**删除你键入的最后一个单词,使用**ctrl-u**删除整行,使用**alt-b**和**alt-f**按单词移动, 使用**ctrl-k**从光标处删除到行尾,使用**ctrl-l**清屏。键入`man readline`查看Bash中的默认快捷键,内容很多。例如**alt-.** 循环地移向前一个参数, 以及**alt-***展开通配符。 -- 你喜欢的话,可以键入`set -o vi`来使用vi风格的快捷键。 +- 你喜欢的话,可以键入`set -o vi`来使用vi风格的快捷键。 -- 键入`history`查看命令行历史记录。其中有许多缩写, 例如`!$` (最后键入的参数)和`!!`(最后键入的命令),尽管通常被 **ctrl-r*和**alt-.**取代。 +- 键入`history`查看命令行历史记录。其中有许多缩写, 例如`!$` (最后键入的参数)和`!!`(最后键入的命令),尽管通常被 **ctrl-r**和**alt-.**取代。 - 回到上一个工作路径: `cd -` -- 如果你输入命令的时候改变了注意,按下**alt-#**在行首添加`#`(将你输入的命令视为注释),并回车。这样做的话,之后你可以很方便的利用命令行历史回到你刚才输入到一半的命令。 +- 如果你输入命令的时候改变了注意,按下**alt-#**在行首添加`#`(将你输入的命令视为注释),并回车。这样做的话,之后你可以很方便的利用命令行历史回到你刚才输入到一半的命令。 -- 使用`xargs` ( 或`parallel`)。他们非常给力。注意到你可以控制每行参数个数(`-L`)和最大并行数 (`-P`)。如果你不确定它们是否会按你想的那样工作,先使用`xargs echo`查看一下。此外, 使用`-I{}`会很方便。例如: +- 使用`xargs` ( 或`parallel`)。他们非常给力。注意到你可以控制每行参数个数(`-L`)和最大并行数 (`-P`)。如果你不确定它们是否会按你想的那样工作,先使用`xargs echo`查看一下。此外, 使用`-I{}`会很方便。例如: ```bash find . -name '*.py' | xargs grep some_function cat hosts | xargs -I{} ssh root@{} hostname @@ -87,24 +87,24 @@ - 使用`pgrep`和`pkill`根据名字查找进程或发送信号。 -- 了解你可以发往进程的信号的种类。比如,使用`kill -STOP [pid]`停止一个进程。使用`man 7 signal`查看详细列表。 +- 了解你可以发往进程的信号的种类。比如,使用`kill -STOP [pid]`停止一个进程。使用`man 7 signal`查看详细列表。 - 使用`nohup`或`disown`使一个后台进程持续运行。 - 使用`netstat -lntp`或`ss -plat`检查哪些进程在监听端口(默认是检查TCP端口; 使用参数`-u`检查UDP端口)。 -- 有关打开套接字和文件,请参阅`lsof`。 +- 有关打开套接字和文件,请参阅`lsof`。 -- 在Bash脚本中,使用`set -x`去调试输出,尽可能的使用严格模式,使用`set -e`令脚本在发生错误时退出而不是继续运行,使用`set -o pipefail`严谨地对待错误(尽管问题可能很微妙)。当牵扯到很多脚本时, 使用`trap`。 +- 在Bash脚本中,使用`set -x`去调试输出,尽可能的使用严格模式,使用`set -e`令脚本在发生错误时退出而不是继续运行,使用`set -o pipefail`严谨地对待错误(尽管问题可能很微妙)。当牵扯到很多脚本时, 使用`trap`。 -- 在Bash脚本中,子shell(使用括号`(...)`)是一种便捷的方式去组织参数。一个常见的例子是临时地移动工作路径,代码如下: +- 在Bash脚本中,子shell(使用括号`(...)`)是一种便捷的方式去组织参数。一个常见的例子是临时地移动工作路径,代码如下: ```bash # do something in current dir (cd /some/other/dir && other-command) # continue in original dir ``` -- 在Bash中, 注意到其中有许多形式的扩展。检查变量是否存在: `${name:?error message}`。例如, 当Bash脚本需要一个参数时, 可以使用这样的代码`input_file=${1:?usage: $0 input_file}`。数学表达式: `i=$(( (i + 1) % 5 ))`。序列: `{1..10}`。 截断字符串: `${var%suffix}`和`${var#prefix}`。例如,假设`var=foo.pdf`, 那么`echo ${var%.pdf}.txt`将输出`foo.txt`。 +- 在Bash中, 注意到其中有许多形式的扩展。检查变量是否存在: `${name:?error message}`。例如, 当Bash脚本需要一个参数时, 可以使用这样的代码`input_file=${1:?usage: $0 input_file}`。数学表达式: `i=$(( (i + 1) % 5 ))`。序列: `{1..10}`。 截断字符串: `${var%suffix}`和`${var#prefix}`。例如,假设`var=foo.pdf`, 那么`echo ${var%.pdf}.txt`将输出`foo.txt`。 - 通过使用`<(some command)`可以将输出视为文件。例如, 对比本地文件`/etc/hosts`和一个远程文件: ```sh @@ -113,15 +113,15 @@ - 了解Bash中的"here documents", 例如`cat <logfile 2>&1`。通常,为了保证命令不会在标准输入里残留一个打开了的文件句柄导致你当前所在的终端无法操作,添加`logfile 2>&1`。通常,为了保证命令不会在标准输入里残留一个打开了的文件句柄导致你当前所在的终端无法操作,添加` foo: rename 's/\.bak$//' *.bak @@ -192,53 +192,53 @@ - 使用`shuf`从一个文件中随机选取行。 -- 了解`sort`的参数。明白键的工作原理(`-t`和`-k`)。例如,注意到你需要`-k1,1`来仅按第一个域来排序,而`-k1`意味着按整行排序。 +- 了解`sort`的参数。明白键的工作原理(`-t`和`-k`)。例如,注意到你需要`-k1,1`来仅按第一个域来排序,而`-k1`意味着按整行排序。 -- 稳定排序(`sort -s`)在某些情况下很有用。例如,以第二个域为主关键字,第一个域为次关键字进行排序,你可以使用`sort -k1,1 | sort -s -k2,2` +- 稳定排序(`sort -s`)在某些情况下很有用。例如,以第二个域为主关键字,第一个域为次关键字进行排序,你可以使用`sort -k1,1 | sort -s -k2,2` -- 如果你想在Bash命令行中写tab制表符,按下**ctrl-v** **[Tab]** 或键入`$'\t'`(后者可能更好,因为你可以复制粘贴它)。 +- 如果你想在Bash命令行中写tab制表符,按下**ctrl-v** **[Tab]** 或键入`$'\t'`(后者可能更好,因为你可以复制粘贴它)。 - 标准的源代码对比及合并工具是`diff`和`patch`。使用`diffstat`查看变更总览数据。注意到`diff -r`对整个文件夹有效。使用`diff -r tree1 tree2 | diffstat`查看变更总览数据。 -- 对于二进制文件,使用`hd`使其以十六进制显示以及使用`bvi`来编辑二进制。 +- 对于二进制文件,使用`hd`使其以十六进制显示以及使用`bvi`来编辑二进制。 -- 同样对于二进制文件,使用`strings`(包括`grep`等等)允许你查找一些文本。 +- 同样对于二进制文件,使用`strings`(包括`grep`等等)允许你查找一些文本。 -- 二进制文件对比(Delta压缩),使用`xdelta3`。 +- 二进制文件对比(Delta压缩),使用`xdelta3`。 -- 使用`iconv`更改文本编码。而更高级的用法,可以使用`uconv`,它支持一些高级的Unicode功能。例如,这条命令将所有元音字母转为小写并移除了: +- 使用`iconv`更改文本编码。而更高级的用法,可以使用`uconv`,它支持一些高级的Unicode功能。例如,这条命令将所有元音字母转为小写并移除了: ```sh uconv -f utf-8 -t utf-8 -x '::Any-Lower; ::Any-NFD; [:Nonspacing Mark:] >; ::Any-NFC; ' < input.txt > output.txt ``` -- 拆分文件,查看`split`(按大小拆分)和`csplit`(按模式拆分)。 +- 拆分文件,查看`split`(按大小拆分)和`csplit`(按模式拆分)。 - 使用`zless`, `zmore`, `zcat`和`zgrep`对压缩过的文件进行操作。 ## 系统调试 -- `curl`和`curl -I`可以便捷地被应用于web调试中,它们的好兄弟`wget`也可以,或者是更潮流的[`httpie`](https://github.com/jakubroztocil/httpie)。 +- `curl`和`curl -I`可以便捷地被应用于web调试中,它们的好兄弟`wget`也可以,或者是更潮流的[`httpie`](https://github.com/jakubroztocil/httpie)。 - 使用`iostat`、`netstat`、`top` (`htop`更佳) 和`dstat`去获取硬盘、cpu和网络的状态。熟练掌握这些工具可以使你快速的对系统的当前状态有一个大概的认识。 - 若要对系统有一个深度的总体认识, 使用[`glances`](https://github.com/nicolargo/glances)。它在一个终端窗口中向你提供一些系统级的数据。这对于快速的检查各个子系统非常有帮助。 -- 若要了解内存状态,运行并理解`free`和`vmstat`的输出。尤其注意"cached"的值,它指的是Linux内核用来作为文件缓存的内存大小,因此它与空闲内存无关。 +- 若要了解内存状态,运行并理解`free`和`vmstat`的输出。尤其注意"cached"的值,它指的是Linux内核用来作为文件缓存的内存大小,因此它与空闲内存无关。 -- Java系统调试则是一件截然不同的事,一个可以用于Oracle的JVM或其他JVM上的调试的小技巧是你可以运行`kill -3 `同时一个完整的栈轨迹和堆概述(包括GC的细节)会被保存到标准输出/日志文件。 +- Java系统调试则是一件截然不同的事,一个可以用于Oracle的JVM或其他JVM上的调试的小技巧是你可以运行`kill -3 `同时一个完整的栈轨迹和堆概述(包括GC的细节)会被保存到标准输出/日志文件。 -- 使用`mtr`去跟踪路由,用于确定网络问题。 +- 使用`mtr`去跟踪路由,用于确定网络问题。 -- 用`ncdu`来查看磁盘使用情况,它比常用的命令,如`du -sh *`,更节省时间。 +- 用`ncdu`来查看磁盘使用情况,它比常用的命令,如`du -sh *`,更节省时间。 -- 查找正在使用带宽的套接字连接或进程,使用`iftop`或`nethogs`。 +- 查找正在使用带宽的套接字连接或进程,使用`iftop`或`nethogs`。 -- `ab`工具(捆绑于Apache)可以简单粗暴地检查web服务器的性能。对于更复杂的负载测试,使用`siege`。 +- `ab`工具(捆绑于Apache)可以简单粗暴地检查web服务器的性能。对于更复杂的负载测试,使用`siege`。 -- `wireshark`,`tshark`和`ngrep`可用于复杂的网络调试。 +- `wireshark`,`tshark`和`ngrep`可用于复杂的网络调试。 -- 了解`strace`和`ltrace`。这俩工具在你的程序运行失败、挂起甚至崩溃,而你却不知道为什么或你想对性能有个总体的认识的时候是非常有用的。注意profile参数(`-c`)和附加到一个运行的进程参数 (`-p`)。 +- 了解`strace`和`ltrace`。这俩工具在你的程序运行失败、挂起甚至崩溃,而你却不知道为什么或你想对性能有个总体的认识的时候是非常有用的。注意profile参数(`-c`)和附加到一个运行的进程参数 (`-p`)。 - 了解使用`ldd`来检查共享库。 @@ -246,9 +246,9 @@ - 学会使用`/proc`。它在调试正在出现的问题的时候有时会效果惊人。比如: `/proc/cpuinfo`, `/proc/xxx/cwd`, `/proc/xxx/exe`, `/proc/xxx/fd/`, `/proc/xxx/smaps`。 -- 当调试一些之前出现的问题的时候,`sar`非常有用。它展示了cpu、内存以及网络等的历史数据。 +- 当调试一些之前出现的问题的时候,`sar`非常有用。它展示了cpu、内存以及网络等的历史数据。 -- 关于更深层次的系统分析以及性能分析,看看`stap` ([SystemTap](https://sourceware.org/systemtap/wiki)), [`perf`](http://en.wikipedia.org/wiki/Perf_(Linux)), 以及[`sysdig`](https://github.com/draios/sysdig)。 +- 关于更深层次的系统分析以及性能分析,看看`stap` ([SystemTap](https://sourceware.org/systemtap/wiki)), [`perf`](http://en.wikipedia.org/wiki/Perf_(Linux)), 以及[`sysdig`](https://github.com/draios/sysdig)。 - 查看你当前使用的Linux发型版(大部分发行版有效): `lsb_release -a` @@ -259,32 +259,32 @@ 一些命令组合的例子: -- 当你需要对文本文件做集合交、并、差运算时,结合使用`sort`/`uniq`很有帮助。假设`a`与`b`是两内容不同的文件。这种方式效率很高, 并且在小文件和上G的文件上都能运用 (`sort`不被内存大小约束,尽管在`/tmp`在一个小的根分区上时你可能需要`-T`参数),参阅前文中关于`LC_ALL`和`sort`的`-u`参数的部分。 +- 当你需要对文本文件做集合交、并、差运算时,结合使用`sort`/`uniq`很有帮助。假设`a`与`b`是两内容不同的文件。这种方式效率很高, 并且在小文件和上G的文件上都能运用 (`sort`不被内存大小约束,尽管在`/tmp`在一个小的根分区上时你可能需要`-T`参数),参阅前文中关于`LC_ALL`和`sort`的`-u`参数的部分。 ```sh cat a b | sort | uniq > c # c is a union b cat a b | sort | uniq -d > c # c is a intersect b cat a b b | sort | uniq -u > c # c is set difference a - b ``` -- 使用`grep . *`来阅读检查目录下所有文件的内容,例如检查一个充满配置文件的目录比如`/sys`、`/proc`、`/etc`。 +- 使用`grep . *`来阅读检查目录下所有文件的内容,例如检查一个充满配置文件的目录比如`/sys`、`/proc`、`/etc`。 -- 计算文本文件第三列中所有数的和(可能比同等作用的Python代码块三倍且代码量少三倍): +- 计算文本文件第三列中所有数的和(可能比同等作用的Python代码块三倍且代码量少三倍): ```sh awk '{ x += $3 } END { print x }' myfile ``` -- 如果你想在文件树上查看大小\日期,这可能看起来像递归版的`ls -l`但比`ls -lR`更易于理解: +- 如果你想在文件树上查看大小\日期,这可能看起来像递归版的`ls -l`但比`ls -lR`更易于理解: ```sh find . -type f -ls ``` -- 尽可能的使用`xargs`或`parallel`。。注意到你可以控制每行参数个数(`-L`)和最大并行数 (`-P`)。如果你不确定它们是否会按你想的那样工作,先使用`xargs echo`查看一下。此外, 使用`-I{}`会很方便。例如: +- 尽可能的使用`xargs`或`parallel`。。注意到你可以控制每行参数个数(`-L`)和最大并行数 (`-P`)。如果你不确定它们是否会按你想的那样工作,先使用`xargs echo`查看一下。此外, 使用`-I{}`会很方便。例如: ```sh find . -name '*.py' | xargs grep some_function cat hosts | xargs -I{} ssh root@{} hostname ``` -- 假设你有一个类似于web服务器日志文件的文本文件,并且一个确定的值只会出现在某些行上,假设一个`acct_id`参数在URI中。如果你想计算出每个`acct_id`值有多少次请求,使用如下代码: +- 假设你有一个类似于web服务器日志文件的文本文件,并且一个确定的值只会出现在某些行上,假设一个`acct_id`参数在URI中。如果你想计算出每个`acct_id`值有多少次请求,使用如下代码: ```sh cat access.log | egrep -o 'acct_id=[0-9]+' | cut -d= -f2 | sort | uniq -c | sort -rn ``` @@ -341,7 +341,7 @@ - `nc`: 网络调试及数据传输 -- `socat`: 套接字代理,与`netcat`类似 +- `socat`: 套接字代理,与`netcat`类似 - `slurm`: 网络可视化 @@ -349,7 +349,7 @@ - `file`: 确定文件类型 -- `tree`: 以树的形式显示路径和文件,类似于递归的`ls` +- `tree`: 以树的形式显示路径和文件,类似于递归的`ls` - `stat`: 文件信息 @@ -423,7 +423,7 @@ - `lshw`: 硬件信息 -- `fortune`,`ddate`和`sl`: 额,这主要取决于你是否认为蒸汽火车和莫名其妙的名人名言是否"有用" +- `fortune`,`ddate`和`sl`: 额,这主要取决于你是否认为蒸汽火车和莫名其妙的名人名言是否"有用" ## 更多资源 @@ -434,7 +434,7 @@ ## 免责声明 -除去特别微小的任务,记录下这些代码以便他人查看。责任往往伴随着能力,*可以*做并不意味着应该做。 +除去特别微小的任务,记录下这些代码以便他人查看。责任往往伴随着能力,*可以*做并不意味着应该做。 ## 授权条款 From 0951e261b93ef18c25ae226dc5cca087f713f331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E6=A5=9A=E6=9D=B0?= Date: Sun, 21 Jun 2015 23:37:36 +0800 Subject: [PATCH 41/53] Add the missing * --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 39bef86..8ce6c36 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ Notes: - Alternatively, if you love vi-style key-bindings, use `set -o vi`. -- To see recent commands, `history`. There are also many abbreviations such as `!$` (last argument) and `!!` last command, though these are often easily replaced with **ctrl-r* and **alt-.**. +- To see recent commands, `history`. There are also many abbreviations such as `!$` (last argument) and `!!` last command, though these are often easily replaced with **ctrl-r** and **alt-.**. - To go back to the previous working directory: `cd -` From 987cd4b1f8a4345ced9166aa1f9f9aed08fffb8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E6=A5=9A=E6=9D=B0?= Date: Sun, 21 Jun 2015 23:42:17 +0800 Subject: [PATCH 42/53] Revert "Add the missing *" This reverts commit 0951e261b93ef18c25ae226dc5cca087f713f331. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8ce6c36..39bef86 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ Notes: - Alternatively, if you love vi-style key-bindings, use `set -o vi`. -- To see recent commands, `history`. There are also many abbreviations such as `!$` (last argument) and `!!` last command, though these are often easily replaced with **ctrl-r** and **alt-.**. +- To see recent commands, `history`. There are also many abbreviations such as `!$` (last argument) and `!!` last command, though these are often easily replaced with **ctrl-r* and **alt-.**. - To go back to the previous working directory: `cd -` From e940c2f12ed254f8c3f1b52b5bc50b5bb3cf1abd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E6=A5=9A=E6=9D=B0?= Date: Sun, 21 Jun 2015 23:48:46 +0800 Subject: [PATCH 43/53] Added a mising * --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 39bef86..8ce6c36 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ Notes: - Alternatively, if you love vi-style key-bindings, use `set -o vi`. -- To see recent commands, `history`. There are also many abbreviations such as `!$` (last argument) and `!!` last command, though these are often easily replaced with **ctrl-r* and **alt-.**. +- To see recent commands, `history`. There are also many abbreviations such as `!$` (last argument) and `!!` last command, though these are often easily replaced with **ctrl-r** and **alt-.**. - To go back to the previous working directory: `cd -` From 75b81d89efe9be66be38dd5b686c8e01bfba3056 Mon Sep 17 00:00:00 2001 From: Saksham Sharma Date: Sun, 21 Jun 2015 21:38:58 +0530 Subject: [PATCH 44/53] Added pacman in install options. Arch needs to be appreciated :) --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 39bef86..556bca3 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Scope: Notes: -- To keep this to one page, content is implicitly included by reference. You're smart enough to look up more detail elsewhere once you know the idea or command to Google. Use `apt-get`/`yum`/`dnf`/`pip`/`brew` (as appropriate) to install new programs. +- To keep this to one page, content is implicitly included by reference. You're smart enough to look up more detail elsewhere once you know the idea or command to Google. Use `apt-get`/`yum`/`dnf`/`pacman`/`pip`/`brew` (as appropriate) to install new programs. - Use [Explainshell](http://explainshell.com/) to get a helpful breakdown of what commands, options, pipes etc. do. @@ -59,7 +59,7 @@ Notes: - Know regular expressions well, and the various flags to `grep`/`egrep`. The `-i`, `-o`, `-A`, and `-B` options are worth knowing. -- Learn to use `apt-get`, `yum`, or `dnf` (depending on distro) to find and install packages. And make sure you have `pip` to install Python-based command-line tools (a few below are easiest to install via `pip`). +- Learn to use `apt-get`, `yum`, `dnf` or `pacman` (depending on distro) to find and install packages. And make sure you have `pip` to install Python-based command-line tools (a few below are easiest to install via `pip`). ## Everyday use From e640d36189f8fb5f020032adb90c3108177ac6b7 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Sun, 21 Jun 2015 10:13:33 -0700 Subject: [PATCH 45/53] Add langauges link and Chinese translation. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b7d4766..dadbfd3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[ Languages: [中文](README-zh.md) ] + # The Art of Command Line - [Meta](#meta) From e3e79e863c572cb35cde3efa974debb92e46bde9 Mon Sep 17 00:00:00 2001 From: Okunev Yu Dmitry Date: Mon, 22 Jun 2015 09:57:51 +0300 Subject: [PATCH 46/53] Added "pv" to "Obscure but useful" --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index dadbfd3..2904746 100644 --- a/README.md +++ b/README.md @@ -361,6 +361,8 @@ A few examples of piecing together commands: - `comm`: compare sorted files line by line +- `pv`: monitor the progress of data through a pipe + - `hd` and `bvi`: dump or edit binary files - `strings`: extract text from binary files From cfae7ff7648103dd98b6a22b27274f4286a2c3d9 Mon Sep 17 00:00:00 2001 From: Fazle Arefin Date: Tue, 23 Jun 2015 11:42:05 +1000 Subject: [PATCH 47/53] Added lsblk command --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index dadbfd3..15311f2 100644 --- a/README.md +++ b/README.md @@ -423,6 +423,8 @@ A few examples of piecing together commands: - `lsb_release`: Linux distribution info +- `lsblk`: List block devices: a tree view of your disks and disk paritions + - `lshw`: hardware information - `fortune`, `ddate`, and `sl`: um, well, it depends on whether you consider steam locomotives and Zippy quotations "useful" From b1ac875bae9b0ec6a3ec2a37b2170facff012f73 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Mon, 22 Jun 2015 18:51:29 -0700 Subject: [PATCH 48/53] Mention lsblk. Worth adding here as well as #109. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 15311f2..0eecfaa 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ Notes: - Know `ssh`, and the basics of passwordless authentication, via `ssh-agent`, `ssh-add`, etc. -- Basic file management: `ls` and `ls -l` (in particular, learn what every column in `ls -l` means), `less`, `head`, `tail` and `tail -f` (or even better, `less +F`), `ln` and `ln -s` (learn the differences and advantages of hard versus soft links), `chown`, `chmod`, `du` (for a quick summary of disk usage: `du -sk *`). For filesystem management, `df`, `mount`, `fdisk`, `mkfs`. +- Basic file management: `ls` and `ls -l` (in particular, learn what every column in `ls -l` means), `less`, `head`, `tail` and `tail -f` (or even better, `less +F`), `ln` and `ln -s` (learn the differences and advantages of hard versus soft links), `chown`, `chmod`, `du` (for a quick summary of disk usage: `du -sk *`). For filesystem management, `df`, `mount`, `fdisk`, `mkfs`, `lsblk`. - Basic network management: `ip` or `ifconfig`, `dig`. From 74943cbd3b4eabf58cee8e19dd3d05fc1f881db7 Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Tue, 23 Jun 2015 16:05:53 -0700 Subject: [PATCH 49/53] Add printenv. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0eecfaa..0b7ef27 100644 --- a/README.md +++ b/README.md @@ -315,6 +315,8 @@ A few examples of piecing together commands: - `env`: run a command (useful in scripts) +- `printenv`: print out environment variables (useful in debugging and scripts) + - `look`: find English words (or lines in a file) beginning with a string - `cut `and `paste` and `join`: data manipulation From c327c3d9985d34b56f301c3e6d6866d627cfc43a Mon Sep 17 00:00:00 2001 From: Chunyang Xu Date: Wed, 24 Jun 2015 15:34:17 +0800 Subject: [PATCH 50/53] README-zh.md: Use Full-width comma MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change is made with GNU sed by using: gsed --in-place 's/,/,/g' README-zh.md gsed --in-place 's/, /,/g' README-zh.md --- README-zh.md | 164 +++++++++++++++++++++++++-------------------------- 1 file changed, 82 insertions(+), 82 deletions(-) diff --git a/README-zh.md b/README-zh.md index c7977d2..b089527 100644 --- a/README-zh.md +++ b/README-zh.md @@ -14,70 +14,70 @@ ![curl -s 'https://raw.githubusercontent.com/jlevy/the-art-of-command-line/master/README.md' | egrep -o '`\w+`' | tr -d '`' | cowsay -W50](cowsay.png) -熟练使用命令行是一种常常被忽视或被认为晦涩难懂,但实际上,它可以提高你作为工程师的灵活性以及生产力。本文是一份我在Linux上工作时发现的一些关于命令行的使用的小技巧的摘要。这些小技巧有基础的、相当复杂的甚至晦涩难懂的。这篇文章并不长,但当你能够熟练掌握这里列出的所有技巧时,你就学会了很多关于命令行的东西了。 +熟练使用命令行是一种常常被忽视或被认为晦涩难懂,但实际上,它可以提高你作为工程师的灵活性以及生产力。本文是一份我在Linux上工作时发现的一些关于命令行的使用的小技巧的摘要。这些小技巧有基础的、相当复杂的甚至晦涩难懂的。这篇文章并不长,但当你能够熟练掌握这里列出的所有技巧时,你就学会了很多关于命令行的东西了。 这里的大部分内容 [首次](http://www.quora.com/What-are-some-lesser-known-but-useful-Unix-commands) [出现](http://www.quora.com/What-are-the-most-useful-Swiss-army-knife-one-liners-on-Unix) -于 [Quora](http://www.quora.com/What-are-some-time-saving-tips-that-every-Linux-user-should-know), -但考虑到这里的人们都具有学习的天赋且乐于接受别人的建议,使用Github来做这件事是更佳的选择。如果你在本文中发现了错误或者存在可以改善的地方,请果断提交Issue或Pull Request!(当然在提交前请看一下必读节和已有的issue/PR)。 +于 [Quora](http://www.quora.com/What-are-some-time-saving-tips-that-every-Linux-user-should-know), +但考虑到这里的人们都具有学习的天赋且乐于接受别人的建议,使用Github来做这件事是更佳的选择。如果你在本文中发现了错误或者存在可以改善的地方,请果断提交Issue或Pull Request!(当然在提交前请看一下必读节和已有的issue/PR)。 ## 必读 涵盖范围: -- 这篇文章对刚接触命令行的新手以及具有命令行使用经验的人都有用处。本文致力于做到覆盖面广(尽量包括一切重要的内容), 具体(给出最常见的具体的例子)以及简洁(避免一些不必要的东西以及一些偏题的可以在其他地方翻阅到文献的东西)。 每个小技巧在某个特定情境下都是基本的或能够显著地节约时间。 -- 本文为Linux所写,但很多内容(并非所有的)同样适用于MacOS甚至Cygwin。 -- 本文关注于交互式Bash,尽管很多技巧适用于其他shell或Bash脚本。 -- 本文包括了"标准的"Unix命令和需要安装特定包的命令,只要它们足够重要。 +- 这篇文章对刚接触命令行的新手以及具有命令行使用经验的人都有用处。本文致力于做到覆盖面广(尽量包括一切重要的内容),具体(给出最常见的具体的例子)以及简洁(避免一些不必要的东西以及一些偏题的可以在其他地方翻阅到文献的东西)。 每个小技巧在某个特定情境下都是基本的或能够显著地节约时间。 +- 本文为Linux所写,但很多内容(并非所有的)同样适用于MacOS甚至Cygwin。 +- 本文关注于交互式Bash,尽管很多技巧适用于其他shell或Bash脚本。 +- 本文包括了"标准的"Unix命令和需要安装特定包的命令,只要它们足够重要。 注意事项: -- 为了能在一页内展示尽量多的东西, 一些具体的信息会被间接的包含在引用页里。聪明机智的你如果掌握了使用 Google 搜索引擎的基本思路与命令, 那么你将可以查阅到更多的详细信息。使用 `apt-get`/`yum`/`dnf`/`pip`/`brew` 来安装新程序。 +- 为了能在一页内展示尽量多的东西,一些具体的信息会被间接的包含在引用页里。聪明机智的你如果掌握了使用 Google 搜索引擎的基本思路与命令,那么你将可以查阅到更多的详细信息。使用 `apt-get`/`yum`/`dnf`/`pip`/`brew` 来安装新程序。 - 使用 [Explainshell](http://explainshell.com/) 去获取相关命令、参数、管道等内容的解释。 ## 基础 -- 学习Bash的基础知识。具体来说, 输入 `man bash` 并至少全文浏览一遍; 它很简单并且不长。其他的shell可能很好用,但Bash功能强大且几乎所有情况下都是可用的 ( *只*学习 zsh, fish或其他的shell的话, 在你自己的电脑上会显得很方便, 但在很多情况下会限制你, 比如当你需要在服务器上工作时)。 +- 学习Bash的基础知识。具体来说,输入 `man bash` 并至少全文浏览一遍; 它很简单并且不长。其他的shell可能很好用,但Bash功能强大且几乎所有情况下都是可用的 ( *只*学习 zsh,fish或其他的shell的话,在你自己的电脑上会显得很方便,但在很多情况下会限制你,比如当你需要在服务器上工作时)。 - 学习并掌握至少一个基于文本的编辑器。通常 Vim (`vi`) 会是你最好的选择。 -- 学会如何使用`man`命令去阅读文档。学会使用`apropos`去查找文档。了解有些命令并不对应可执行文件,而是Bash内置的,可以使用`help`和`help -d`命令获取帮助信息。 +- 学会如何使用`man`命令去阅读文档。学会使用`apropos`去查找文档。了解有些命令并不对应可执行文件,而是Bash内置的,可以使用`help`和`help -d`命令获取帮助信息。 -- 学会使用`>`和`<`来重定向输出和输入,学会使用`|`来重定向管道。了解标准输出stdout和标准错误stderr。 +- 学会使用`>`和`<`来重定向输出和输入,学会使用`|`来重定向管道。了解标准输出stdout和标准错误stderr。 - 学会使用通配符`*` ( 若能`?`和`{`...`}`更好) 和引用以及引用中`'`和`"`的区别。 -- 熟悉Bash任务管理工具: `&`, **ctrl-z**, **ctrl-c**, `jobs`, `fg`, `bg`, `kill` 等。 +- 熟悉Bash任务管理工具: `&`,**ctrl-z**,**ctrl-c**,`jobs`,`fg`,`bg`,`kill` 等。 -- 了解`ssh`, 以及基本的无密码认证, `ssh-agent`, `ssh-add`等。 +- 了解`ssh`,以及基本的无密码认证,`ssh-agent`,`ssh-add`等。 -- 学会基本的文件管理: `ls` 和 `ls -l` (了解`ls -l`中每一列代表的意义), `less`, `head`, `tail`和`tail -f` (甚至 `less +F`), `ln` and `ln -s` (了解软连接和硬连接的区别), `chown`, `chmod`, `du` (硬盘使用情况概述: `du -sk *`)。 关于文件系统的管理,学习 `df`, `mount`, `fdisk`, `mkfs`。 +- 学会基本的文件管理: `ls` 和 `ls -l` (了解`ls -l`中每一列代表的意义),`less`,`head`,`tail`和`tail -f` (甚至 `less +F`),`ln` and `ln -s` (了解软连接和硬连接的区别),`chown`,`chmod`,`du` (硬盘使用情况概述: `du -sk *`)。 关于文件系统的管理,学习 `df`,`mount`,`fdisk`,`mkfs`。 -- 学习基本的网络管理: `ip` 或 `ifconfig`, `dig`。 +- 学习基本的网络管理: `ip` 或 `ifconfig`,`dig`。 -- 熟悉正则表达式,以及`grep`/`egrep`里不同参数的作用,例如`-i`, `-o`, `-A`,和 `-B`。 +- 熟悉正则表达式,以及`grep`/`egrep`里不同参数的作用,例如`-i`,`-o`,`-A`,和 `-B`。 -- 学会使用`apt-get`, `yum`, 或`dnf` (取决于你使用的Linux发行版)来查找或安装包。确保你的环境中有 `pip` 来安装基于Python的命令韩工具 (部分程序使用`pip`来安装会很简单)。 +- 学会使用`apt-get`,`yum`,或`dnf` (取决于你使用的Linux发行版)来查找或安装包。确保你的环境中有 `pip` 来安装基于Python的命令韩工具 (部分程序使用`pip`来安装会很简单)。 ## 日常使用 -- 在Bash中,可以使用**Tab**自动补全参数,使用**ctrl-r**搜索命令行历史。 +- 在Bash中,可以使用**Tab**自动补全参数,使用**ctrl-r**搜索命令行历史。 -- 在Bash中,使用**ctrl-w**删除你键入的最后一个单词,使用**ctrl-u**删除整行,使用**alt-b**和**alt-f**按单词移动, 使用**ctrl-k**从光标处删除到行尾,使用**ctrl-l**清屏。键入`man readline`查看Bash中的默认快捷键,内容很多。例如**alt-.** 循环地移向前一个参数, 以及**alt-***展开通配符。 +- 在Bash中,使用**ctrl-w**删除你键入的最后一个单词,使用**ctrl-u**删除整行,使用**alt-b**和**alt-f**按单词移动,使用**ctrl-k**从光标处删除到行尾,使用**ctrl-l**清屏。键入`man readline`查看Bash中的默认快捷键,内容很多。例如**alt-.** 循环地移向前一个参数,以及**alt-***展开通配符。 -- 你喜欢的话,可以键入`set -o vi`来使用vi风格的快捷键。 +- 你喜欢的话,可以键入`set -o vi`来使用vi风格的快捷键。 -- 键入`history`查看命令行历史记录。其中有许多缩写, 例如`!$` (最后键入的参数)和`!!`(最后键入的命令),尽管通常被 **ctrl-r**和**alt-.**取代。 +- 键入`history`查看命令行历史记录。其中有许多缩写,例如`!$` (最后键入的参数)和`!!`(最后键入的命令),尽管通常被 **ctrl-r**和**alt-.**取代。 - 回到上一个工作路径: `cd -` -- 如果你输入命令的时候改变了注意,按下**alt-#**在行首添加`#`(将你输入的命令视为注释),并回车。这样做的话,之后你可以很方便的利用命令行历史回到你刚才输入到一半的命令。 +- 如果你输入命令的时候改变了注意,按下**alt-#**在行首添加`#`(将你输入的命令视为注释),并回车。这样做的话,之后你可以很方便的利用命令行历史回到你刚才输入到一半的命令。 -- 使用`xargs` ( 或`parallel`)。他们非常给力。注意到你可以控制每行参数个数(`-L`)和最大并行数 (`-P`)。如果你不确定它们是否会按你想的那样工作,先使用`xargs echo`查看一下。此外, 使用`-I{}`会很方便。例如: +- 使用`xargs` ( 或`parallel`)。他们非常给力。注意到你可以控制每行参数个数(`-L`)和最大并行数 (`-P`)。如果你不确定它们是否会按你想的那样工作,先使用`xargs echo`查看一下。此外,使用`-I{}`会很方便。例如: ```bash find . -name '*.py' | xargs grep some_function cat hosts | xargs -I{} ssh root@{} hostname @@ -87,41 +87,41 @@ - 使用`pgrep`和`pkill`根据名字查找进程或发送信号。 -- 了解你可以发往进程的信号的种类。比如,使用`kill -STOP [pid]`停止一个进程。使用`man 7 signal`查看详细列表。 +- 了解你可以发往进程的信号的种类。比如,使用`kill -STOP [pid]`停止一个进程。使用`man 7 signal`查看详细列表。 - 使用`nohup`或`disown`使一个后台进程持续运行。 - 使用`netstat -lntp`或`ss -plat`检查哪些进程在监听端口(默认是检查TCP端口; 使用参数`-u`检查UDP端口)。 -- 有关打开套接字和文件,请参阅`lsof`。 +- 有关打开套接字和文件,请参阅`lsof`。 -- 在Bash脚本中,使用`set -x`去调试输出,尽可能的使用严格模式,使用`set -e`令脚本在发生错误时退出而不是继续运行,使用`set -o pipefail`严谨地对待错误(尽管问题可能很微妙)。当牵扯到很多脚本时, 使用`trap`。 +- 在Bash脚本中,使用`set -x`去调试输出,尽可能的使用严格模式,使用`set -e`令脚本在发生错误时退出而不是继续运行,使用`set -o pipefail`严谨地对待错误(尽管问题可能很微妙)。当牵扯到很多脚本时,使用`trap`。 -- 在Bash脚本中,子shell(使用括号`(...)`)是一种便捷的方式去组织参数。一个常见的例子是临时地移动工作路径,代码如下: +- 在Bash脚本中,子shell(使用括号`(...)`)是一种便捷的方式去组织参数。一个常见的例子是临时地移动工作路径,代码如下: ```bash # do something in current dir (cd /some/other/dir && other-command) # continue in original dir ``` -- 在Bash中, 注意到其中有许多形式的扩展。检查变量是否存在: `${name:?error message}`。例如, 当Bash脚本需要一个参数时, 可以使用这样的代码`input_file=${1:?usage: $0 input_file}`。数学表达式: `i=$(( (i + 1) % 5 ))`。序列: `{1..10}`。 截断字符串: `${var%suffix}`和`${var#prefix}`。例如,假设`var=foo.pdf`, 那么`echo ${var%.pdf}.txt`将输出`foo.txt`。 +- 在Bash中,注意到其中有许多形式的扩展。检查变量是否存在: `${name:?error message}`。例如,当Bash脚本需要一个参数时,可以使用这样的代码`input_file=${1:?usage: $0 input_file}`。数学表达式: `i=$(( (i + 1) % 5 ))`。序列: `{1..10}`。 截断字符串: `${var%suffix}`和`${var#prefix}`。例如,假设`var=foo.pdf`,那么`echo ${var%.pdf}.txt`将输出`foo.txt`。 -- 通过使用`<(some command)`可以将输出视为文件。例如, 对比本地文件`/etc/hosts`和一个远程文件: +- 通过使用`<(some command)`可以将输出视为文件。例如,对比本地文件`/etc/hosts`和一个远程文件: ```sh diff /etc/hosts <(ssh somehost cat /etc/hosts) ``` -- 了解Bash中的"here documents", 例如`cat <logfile 2>&1`。通常,为了保证命令不会在标准输入里残留一个打开了的文件句柄导致你当前所在的终端无法操作,添加`logfile 2>&1`。通常,为了保证命令不会在标准输入里残留一个打开了的文件句柄导致你当前所在的终端无法操作,添加` foo: rename 's/\.bak$//' *.bak - # Full rename of filenames, directories, and contents foo -> bar: + # Full rename of filenames,directories,and contents foo -> bar: repren --full --preserve-case --from foo --to bar . ``` - 使用`shuf`从一个文件中随机选取行。 -- 了解`sort`的参数。明白键的工作原理(`-t`和`-k`)。例如,注意到你需要`-k1,1`来仅按第一个域来排序,而`-k1`意味着按整行排序。 +- 了解`sort`的参数。明白键的工作原理(`-t`和`-k`)。例如,注意到你需要`-k1,1`来仅按第一个域来排序,而`-k1`意味着按整行排序。 -- 稳定排序(`sort -s`)在某些情况下很有用。例如,以第二个域为主关键字,第一个域为次关键字进行排序,你可以使用`sort -k1,1 | sort -s -k2,2` +- 稳定排序(`sort -s`)在某些情况下很有用。例如,以第二个域为主关键字,第一个域为次关键字进行排序,你可以使用`sort -k1,1 | sort -s -k2,2` -- 如果你想在Bash命令行中写tab制表符,按下**ctrl-v** **[Tab]** 或键入`$'\t'`(后者可能更好,因为你可以复制粘贴它)。 +- 如果你想在Bash命令行中写tab制表符,按下**ctrl-v** **[Tab]** 或键入`$'\t'`(后者可能更好,因为你可以复制粘贴它)。 - 标准的源代码对比及合并工具是`diff`和`patch`。使用`diffstat`查看变更总览数据。注意到`diff -r`对整个文件夹有效。使用`diff -r tree1 tree2 | diffstat`查看变更总览数据。 -- 对于二进制文件,使用`hd`使其以十六进制显示以及使用`bvi`来编辑二进制。 +- 对于二进制文件,使用`hd`使其以十六进制显示以及使用`bvi`来编辑二进制。 -- 同样对于二进制文件,使用`strings`(包括`grep`等等)允许你查找一些文本。 +- 同样对于二进制文件,使用`strings`(包括`grep`等等)允许你查找一些文本。 -- 二进制文件对比(Delta压缩),使用`xdelta3`。 +- 二进制文件对比(Delta压缩),使用`xdelta3`。 -- 使用`iconv`更改文本编码。而更高级的用法,可以使用`uconv`,它支持一些高级的Unicode功能。例如,这条命令将所有元音字母转为小写并移除了: +- 使用`iconv`更改文本编码。而更高级的用法,可以使用`uconv`,它支持一些高级的Unicode功能。例如,这条命令将所有元音字母转为小写并移除了: ```sh uconv -f utf-8 -t utf-8 -x '::Any-Lower; ::Any-NFD; [:Nonspacing Mark:] >; ::Any-NFC; ' < input.txt > output.txt ``` -- 拆分文件,查看`split`(按大小拆分)和`csplit`(按模式拆分)。 +- 拆分文件,查看`split`(按大小拆分)和`csplit`(按模式拆分)。 -- 使用`zless`, `zmore`, `zcat`和`zgrep`对压缩过的文件进行操作。 +- 使用`zless`,`zmore`,`zcat`和`zgrep`对压缩过的文件进行操作。 ## 系统调试 -- `curl`和`curl -I`可以便捷地被应用于web调试中,它们的好兄弟`wget`也可以,或者是更潮流的[`httpie`](https://github.com/jakubroztocil/httpie)。 +- `curl`和`curl -I`可以便捷地被应用于web调试中,它们的好兄弟`wget`也可以,或者是更潮流的[`httpie`](https://github.com/jakubroztocil/httpie)。 - 使用`iostat`、`netstat`、`top` (`htop`更佳) 和`dstat`去获取硬盘、cpu和网络的状态。熟练掌握这些工具可以使你快速的对系统的当前状态有一个大概的认识。 -- 若要对系统有一个深度的总体认识, 使用[`glances`](https://github.com/nicolargo/glances)。它在一个终端窗口中向你提供一些系统级的数据。这对于快速的检查各个子系统非常有帮助。 +- 若要对系统有一个深度的总体认识,使用[`glances`](https://github.com/nicolargo/glances)。它在一个终端窗口中向你提供一些系统级的数据。这对于快速的检查各个子系统非常有帮助。 -- 若要了解内存状态,运行并理解`free`和`vmstat`的输出。尤其注意"cached"的值,它指的是Linux内核用来作为文件缓存的内存大小,因此它与空闲内存无关。 +- 若要了解内存状态,运行并理解`free`和`vmstat`的输出。尤其注意"cached"的值,它指的是Linux内核用来作为文件缓存的内存大小,因此它与空闲内存无关。 -- Java系统调试则是一件截然不同的事,一个可以用于Oracle的JVM或其他JVM上的调试的小技巧是你可以运行`kill -3 `同时一个完整的栈轨迹和堆概述(包括GC的细节)会被保存到标准输出/日志文件。 +- Java系统调试则是一件截然不同的事,一个可以用于Oracle的JVM或其他JVM上的调试的小技巧是你可以运行`kill -3 `同时一个完整的栈轨迹和堆概述(包括GC的细节)会被保存到标准输出/日志文件。 -- 使用`mtr`去跟踪路由,用于确定网络问题。 +- 使用`mtr`去跟踪路由,用于确定网络问题。 -- 用`ncdu`来查看磁盘使用情况,它比常用的命令,如`du -sh *`,更节省时间。 +- 用`ncdu`来查看磁盘使用情况,它比常用的命令,如`du -sh *`,更节省时间。 -- 查找正在使用带宽的套接字连接或进程,使用`iftop`或`nethogs`。 +- 查找正在使用带宽的套接字连接或进程,使用`iftop`或`nethogs`。 -- `ab`工具(捆绑于Apache)可以简单粗暴地检查web服务器的性能。对于更复杂的负载测试,使用`siege`。 +- `ab`工具(捆绑于Apache)可以简单粗暴地检查web服务器的性能。对于更复杂的负载测试,使用`siege`。 -- `wireshark`,`tshark`和`ngrep`可用于复杂的网络调试。 +- `wireshark`,`tshark`和`ngrep`可用于复杂的网络调试。 -- 了解`strace`和`ltrace`。这俩工具在你的程序运行失败、挂起甚至崩溃,而你却不知道为什么或你想对性能有个总体的认识的时候是非常有用的。注意profile参数(`-c`)和附加到一个运行的进程参数 (`-p`)。 +- 了解`strace`和`ltrace`。这俩工具在你的程序运行失败、挂起甚至崩溃,而你却不知道为什么或你想对性能有个总体的认识的时候是非常有用的。注意profile参数(`-c`)和附加到一个运行的进程参数 (`-p`)。 - 了解使用`ldd`来检查共享库。 - 了解如何运用`gdb`连接到一个运行着的进程并获取它的堆栈轨迹。 -- 学会使用`/proc`。它在调试正在出现的问题的时候有时会效果惊人。比如: `/proc/cpuinfo`, `/proc/xxx/cwd`, `/proc/xxx/exe`, `/proc/xxx/fd/`, `/proc/xxx/smaps`。 +- 学会使用`/proc`。它在调试正在出现的问题的时候有时会效果惊人。比如: `/proc/cpuinfo`,`/proc/xxx/cwd`,`/proc/xxx/exe`,`/proc/xxx/fd/`,`/proc/xxx/smaps`。 -- 当调试一些之前出现的问题的时候,`sar`非常有用。它展示了cpu、内存以及网络等的历史数据。 +- 当调试一些之前出现的问题的时候,`sar`非常有用。它展示了cpu、内存以及网络等的历史数据。 -- 关于更深层次的系统分析以及性能分析,看看`stap` ([SystemTap](https://sourceware.org/systemtap/wiki)), [`perf`](http://en.wikipedia.org/wiki/Perf_(Linux)), 以及[`sysdig`](https://github.com/draios/sysdig)。 +- 关于更深层次的系统分析以及性能分析,看看`stap` ([SystemTap](https://sourceware.org/systemtap/wiki)),[`perf`](http://en.wikipedia.org/wiki/Perf_(Linux)),以及[`sysdig`](https://github.com/draios/sysdig)。 - 查看你当前使用的Linux发型版(大部分发行版有效): `lsb_release -a` @@ -259,32 +259,32 @@ 一些命令组合的例子: -- 当你需要对文本文件做集合交、并、差运算时,结合使用`sort`/`uniq`很有帮助。假设`a`与`b`是两内容不同的文件。这种方式效率很高, 并且在小文件和上G的文件上都能运用 (`sort`不被内存大小约束,尽管在`/tmp`在一个小的根分区上时你可能需要`-T`参数),参阅前文中关于`LC_ALL`和`sort`的`-u`参数的部分。 +- 当你需要对文本文件做集合交、并、差运算时,结合使用`sort`/`uniq`很有帮助。假设`a`与`b`是两内容不同的文件。这种方式效率很高,并且在小文件和上G的文件上都能运用 (`sort`不被内存大小约束,尽管在`/tmp`在一个小的根分区上时你可能需要`-T`参数),参阅前文中关于`LC_ALL`和`sort`的`-u`参数的部分。 ```sh cat a b | sort | uniq > c # c is a union b cat a b | sort | uniq -d > c # c is a intersect b cat a b b | sort | uniq -u > c # c is set difference a - b ``` -- 使用`grep . *`来阅读检查目录下所有文件的内容,例如检查一个充满配置文件的目录比如`/sys`、`/proc`、`/etc`。 +- 使用`grep . *`来阅读检查目录下所有文件的内容,例如检查一个充满配置文件的目录比如`/sys`、`/proc`、`/etc`。 - 计算文本文件第三列中所有数的和(可能比同等作用的Python代码块三倍且代码量少三倍): ```sh awk '{ x += $3 } END { print x }' myfile ``` -- 如果你想在文件树上查看大小\日期,这可能看起来像递归版的`ls -l`但比`ls -lR`更易于理解: +- 如果你想在文件树上查看大小\日期,这可能看起来像递归版的`ls -l`但比`ls -lR`更易于理解: ```sh find . -type f -ls ``` -- 尽可能的使用`xargs`或`parallel`。。注意到你可以控制每行参数个数(`-L`)和最大并行数 (`-P`)。如果你不确定它们是否会按你想的那样工作,先使用`xargs echo`查看一下。此外, 使用`-I{}`会很方便。例如: +- 尽可能的使用`xargs`或`parallel`。。注意到你可以控制每行参数个数(`-L`)和最大并行数 (`-P`)。如果你不确定它们是否会按你想的那样工作,先使用`xargs echo`查看一下。此外,使用`-I{}`会很方便。例如: ```sh find . -name '*.py' | xargs grep some_function cat hosts | xargs -I{} ssh root@{} hostname ``` -- 假设你有一个类似于web服务器日志文件的文本文件,并且一个确定的值只会出现在某些行上,假设一个`acct_id`参数在URI中。如果你想计算出每个`acct_id`值有多少次请求,使用如下代码: +- 假设你有一个类似于web服务器日志文件的文本文件,并且一个确定的值只会出现在某些行上,假设一个`acct_id`参数在URI中。如果你想计算出每个`acct_id`值有多少次请求,使用如下代码: ```sh cat access.log | egrep -o 'acct_id=[0-9]+' | cut -d= -f2 | sort | uniq -c | sort -rn ``` @@ -341,7 +341,7 @@ - `nc`: 网络调试及数据传输 -- `socat`: 套接字代理,与`netcat`类似 +- `socat`: 套接字代理,与`netcat`类似 - `slurm`: 网络可视化 @@ -349,7 +349,7 @@ - `file`: 确定文件类型 -- `tree`: 以树的形式显示路径和文件,类似于递归的`ls` +- `tree`: 以树的形式显示路径和文件,类似于递归的`ls` - `stat`: 文件信息 @@ -423,7 +423,7 @@ - `lshw`: 硬件信息 -- `fortune`,`ddate`和`sl`: 额,这主要取决于你是否认为蒸汽火车和莫名其妙的名人名言是否"有用" +- `fortune`,`ddate`和`sl`: 额,这主要取决于你是否认为蒸汽火车和莫名其妙的名人名言是否"有用" ## 更多资源 @@ -434,7 +434,7 @@ ## 免责声明 -除去特别微小的任务,记录下这些代码以便他人查看。责任往往伴随着能力,*可以*做并不意味着应该做。 +除去特别微小的任务,记录下这些代码以便他人查看。责任往往伴随着能力,*可以*做并不意味着应该做。 ## 授权条款 From c878d4e1683d0138468c96ffba3a818b9ef44efa Mon Sep 17 00:00:00 2001 From: Chunyang Xu Date: Wed, 24 Jun 2015 17:09:45 +0800 Subject: [PATCH 51/53] REMOTE-zh.md: Fix typos --- README-zh.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README-zh.md b/README-zh.md index b089527..445c08a 100644 --- a/README-zh.md +++ b/README-zh.md @@ -60,7 +60,7 @@ - 熟悉正则表达式,以及`grep`/`egrep`里不同参数的作用,例如`-i`,`-o`,`-A`,和 `-B`。 -- 学会使用`apt-get`,`yum`,或`dnf` (取决于你使用的Linux发行版)来查找或安装包。确保你的环境中有 `pip` 来安装基于Python的命令韩工具 (部分程序使用`pip`来安装会很简单)。 +- 学会使用`apt-get`,`yum`,或`dnf` (取决于你使用的Linux发行版)来查找或安装包。确保你的环境中有 `pip` 来安装基于Python的命令行工具 (部分程序使用`pip`来安装会很简单)。 ## 日常使用 @@ -75,7 +75,7 @@ - 回到上一个工作路径: `cd -` -- 如果你输入命令的时候改变了注意,按下**alt-#**在行首添加`#`(将你输入的命令视为注释),并回车。这样做的话,之后你可以很方便的利用命令行历史回到你刚才输入到一半的命令。 +- 如果你输入命令的时候改变了主意,按下**alt-#**在行首添加`#`(将你输入的命令视为注释),并回车。这样做的话,之后你可以很方便的利用命令行历史回到你刚才输入到一半的命令。 - 使用`xargs` ( 或`parallel`)。他们非常给力。注意到你可以控制每行参数个数(`-L`)和最大并行数 (`-P`)。如果你不确定它们是否会按你想的那样工作,先使用`xargs echo`查看一下。此外,使用`-I{}`会很方便。例如: ```bash @@ -278,7 +278,7 @@ find . -type f -ls ``` -- 尽可能的使用`xargs`或`parallel`。。注意到你可以控制每行参数个数(`-L`)和最大并行数 (`-P`)。如果你不确定它们是否会按你想的那样工作,先使用`xargs echo`查看一下。此外,使用`-I{}`会很方便。例如: +- 尽可能的使用`xargs`或`parallel`。注意到你可以控制每行参数个数(`-L`)和最大并行数 (`-P`)。如果你不确定它们是否会按你想的那样工作,先使用`xargs echo`查看一下。此外,使用`-I{}`会很方便。例如: ```sh find . -name '*.py' | xargs grep some_function cat hosts | xargs -I{} ssh root@{} hostname From 99a6214ab6fcc70852743363acec35aa7334b2ed Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Wed, 24 Jun 2015 08:08:39 -0700 Subject: [PATCH 52/53] Additional reference to comm. Fixes #105. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5131fc0..5174ab4 100644 --- a/README.md +++ b/README.md @@ -166,7 +166,7 @@ Notes: - For Amazon S3, [`s3cmd`](https://github.com/s3tools/s3cmd) is convenient and [`s4cmd`](https://github.com/bloomreach/s4cmd) is faster. Amazon's [`aws`](https://github.com/aws/aws-cli) is essential for other AWS-related tasks. -- Know about `sort` and `uniq`, including uniq's `-u` and `-d` options -- see one-liners below. +- Know about `sort` and `uniq`, including uniq's `-u` and `-d` options -- see one-liners below. See also `comm`. - Know about `cut`, `paste`, and `join` to manipulate text files. Many people use `cut` but forget about `join`. From 1f461a98af44fb75396a10f9e97ad6b9941cffab Mon Sep 17 00:00:00 2001 From: Joshua Levy Date: Wed, 24 Jun 2015 08:14:10 -0700 Subject: [PATCH 53/53] Add lspci. Fixes #112. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5174ab4..0d890a1 100644 --- a/README.md +++ b/README.md @@ -429,7 +429,7 @@ A few examples of piecing together commands: - `lsblk`: List block devices: a tree view of your disks and disk paritions -- `lshw`: hardware information +- `lshw` and `lspci`: hardware information, including RAID, graphics, etc. - `fortune`, `ddate`, and `sl`: um, well, it depends on whether you consider steam locomotives and Zippy quotations "useful"