1. 空格
- 等号赋值两边不能有空格
- 命令与选项之间需要空格
- 管道两边空格可有可无
2. 方括号
[a=1]
等号为赋值语句, 两边没有空格- if语句
if [ "a" = "1" ];then echo a;fi
有空格, 因为[
为bash builtin test command的synonym, 是一个命令, 用]
表示命令的结束, 返回中间语句的exit status. 中间为参数, 需要空格隔开.- 中间的
=
为比较运算符, 需要用空格隔开.
3. 引号
- 单引号
'
full quoting
两个单引号包围起来的字符串就是普通的字符串,它将保留原始的字面意思. - 双引号
"
partial quoting
两个双引号包围起来的字符串,部分特殊字符将起到它们的作用.
这些特殊字符有:$
,\
,`
,!
. - 反引号`
两个反引号包围起来的字符串,将作为命令来运行,执行的输出结果作为该反引号的内容,称为命令替换,它有另一种更好的写法: $(command)
4. 转向符
>
> filename
输出保存在filename中<
< filename
将filename作为输入|
Useful for chaining commands, scripts, files, and programs together.
cat *.txt | sort | uniq > result-file
ssh user@host "cat > remote_dst; command; cat remote_src" < local_src > local_dst
- Copies
local_src
toremote_dst
, - Executes
command
, - Copies
remote_src
tolocal_dst
.
5. find
find / -name "wa*"#查找所有目录文件名为wa*的文件
find / -iname '*ensembl*'
6. which
which指令会在环境变量$PATH设置的目录里查找符合条件的文件。
7. 打包文件
.tar[.gz|.xz|.bz2|.lz]
打包[压缩]tar -[z|J|j|--lzip]cvf archive.tar.gz originalfiles
[解压]解包tar -[z|J|j|--lzip]xvf archive.tar.gz -C dir
-t
查看包
-a
根据后缀压缩/解压
--strip=n
忽略前n个目录.zip
压缩:zip -rv zipfile.zip originalfiles
解压:unzip zipfile.zip [-d dir]
合并压缩文件zip -s 0 split.zip -O unsplit.zip
解压分卷压缩zip -s 0 split.zip - | unzip - -d dir
(未测试).gz
压缩:gzip originfile
解压:gunzip zipfile.gz
,gzip -d zipfile.gz
-k
保留原文件
gzip -c originfile > dir/zipfile.gz
压缩至指定位置
8. 查找命令
查找该命令的内容
type -a python3
- determines if command is alias, function, built-in command, cached executable (hashed), binary in
$PATH
, etc - shell builtin
- determines if command is alias, function, built-in command, cached executable (hashed), binary in
which python3
- finds executable in your
$PATH
- lives in /usr/bin/which
- finds executable in your
9. 查看进程
ps
ps aux | less
显示所有运行中的进程
ps -u vivek
查看用户vivek运行的进程top
显示运行中系统的动态实时视图
按q退出,按h进入帮助pstree
以树状显示正在运行的进程pgrep
查找进程
pgrep -u root sshd