Redirect standard input and standard output
cat oceans.txt > continents.txt
:'overwrite all original content in contines.txt using oceans.txt
将oceans.txt里的内容覆写入continents.txt中'
cat glaciers.txt >> rivers.txt
:'the output data of rivers.txt will contain the original contents
of rivers.txt with the content of glaciers.txt appended to it.
将glaciers.txt中的内容加到rivers.txt中'
cat < lakes.txt
:'Redirect the contents of lakes.txt as standard input for the cat command.
等价于cat lakes.txt 查看lakes.txt中的内容'
cat volcanoes.txt | wc
:'the wc command outputs the number of lines, words, and characters in volcanoes.txt, respectively.
wc命令分别输出volcanoes.txt中的行数、字数和字符数'
cat volcanoes.txt | wc | cat > islands.txt
:'将cat volcanoes.txt | wc的结果输入到islands.txt中'
sort lakes.txt
:'sort takes the standard input and orders it alphabetically for the standard output.
Here, the lakes in sort lakes.txt are listed in alphabetical order.
将lakes.txt中的内容按字母表顺序排列'
cat lakes.txt | sort > sorted-lakes.txt
:'Here, the command takes the standard output from cat lakes.txt and "pipes" it to sort.
The standard output of sort is redirected to sorted-lakes.txt.
将lakes.txt中的内容按字母表顺序排列输出到sorted-lakes.txt中,lakes.txt中的内容不变'
uniq deserts.txt
:'filters out adjacent, duplicate lines in a file.
将deserts.txt中的相邻且重复的行滤除'
sort deserts.txt | uniq > uniq-deserts.txt
:'Here by piping sort deserts.txt to uniq, all duplicate lines
are alphabetized (and thereby made adjacent) and filtered out.
先将deserts.txt中的内容按字母表顺序排列再滤除重复的内容行,最后输出到uniq-deserts.txt中'
grep Mount mountains.txt
:'grep stands for "global regular expression print". It searches files for lines
that match a pattern and returns the results. It is also case sensitive.
从mountains.txt中搜索到带有Mount的内容行(对大小写敏感)'
grep -i Mount mountains.txt
:'grep -i enables the command to be case insensitive
对大小写不敏感'
grep -R Arctic /home/ccuser/workspace/geography
:'grep -R searches all files in a directory and outputs filenames and lines containing matched results.
搜索/home/ccuser/workspace/geography中的所有文件,输出内容中带Arctic的文件名和内容行'
grep -Rl Arctic /home/ccuser/workspace/geography
:'grep -Rl searches all files in a directory and outputs only filenames with matched results.
搜索/home/ccuser/workspace/geography中的所有文件,输出内容中带Arctic的文件名'
sed 's/snow/rain/' forests.txt
:'In this case, sed searches forests.txt for the word "snow" and replaces it with "rain."
Importantly, the above command will only replace the first instance of "snow" on a line.
把forests.txt中带有snow的内容行中的第一个snow换成rain'
sed 's/snow/rain/g' forests.txt
:'Here sed searches forests.txt for the word "snow" and replaces it with "rain", globally.
把forests.txt中带snow的内容行中的snow换成rain'
Generalizations:
Redirection reroutes standard input, standard output, and standard error.
-
redirects standard output of a command to a file, overwriting previous content.>
-
redirects standard output of a command to a file, appending new content to old content.>>
-
redirects standard input to a command.<
-
redirects standard output of a command to another command.|
A number of other commands are powerful when combined with redirection commands:
sort
: sorts lines of text alphabetically.uniq
: filters duplicate, adjacent lines of text.grep
: searches for a text pattern and outputs it.sed
: searches for a text pattern, modifies it, and outputs it.
ENVIRONMENT
nano hello.txt
:'nano is a command line text editor. It works just like a desktop text editor like TextEdit or Notepad,
except that it is accessible from the command line and only accepts keyboard input.
The command nano hello.txt opens a new text file named hello.txt in the nano text editor.'
nano ~/.bash_profile
:"~/.bash_profile is the name of file used to store environment settings. It is commonly called the "bash profile".
When a session starts, it will load the contents of the bash profile before executing commands.
The ~ represents the user's home directory.
The . indicates a hidden file.
The command nano ~/.bash_profile opens up ~/.bash_profile in nano.
可以在~/.bash_profile中配置命令"
在~/.bash_profile中写入echo "Welcome, Jane Doe"。就相当于cmd脚本
在~/.bash_profile中写入alias pd="pwd" 保存后用source激活可以用pd代替pwd命令
source ~/.bash_profile
:'执行~/.bash_profile中的脚本,激活~/.bash_profile做的更改。'
history 查看用过的命令