Terminal-笔记-1.0

文章目录

    • @[toc]
    • 装饰终端的显示 增加易读性
    • 取消/激活Zsh皮肤
  • Lecture 1: The Shell (2020)
    • 查看日期
    • 返回argument - echo
    • 关于目录的命令 pwd、 cd -、随机游走:
    • 关于ls --help 无法在macOS中运行的问题
    • dash
    • mv 重命名/更改文件所处的位置
      • 重命名
      • 移动位置
    • Input stream and ouput stream
      • 把命令的输出内容写入一个文件(使用>的意思是overwrite)
      • 运行python文件,将结果写入一个文件
      • 重定向cat < hello.txt >hello2.txt
      • 把命令的输出内容写入一个文件(使用>>的意思是append)
    • 使用pip->| 连接两个命令
      • Data wrangling lecture-------curl | grep?
    • 超级用户sudo
      • tee 命令
  • References:

#安装 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

你可能感兴趣的:(笔记,脚本语言)