原文发于我的独立博客:通过《The Linux Command Line》入门linux命令行
此书网站:The Linux Command Line ,它是免费的。
它有中文版,而且还有一个好听的名字:快乐的 Linux 命令行
学习有两种方法,一种是系统的学,一种是根据实际需求来学。两种各有优缺点,前一种,知识不会有缺漏,对日后的融会贯通和触类旁通有好处,但是这样慢,而且有可能不如针对性学习的精神集中。后一种,只找需求的学,能够任务完成就行,这样快,它的缺点就是前一种的优点。显然通看整本书属于第一种。
作者写的很好,娓娓道来,讲的很清楚,就像一本故事书一样。我很喜欢这样的书。作者比较幽默,一些用词看了会让人发笑。
这本书的内容很浅,真的只是入门。
正如作者所说,这是一场linux的journey(旅行),就把看书当旅行吧。
以下是随手记的,只适合自己看。
命令
date
cal
-calendardf
-current amount of free space on your disk drivesfree
-display the amount of free memoryexit
-closing the terminal emulator window
2 - Navigation
pwd
- Print name of current working directorycd
- Change directory
cd home directory
cd - previous working directory
cd ~user_name home directory of user
. 点号代表当前文件夹
ls
- List directory contents
ls -a all files
ls -l long format
ls -lt sort the result by the file's modification time
ls -lt --reverse
-
file
– Determine file type -
less
– View file contents和cat的区别?参考
4 - Manipulating Files And Directories
-
cp
如果文件已经存在,直接覆盖,没有提示,要提示用
-i
-
mv
move (rename) files
-
mkdir
mkdir dir1 dir2
rm
– Remove Files And Directories
rm -r playground # 递归删除
ln
– Create Links
ln myfile hard # hard link
ln -s myfile soft # symbolic link
软链接”和“硬链接”的区别:硬链接指向磁盘区块,软链接指向绝对路径。
hard links的缺点:
1.cannot span physical devices.
2.cannot reference directories, only files.
5 - Working With Commands
type
– Indicate how a command name is interpreted
tsc@tsc:~$ type ls
ls is aliased to `ls --color=auto' # alias
tsc@tsc:~$ type cd
cd is a shell builtin # builtin
tsc@tsc:~$ type cp
cp is /bin/cp # executable program
which
– Display An Executable’s Location
tsc@tsc:~$ which cp
/bin/cp
只用于exe
-
help
– Get help for shell builtins注意是shell builtin
tsc@tsc:/bin$ help cd
cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.
理解注释:[]
是可选,|
是互斥。
--help
– Display Usage Information
tsc@tsc:~$ mkdir --help
man
– Display a command’s manual page
没有example
tsc@tsc:~$ man ls
apropos
– Display a list of appropriate commandsinfo
– Display a command’s info entrywhatis
– Display a very brief description of a commandalias
– 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
终端关闭后作用就消失了。
去除:
tsc@tsc:~$ unalias foo
- trick:多条命令于一行
command1; command2; command3...
tsc@tsc:~$ cd /usr; ls
-
help最简单,info最详细,man在两者之间。
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 textuniq
- 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 filetail
- Output the last part of a filetee
- 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.老版本
# 顺序很重要
tsc@tsc:~$ ls -l /bin/usr > ls-output.txt 2>&1
# 2.新,用&>
tsc@tsc:~$ ls -l /bin/usr &> ls-output.txt
tsc@tsc:~$ ls -l /bin/usr &>> ls-output.txt # 追加
- Pipelines
tsc@tsc:~$ ls -l /usr/bin | less
千万不要把>
用成|
,不然有些后果很严重。
- filters
tsc@tsc:~$ ls /bin /usr/bin | sort | less
7 – Seeing The World As The Shell Sees It
这节讲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 -
Cursor Movement
-
completion
补全
-
history
history expansion-使用history中1209行的命令:
tsc@tsc:~$ !1209
- History Commands快捷键
9 – Permissions
思考:多用户为什么存在?想想计算中心。
多用户存在要解决的问题:
1.一个用户的错误操作不能使计算机crash
2.一个用户不能影响其他用户
- 几个相关文件夹
/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- File Modes中八进制的妙用
$ 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 2 – Configuration And The Environment
11 – The 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
-
Login和Non-Login的区别?
参考,
-
PATH的作用
命令从中找。
-
改环境变量时改哪个文件?
.profile
:
add directories
define additional environment variables
.bashrc
:
everything else
- 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 – A Gentle Introduction To vi
-
why vi?
always available
lightweight and fast
-
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
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
Linux vi and vim editor: Tutorial and advanced features
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 ,例子: