文章目录
- Part one - 基础命令
- 1、命令
- 2、Navigation(导航)
- 4 Manipulating Files And Directories(文件与目录)
- 5 Working With Commands(使用命令)
- 6 – Redirection(重定向)
- 7 符号变量
- 8 Advanced Keyboard Tricks(高级键盘技巧)
- 9 Permissions(权限)
- 10 Processes(进程)
- Part two – 配置与环境变量
- Part 3 – Common Tasks And Essential Tools(常见任务和基本工具)
- 14 – Package Management(包管理)
- 15 Storage Media(存储介质)
- 16 Networking(网络)
- 17 Searching For Files(文件搜索)
- 18 Archiving And Backup(存档和备份)
- 19 – Regular Expressions(正则表达式)
- 20 Text Processing(文本处理)
- search-and-replace
- 21 Formatting Output(格式化输出)
- 22 Printing(打印)
- CUPS (Common Unix Printing System) :provides print drivers and print-job management
- Ghostscript: a PostScript interpreter, acts as a RIP.
- CUPS 有两种printing,lpr, lp.
- 23 – Compiling Programs(程序编译)
- Part 4 – Writing Shell Scripts(编写shell脚本)
- 24 – Writing Your First Script(开始你的第一个脚本)
- 25 – Starting A Project(开始)
- 26 Top-Down Design(自上而下的设计)
- 27 – Flow Control: Branching With if(流程控制:if分支)
- 28 Reading Keyboard Input(读取键盘输入)
- 29 – Flow Control: Looping With while / until(流程控制:while/until)
- 30 – Troubleshooting
- 31 – Flow Control: Branching With case(流程控制 case分支)
- 32 – Positional Parameters(位置参数)
- 33 – Flow Control: Looping With for(流程控制:for循环)
- 34 – Strings And Numbers(字符与数字)
- 35 – Arrays(数组)
- 36 – Exotica(特殊)
- 问题
- 需要了解的知识
Part one - 基础命令
1、命令
- date
- cal -calendar
- df -current amount of free space on your disk drives
- free -display the amount of free memory
- exit -closing the terminal emulator window
2、Navigation(导航)
- pwd - Print name of current working directory
- cd - Change directory
- ls - List directory contents
- file – Determine file type
- less – View file contents(和cat的区别?参考)
4 Manipulating Files And Directories(文件与目录)
- cp #如果文件已经存在,直接覆盖,没有提示,要提示用-i
- mv
- mkdir
- rm # Remove Files And Directories
- ln # Create Links,软链接”和“硬链接”的区别:硬链接指向磁盘区块,软链接指向绝对路径。
hard links的缺点:
- cannot span physical devices.
- cannot reference directories, only files.
5 Working With Commands(使用命令)
-
type – Indicate how a command name is interpreted
-
which – Display An Executable’s Location
-
help – Get help for shell builtins
help cd cd: cd [-L|[-P [-e]] [-@]] [dir] Change the shell working directory.
理解注释:[]是可选,|是互斥。
-
-help – Display Usage Information
-
man – Display a command’s manual page
-
info – Display a command’s info entry
man .info .–help 的用法和区别:help最简单,info最详细,man在两者之间。
-
apropos – Display a list of appropriate commands
-
whatis – Display a very brief description of a command
-
alias – Create an alias for a command
使用:
#起别名前先检查是否存在,用type: tsc@tsc:~$ type test test is a shell builtin alias name='string' # name后不能有空格 tsc@tsc:~$ alias foo='cd /usr; ls; cd -' tsc@tsc:~$ foo
终端关闭后作用就消失了。
-
unalias --去除别名
6 – Redirection(重定向)
-
cat - Concatenate files
# cat file1 # 输出文件到屏幕,没有分页 cat > file.txt # cat没有参数时接受stdin* this is test. # ctrl+d 结束* # < 接受文件输入* tsc@tsc:~$ cat < lazy_dog.txt
-
sort - Sort lines of text
-
uniq - Report or omit repeated lines
# -d 显示重复的 tsc@tsc:~$ ls /bin /usr/bin | sort | uniq -d | less
-
grep - Print lines matching a pattern
#“global regular expression print”,find功能。 tsc@tsc:~$ ls /bin /usr/bin | sort | uniq | grep zip
-
wc - Print newline, word, and byte counts for each file
#联合使用看条目个数: tsc@tsc:~$ ls /bin /usr/bin | sort | uniq | wc -l
-
head - Output the first part of a file
-
tail - Output the last part of a file
-
tee - Read from standard input and write to standard output and files
这个名字很有意思,tee有三通管的意思,配合pipe使用,它的作用是从stdin读入,复制到stdout和文件。
# 一方面输出到ls.txt,一方面传给grep tsc@tsc:~$ ls /usr/bin/ | tee ls.txt | grep zip
I/O redirection 的作用
I/O redirection allows us to change where output goes and where input comes from.
>重定向
tsc@tsc:~$ ls -l /usr/bin/ > ls-output.txt
tsc@tsc:~$ > ls-output2.txt # 创建一个空文件
# 要注意重复>时,原来的文件会被清空。
>> 追加
tsc@tsc:~$ ls -l /usr/bin >> ls-output2.txt
#文件不存在会新建。
error 输出
tsc@tsc:~$ ls -l /bin/usr 2> ls-error.txt
# 我的问题:在不知道有没有error的情况下,>和2>要如何同时执行?
都输出到一个文件(有两种方法)
# 1.老版本
# 顺序很重要
ls -l /bin/usr > ls-output.txt 2>&
# 2.新,用&>
ls -l /bin/usr &> ls-output.txt
ls -l /bin/usr &>> ls-output.txt # 追加
Pipelines
ls -l /usr/bin | less
#千万不要把>用成|,不然有些后果很严重。
filters
tsc@tsc:~$ ls /bin /usr/bin | sort | less
7 符号变量
这节讲expansion, 就是*、~之类的,本质就是变量。
-
echo – Display a line of text
It prints out its text arguments on standard output
tsc@tsc:~$ echo this is a test this is a test # Tilde Expansion tsc@tsc:~/playground$ echo ~ /home/tsc #Arithmetic Expansion #形式:$((expression)) tsc@tsc:~/playground$ echo $((2+2)) 4
brace expansion
括号中间不能有空格。
用途:批量创建有顺序的文件。
Parameter Expansion
Command Substitution
command的结果也可以作为expansion
echo $(ls)
双引号:Double Quotes
“$”, “” (backslash), and “`” (backquote) 失效,但是parameter expansion, arithmetic expansion, and command substitution 仍有效。
注意区别:
echo $(cal)
echo "$(cal)"
# 空格、tab、换行符都被用来分割,双引号则抑制。所以前一个命令不会换行,后一个会。
单引号:Single Quotes
抑制所有expansions
escape character
和双引号一起使用,抑制部分符号
8 Advanced Keyboard Tricks(高级键盘技巧)
这节讲键盘技巧,提高效率。主要是command line和history的技巧。command line的:移动,删除。history的上下一个命令。
- clear – Clear the screen
- history – Display the contents of the history list
9 Permissions(权限)
思考:多用户为什么存在?想想计算中心。
多用户存在要解决的问题:
- 一个用户的错误操作不能使计算机crash
- 一个用户不能影响其他用户
几个相关文件夹
/etc/passwd
User accounts .
/etc/group
groups
/etc/shadow
user's password
rwx Owner Group World
-
id – Display user identity
id uid=1000(tsc) gid=1000(tsc) groups=1000(tsc),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare) # root # id uid=0(root) gid=0(root) groups=0(root)
-
chmod – Change File Mode
chmod 600 foo.txt
-
umask – Set Default Permissions
-
su – Run A Shell With Substitute User And Group IDs
默认是superuser
su - exit
-
sudo – Execute A Command As Another User
纠正自己的认识,su,sudo是改变用户的,不一定是super,默认才是。
-
chown – Change File Owner And Group
-
chgrp – Change Group Ownership
-
passwd - change user password
10 Processes(进程)
Processes are how Linux organizes the different programs waiting for their turn at the CPU.
-
ps - report a snapshot of the current processes.
-
top - display Linux processes
-
& - Putting A Process In The Background
-
jobs - list the jobs that have been launched from our terminal.
-
fg - Returning A Process To The Foreground
为什么要回到前台来?后台的程序不受terminal的控制。
Ctrl-z - Stopping (Pausing) A Process,注意和ctrl-c的区别,c是结束。
-
bg - resume the program’s execution in the background
-
kill - “kill” processes
-
killall - kill processes by name
-
halt
-
poweroff
-
reboot
-
shutdown
Part two – 配置与环境变量
11 Environment(环境)
什么是environment?
configuration的作用:store program settings
-
printenv - print all or part of environment
# 所有environment tsc@tsc:~$ printenv | less # 某一个environment tsc@tsc:~$ printenv USER tsc@tsc:~$ echo $HOME
-
printenv 显示的一些常见变量:
SHELL PATH # 所有的,最新的
-
set #will show both the shell and environment variables
printenv和set的区别:printenv只显示environment variables,set显示shell和environment variables.
追加
PATH=$PATH:$HOME/bin
-
export # tells the shell to make the contents of PATH available to child processes of this shell.
序
Login Shell Sessions :
/etc/profile # global
~/.bash_profile
~/.bash_login # 如果上面那个不存在,读这个
~/.profile # 如果上面那个不存在,读这个
# .profile文件中有PATH,并且将$HOME/bin添加上了,所以启动系统后$HOME/bin中的命令是直接可以用的。$HOME是用户目录。
Non-Login Shell Sessions :
/etc/bash.bashrc # global
~/.bashrc # user's
Text Editors
有两种:
1.graphical :gedit
2.text based :nano, vi, emacs
nano
ctrl-x : exit
ctrl-o : save, write out
# : comments
使.bashrc生效
首先要知道原理,启动流程,在session一开始才会read .bashrc,所以需要重启terminal才会生效。当然,可以强行重读.bashrc,用source命令:
source .bashrc
12 Gentle Introduction To vi(vi 简介)
-
vi 操作
:q :q! *# 强制退出 i *# insert esc *# to command mode :w *# save ,有:的命令叫ex command # move h, j, k, l ctrl-f/b numberG gg # 第一个字符 G last line of the file 0 (zero) 行首 ^ To the first non-whitespace character on the current line. $ end of current line w beginning of next word or punctuation W ignoring punctuation b beginning of previous word or punctuation B ignoring punctuation # edit u # undo last change i # insert # append a # append, i是前插 A # 直接到最后append # opening a line o # 新建一行在下面 O # 新建一行在上面 # delete x # current character 3x dd # current line,d有cut的作用 dw # dW # ignoring punctuation d$ # current cursor to end d0 # current cursor to beginning dG # current cursor to end of file d20G # current cursor to 20 line,不是要删除20行 5dd # 5行 # Cutting, Copying, And Pasting p # 小写粘贴在后一行(dd),普通在光标前 P # 大写粘贴在前一行(dd),普通在光标后 p # paste after cursor P # upper case, paste before cursor # copy yy # copy current line 5yy # copy 5 line yW # ignoring punctuation yw # y$ # current cursor to end of line y0 # current cursor to beginning of line y^ # current cursor to first non-whitespace char yG # current cursor to last line of file y20G # current cursor to 20th line # redo,undo u # undo ctrl+r # redo J # join line # search, replace fa # 行内搜索a,按;继续搜索 / # 文件内搜索,n继续 :%s/line/Line/g # 替换 :%s/line/Line/gc # 有提醒的替换,%代表对所有内容进行操作 :1,2s/line/Line/gc # 对1-2行进行操作 Ctrl-e, Ctrl-y # Scroll down and scroll up
The leading tilde characters (”~”) indicate that no text exists on that line.
modal editor
command mode
Switching case of characters 大小写转换
You can change the case of text:
Toggle case “HellO” to “hELLo” with g~ then a movement.
Uppercase “HellO” to “HELLO” with gU then a movement.
Lowercase “HellO” to “hello” with gu then a movement.
Alternatively, you can visually select text then press ~to toggle case, or U to convert to uppercase, or u to convert to lowercase.
常规操作
# 到行尾
$ 反:0
A 反:I
Word movement: w, e, b
13 Customizing The Prompt(自定义提示)
- PS1-prompt string one
就是这个:
tsc@tsc:~$
更改它:
tsc@tsc:~$ PS1="<u@h w>$ "
<tsc@tsc ~>$
u # username of the current user.
h # Hostname of the local machine minus the trailing domain name.
w # Name of the current working directory.
$ # This displays a “$” character unless we have superuser privileges. In that case, it displays a “#” instead.
改颜色:
通过escape code 实现,escape code 的开头是八进制的033 ,例子: