Terminal-筆記-1.0

#安裝 Zsh-終端皮膚

裝飾終端的顯示 增加易讀性

macOS下測試可以成功使用的命令

手動安裝:

先從git上clone下來整個倉庫:
git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
安裝:將zshrc.zsh-template 拷貝到根目錄下~/.zshrc
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc

安裝完成之後退出當前會話重新打開一個終端窗口,就可以看見彩色的終端了。

取消/激活Zsh皮膚

You can just use exec to replace your current shell with a new shell:

Switch to bash:

exec bash

Switch to zsh:

exec zsh

This won’t affect new terminal windows or anything, but it’s convenient [5].

Lecture 1: The Shell (2020)

查看日期

➜  Desktop date
Sat May 16 10:54:02 ADT 2020

返回argument - echo

➜  ~ echo hello
hello
使用逃逸字符
➜  ~ echo hello\ world\ with\ spaces
hello world with spaces
系統會在$PATH 變量下尋找所有可能存放命令源文件的地方,來決定是否執行你的文件
➜  terminal_demo echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/Library/TeX/texbin:/Library/Apple/usr/bin
可以使用which命令查看 echo這個東西是在哪裏存放的(macOS)
➜  terminal_demo which echo
echo: shell built-in command



關於目錄的命令 pwd、 cd -、隨機遊走:

➜  terminal_demo pwd
/Users/lianda_duan/Downloads/terminal_demo

pwd-----> print working directly

➜  terminal_demo ls
➜  terminal_demo pwd
/Users/lianda_duan/Downloads/terminal_demo
➜  terminal_demo cd ..
➜  Downloads pwd
/Users/lianda_duan/Downloads

cd - 命令: 回到上次所處的文件位置
➜  Downloads cd -
~/Downloads/terminal_demo
➜  terminal_demo 

在系統的目錄中隨機遊走(與隨機過程的隨機遊走區分開),直到找到bin目錄下的 echo命令 返回參數hello\ world
➜  terminal_demo ../../../../../bin/echo hello\ world
hello world

關於ls --help 無法在macOS中運行的問題

On line 222 of _episodes/02-filedir.md, this command does not work on Max OSX, which implements BSD ls rather than GNU ls:

$ ls --help

I get this on a Mac:

$ ls --help
ls: illegal option -- -
usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]

See this on that topic. I suggest replacing ls --help by man ls, which works for both BSD and GNU tools [4].

dash

-l long use a long listing format

➜  terminal_demo ls -l          
total 0
-rw-r--r--  1 lianda_duan  staff  0 May 17 09:27 demo.py
-rw-r--r--  1 lianda_duan  staff  0 May 17 09:27 demo_2.py

Permission first_group owner

➜  terminal_demo cd /
➜  / ls -l
total 10
drwxrwxr-x+ 69 root  admin  2208 May 14 21:07 Applications
drwxr-xr-x  69 root  wheel  2208 May 13 17:02 Library
drwxr-xr-x@  8 root  wheel   256 Dec  5 04:23 System
drwxr-xr-x   5 root  admin   160 Dec  5 04:21 Users
drwxr-xr-x   4 root  wheel   128 May 14 21:40 Volumes
drwxr-xr-x@ 38 root  wheel  1216 Apr 10 23:12 bin
drwxr-xr-x   2 root  wheel    64 Nov  9  2019 cores
dr-xr-xr-x   3 root  wheel  4666 Apr 24 23:17 dev
lrwxr-xr-x@  1 root  admin    11 Dec  5 07:25 etc -> private/etc
lrwxr-xr-x   1 root  wheel    25 Apr 24 23:17 home -> /System/Volumes/Data/home

Group-3 read-write-execute

➜  / ls -l /bin
total 4848
-rwxr-xr-x  1 root  wheel    35824 Apr  6 17:39 [
-r-xr-xr-x  1 root  wheel   623472 Apr  6 17:39 bash
-rwxr-xr-x  1 root  wheel    36768 Apr  6 17:39 cat
-rwxr-xr-x  1 root  wheel    47264 Apr  6 17:39 chmod
-rwxr-xr-x  1 root  wheel    42272 Apr  6 17:39 cp
-rwxr-xr-x  1 root  wheel   529424 Apr  6 17:39 csh
-rwxr-xr-x  1 root  wheel   110848 Apr  6 17:39 dash
-rwxr-xr-x  1 root  wheel    41872 Apr  6 17:39 date
-rwxr-xr-x  1 root  wheel    45120 Apr  6 17:39 dd
-rwxr-xr-x  1 root  wheel    36512 Apr  6 17:39 df
-rwxr-xr-x  1 root  wheel    31264 Apr  6 17:39 echo
-rwxr-xr-x  1 root  wheel    67200 Apr  6 17:39 ed
-rwxr-xr-x  1 root  wheel    36240 Apr  6 17:39 expr

mv 重命名/更改文件所處的位置

重命名

➜  terminal_demo ls                                     
demo.py      demo_2.py    demo_storage
➜  terminal_demo echo "rename file demo.py->dddd.py"
rename file demo.py->dddd.py
➜  terminal_demo mv demo.py dddd.py
➜  terminal_demo ls
dddd.py      demo_2.py    demo_storage

移動位置

➜  terminal_demo ls demo_storage 
➜  terminal_demo mv dddd.py demo_storage 
➜  terminal_demo ls demo_storage        
dddd.py
rmdir 
only allowed to remove empty file for safty consern

Input stream and ouput stream

把命令的輸出內容寫入一個文件(使用>的意思是overwrite)

➜  terminal_demo echo "這是我寫入的內容" > hello.txt
➜  terminal_demo ls
demo_2.py    demo_storage hello.txt
➜  terminal_demo cat hello.txt 
這是我寫入的內容

運行python文件,將結果寫入一個文件

➜  terminal_demo vim demo_2.py 
➜  terminal_demo python demo_2.py > hello.txt
➜  terminal_demo cat demo_2.py 
for _ in range(10):
	print(_)
➜  terminal_demo cat hello.txt 
0
1
2
3
4
5
6
7
8
9

重定向cat < hello.txt >hello2.txt

這裏實際上將調用

cat < hello.txt

的輸出結果輸入到另一個文件hello2.txt中。

➜  terminal_demo cat hello.txt 
0
1
2
3
4
5
6
7
8
9
➜  terminal_demo cat < hello.txt > hello2.txt
➜  terminal_demo cat hello2.txt 
0
1
2
3
4
5
6
7
8
9
➜  terminal_demo 

把命令的輸出內容寫入一個文件(使用>>的意思是append)

➜  terminal_demo vim demo_2.py 
➜  terminal_demo cat demo_2.py             
for _ in range(3):
	print(_)
➜  terminal_demo python demo_2.py >hello3.py
➜  terminal_demo cat hello3.py 
0
1
2
➜  terminal_demo python demo_2.py >> hello3.py
➜  terminal_demo cat hello3.py                
0
1
2
0
1
2

使用pip->| 連接兩個命令

其中tail 是顯示最後一行

#列出所有
➜  terminal_demo  ls -l
total 32
-rw-r--r--  1 lianda_duan  staff  29 May 17 10:02 demo_2.py
drwxr-xr-x  3 lianda_duan  staff  96 May 17 09:39 demo_storage
-rw-r--r--  1 lianda_duan  staff  20 May 17 09:50 hello.txt
-rw-r--r--  1 lianda_duan  staff  20 May 17 10:00 hello2.txt
-rw-r--r--  1 lianda_duan  staff  12 May 17 10:02 hello3.py
#顯示最後一行
➜  terminal_demo ls -l | tail -n1
-rw-r--r--  1 lianda_duan  staff  12 May 17 10:02 hello3.py
#顯示最後兩行
➜  terminal_demo ls -l | tail -n2
-rw-r--r--  1 lianda_duan  staff  20 May 17 10:00 hello2.txt
-rw-r--r--  1 lianda_duan  staff  12 May 17 10:02 hello3.py
#無用 測試
➜  terminal_demo ls -l | tail    
total 32
-rw-r--r--  1 lianda_duan  staff  29 May 17 10:02 demo_2.py
drwxr-xr-x  3 lianda_duan  staff  96 May 17 09:39 demo_storage
-rw-r--r--  1 lianda_duan  staff  20 May 17 09:50 hello.txt
-rw-r--r--  1 lianda_duan  staff  20 May 17 10:00 hello2.txt
-rw-r--r--  1 lianda_duan  staff  12 May 17 10:02 hello3.py
#顯示最後一行(另一種寫法)
➜  terminal_demo ls -l | tail -1
-rw-r--r--  1 lianda_duan  staff  12 May 17 10:02 hello3.py
#顯示最後兩行(另一種寫法)
➜  terminal_demo ls -l | tail -2
-rw-r--r--  1 lianda_duan  staff  20 May 17 10:00 hello2.txt
-rw-r--r--  1 lianda_duan  staff  12 May 17 10:02 hello3.py

Data wrangling lecture-------curl | grep?

➜  terminal_demo curl --head --silent google.com
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Sun, 17 May 2020 13:17:21 GMT
Expires: Tue, 16 Jun 2020 13:17:21 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
➜  terminal_demo curl --head --silent google.com | grep -i content-length
Content-Length: 219
➜  terminal_demo curl --head --silent google.com | grep -i content-length
Content-Length: 219
➜  terminal_demo curl --head --silent baidu.com                          
HTTP/1.1 200 OK
Date: Sun, 17 May 2020 13:19:28 GMT
Server: Apache
Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
ETag: "51-47cf7e6ee8400"
Accept-Ranges: bytes
Content-Length: 81
Cache-Control: max-age=86400
Expires: Mon, 18 May 2020 13:19:28 GMT
Connection: Keep-Alive
Content-Type: text/html

➜  terminal_demo curl --head --silent baidu.com  | grep -i content-length
Content-Length: 81

#這個命令在mac 下不可運行, 在ubuntu中可以
#返回一個長度信息
➜  terminal_demo curl --head --silent baidu.com  | grep -i content-length | cut --delimiter=' ' -f2   


超級用戶sudo

sudo + any_command_you_want

sudo 1060 | sudo tee brughtness

tee 命令

這個命令可以寫入東西到一個文件裏面並查看內容

tee 命令
echo 1060 | sudo tee brightness
echo 1 | sudo tee brightness

這個命令只在linux中使用,在mac中用的是open:

xdg-open xxxx.html

Exercise

Link:https://missing.csail.mit.edu/2020/course-shell/

References:

[1] https://www.youtube.com/watch?v=Z56Jmr9Z34Q&t=2003s

[2] https://missing.csail.mit.edu/2020/course-shell/

[3] https://zhuanlan.zhihu.com/p/19556676 終端裝飾器參考

[4] https://github.com/swcarpentry/shell-novice/issues/447

[5] https://stackoverflow.com/questions/10341271/switching-from-zsh-to-bash-on-osx-and-back-again/10341338

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章