refers to a program in execution. it is an running instance of program.
- 前台进程
- 后台进程 process not connected to the terminal
- 守护进程:后台进程的特殊类型,它们在系统启动时启动,并作为服务一直运行;它们不会死亡。它们自发地作为系统任务启动(作为服务运行)。但是,它们能被用户通过
init
进程控制。
- 守护进程:后台进程的特殊类型,它们在系统启动时启动,并作为服务一直运行;它们不会死亡。它们自发地作为系统任务启动(作为服务运行)。但是,它们能被用户通过
程序由它的进程 ID(PID process ID)和它父进程的进程 ID(PPID parent process ID)识别,因此进程可以被分类为:
- 父进程 - 这些是在运行时创建其它进程的进程。
- 子进程 - 这些是在运行时由其它进程创建的进程。
init 进程:系统中所有进程的父进程。
process status
进程的一次性查看,查看结果并不动态连续
ps命令能够给出当前系统中进程的快照。它能捕获系统在某一事件的进程状态。
cr% ps
PID TTY TIME CMD
11066 pts/4 00:00:00 fish
11114 pts/4 00:00:00 zsh
11619 pts/4 00:00:00 ps
- PID: 运行着的命令(CMD)的进程编号
- TTY: 命令所运行的位置(终端)
- TIME: 运行着的该命令所占用的CPU处理时间
- CMD: 该进程所运行的命令
使用 -a 参数。-a 代表 all。同时加上x参数会显示没有控制终端的进程。
[ccrr@cr ~]$ ps -ax
PID TTY STAT TIME COMMAND
1 ? Ss 0:00 /sbin/init
使用-u,加上用户名,查看该用户的进程。
ccrr@cr ~ [1]> ps -u ccrr
PID TTY TIME CMD
497 ? 00:00:00 systemd
498 ? 00:00:00 (sd-pam)
507 ? 00:00:00 kwalletd5
...
ps -aux | less
显示所有进程(与-ax
的区别在于输出内容有user信息)
根据 CPU 使用来升序排序(+就是降序)
$ ps -aux --sort -pcpu | less
根据 内存使用 来升序排序
$ ps -aux --sort -pmem | less
$ ps -aux --sort -pcpu,+pmem | head -n 10
显示前10个结果
cr% ps -C fish
PID TTY TIME CMD
1557 pts/1 00:00:00 fish
1608 pts/1 00:00:00 fish
4731 pts/3 00:00:00 fish
13254 pts/5 00:00:00 fish
19398 pts/2 00:00:00 fish
-C参数用来查找所有给定名字的进程
再加上参数**-f**显示更多信息
ps -L PID
展示特定的进程的所有线程
pstree
显示进程的树状结构
链接https://linux.cn/article-4743-1.html
real user ID,永远不会变:当一个用户登录系统之后,该shell的ruid就是改用户了。从该根shell产生的所有进程都会继承ruid,即该用户。
effective user id,如果user1的可执行文件设置了suid位,且该文件可被user2执行,那么user2执行时,该进程的euid是user1,而ruid是user2.
因为user2登录后去执行,所以ruid是user2; 因为user1设置了suid位且该文件是user1的,所以euid是user1.
cr% ps -U root -u root u
-U
筛选ruid
-u
筛选euid
u
针对用户的格式输出
运行系统的动态实时视图
A
sort process with PID
top -u username
c
show absolute path
Press (Shift+P) to sort processes as per CPU utilization.
https://www.tecmint.com/12-top-command-examples-in-linux/
用于删除执行中的程序或工作。
kill [参数] PID
The kill
command sends a signal to specified processes or process groups, causing them to act according to the signal. When the signal is not specified, it defaults to -15
(-TERM).
The most commonly used signals are:
1
(HUP
) - Reload a process.9
(KILL
) - Kill a process.15
(TERM
) - Gracefully stop a process. (优雅的)
see detailed information use command kill -l
如果已经知道杀死进程的名字,可以使用
killall -9 firefox
来杀死有关的所有进程
pkill -u username
pkill '正则表达式'
作为通信的端点
每个端口都会与主机的IP地址与通信协议相关
用16bit数字表示
lsof -i:端口号
查看特定端口号的占用情况
netstat -tunlp
用于显示 tcp,udp 的端口和进程等相关情况
netstat -tunlp | grep 端口号
- -t (tcp) 仅显示tcp相关选项
- -u (udp)仅显示udp相关选项
- -n 拒绝显示别名,能显示数字的全部转化为数字
- -l 仅列出在Listen(监听)的服务状态
- -p 显示建立相关链接的程序名
80端口是否被占用?
netstat -tunlp | grep 80
可以查询到端口信息。
ps -aux | grep [端口]
若被占用,是哪个进程占用了?
http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html
太难了,不知道什么意思。
"守护进程"(daemon)就是一直在后台运行的进程(daemon)。(区分前台任务和后台任务)
systemd 一组命令
systemctl
systemd-analyze
hostnamectl
localectl
timedatectl
loginctl
systemctl list-units
unit状态 systemctl status
systemctl is-active/is-failed/is-enabled application.servoce
system start/stop/restart/killr/reload/show service
- less
b
上翻一页d
后翻半页N
显示行号
- more
- 查看纯文本文件,文字较多的文件
- head
- 查看前N行的文件内容
head [operator] [共几行] filepath
- tail
- 查看后N行的文件内容
- 静态:
tail [operator(-n)] [共几行] filepath
- 动态:
tail -f [共几行] filepath
- cat
- 查看纯文本文件
- 加入参数
-n
以显示行号
- grep
grep [选项] 'string' filepath
-b
将binary当作文本文件(text)来搜索
-c
仅显示找到的行数
-i
忽略大小写
-n
显示行号
-v
反向选择——仅列出没有”关键词“的行
- awk
更适合格式化文本
awk [选项参数] 'script' var=value file(s)
awk [选项参数] -f scriptfile var=value file(s)
用法一:
awk '{[pattern] action}' {filenames}
例如:
$ awk '{print $1,$4}' log.txt 输出每行第一个单词和第四个单词
用法二:
awk -F
$ awk -F, '{print $1,$2}' log.txt
相比默认的用空格和制表符进行分割,-F
规定了用,进行分割,输出第一部分和第二部分
或使用另一种表达方式:
$ awk 'BEGIN{FS=","} {print $1,$2}' log.txt
# 使用多个分隔符.先使用空格分割,然后对分割结果再使用","分割
$ awk -F '[ ,]' '{print $1,$2,$5}' log.txt
用法三:
awk -v # 设置变量
$ awk -va=1 '{print $1,$1+a}' log.txt
$ awk -va=1 -vb=s '{print $1,$1+a,$1b}' log.txt
如果$1是字符串,+数值的话起始值为0.$1b直接在后面加上字符
$ awk '$1>2' log.txt #命令
$ awk '$1==2 {print $1,$3}' log.txt
$ awk 'BEGIN{printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n","FILENAME","ARGC","FNR","FS","NF","NR","OFS","ORS","RS";printf "---------------------------------------------\n"} {printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n",FILENAME,ARGC,FNR,FS,NF,NR,OFS,ORS,RS}' log.txt
FILENAME ARGC FNR FS NF NR OFS ORS RS
---------------------------------------------
log.txt 2 1 5 1
log.txt 2 2 5 2
log.txt 2 3 3 3
log.txt 2 4 4 4
$ awk '$2 ~ /th/ {print $2,$4}' log.txt
# 输出包含 "re" 的行
$ awk '/re/ ' log.txt
~表示模式开始 //中是模式
上句是说在第二列中找出有th的字符串的一行,打印出第二列和第四列
$ awk 'BEGIN{IGNORECASE=1} /this/' #log.txt 忽略大小写
将~!
代替~
表示取反
关于 awk 脚本,我们需要注意两个关键词 BEGIN 和 END。
- BEGIN{ 这里面放的是执行前的语句 }
- END {这里面放的是处理完所有的行后要执行的语句 }
- {这里面放的是处理每一行时要执行的语句}
这里比较复杂,可以看网站进行查询:https://www.runoob.com/linux/linux-comm-awk.html
NF | 一条记录的字段的数目 |
NR | 已经读出的记录数,就是行号,从1开始 |
FILENAME | |
FS | 字段分隔符 |
RS | 记录分隔符 |
当前目录下有几个文件?
文件的详细信息:
ls -al
计数:
ls -al | awk END {print NR-1}
记录一个文件一共有多少字?
awk -vsum=0 '{sum+=NF} END {print sum}'
seq 9 | sed 'H;g' | awk -v RS='' '{for(i=1;i<=NF;i++)printf("%dx%d=%d%s", i, NR, i*NR, i==NR?"\n":"\t")}'
额外加入
wc
参数:
-l
只显示行数
-w
只显示单词数
-c
只显示字节数
# echo "qwert" | passwd --stdin rusky
STDIN 0
STDOUT 1
STDERR 2
输入指令时可能有内容会在屏幕上显示,可以通过IO重定向改变这个输出方向。
> :若文件不存在,则创建,若文件已存在,则覆盖
2> : 将错误重定向到一个文件
>>: 若文件不存在,则创建,若文件已存在,则追加
2>>:同理
命令>>文件 2>&1 或者 命令 &>> 文件:将标准输出和错误输出共同写入到文件中
命令< file : 将file作为标准输入文件
? 命令<< 分界符:从标准输入中读入,直到遇见分界符才停止
命令<文件1>文件2: 将文件1作为标准输入并标准输出到文件2
pwd
当前绝对路径
$0
脚本名称
$#
参数总数
$1
第一个参数
$?
上一次执行命令的返回值
[ 条件表达式 ]
-d 文件是否为目录类型
-e 文件是否存在(实际上不管是文件还是文件夹)
-f 是否为一般文件
-r 是否有读权限
-w 是否优选先写
-x 是否有权限运行
[ -d directory]
如果是directory,返回0。否则返回1
-eq
-ne
-gt
-lt
-le
-ge
其他:free
内存使用量情况
= 相等
!= 不等
-z 判断为空
if [ judgement ]
then
...
elif [ judgement ]
then
...
else
...
fi
循环语句
for value in valuelist
do
...
done
while [judgement]
do
...
done
case
case 变量值 in
mode1 )
....
;;
mode2 )
...
;;
mode3 )
...
;;
*)
...
esac
1 read -p "please enter one character: " C
2 case "$C" in
3 [a-z]|[A-Z])
4 echo "this is english character"
5 ;;
6 [0-9])
7 echo "this is number"
8 ;;
9 *)
10 echo "this is other token"
11 esac
我是无情的搬运机器:https://www.fujieace.com/blackarch-linux/pacman.html
1、列出已经安装的软件包
pacman -Q
2、查看sqlmap包是否已经安装
pacman -Q sqlmap
3、查看已安装的包sqlmap的详细信息
pacman -Qi sqlmap
4、列出已安装包sqlmap的所有文件
pacman -Ql sqlmap
5、查找某个文件属于哪个包?
pacman -Qo /etc/passwd
6、查询包组
pacman -Sg
7、查询包组所包含的软件包
pacman -Sg blackarch
8、搜索sqlmap相关的包
pacman -Ss sqlmap
9、从数据库中搜索sqlmap的信息
pacman -Si sqlmap
10、仅同步源
sudo pacman -Sy
10、更新系统
sudo pacman -Su
11、同步源并更新系统
sudo pacman -Syu
12、同步源后安装sqlmap包
sudo pacman -Sy sqlmap
13、从本地数据库中获取sqlmap的信息,并下载安装
sudo pacman -S sqlmap
14、强制安装sqlmap包
sudo pacman -Sf sqlmap
15、删除sqlmap
sudo pacman -R sqlmap
16、强制删除被依赖的包(慎用)
sudo pacman -Rd sqlmap
17、删除sqlmap包及依赖其的包
sudo pacman -Rc sqlmap
18、删除sqlmap包及其依赖的包
sudo pacman -Rsc sqlmap
19、清理/var/cache/pacman/pkg目录下的旧包
sudo pacman -Sc
20、清除所有下载的包和数据库
sudo pacman -Scc
21、安装下载的virtualbox包(有时候需要降级包的时候就用这个)
cd /var/cache/pacman/pkgsudo
pacman -U virtualbox-5.2.12-1-x86_64.pkg.tar.xz
22、升级时不升级sqlmap包
sudo pacman -Su --ignore sqlmap
https://www.liaoxuefeng.com/wiki/896043488029600
git init
变成git可以管理的仓库
git add
git commit -m ""
ccrr@cr ~/D/learngit (master)> git log --pretty=oneline
963bfb8949b3d2978c5a9201f57094772ed6d83b (HEAD -> master) 3
d253da7c08b3c2e79910a11decbd5cac06e25d6c 2
ea6a4faa8732c6f20554d7b633d157bc474bd9f6 1
一长串数字是版本号
自选方案:
ccrr@cr ~/D/learngit (master)> git log --graph --pretty=oneline --abbrev-commit
* 3bcc0df (HEAD -> master) conflict fixed
|\
| * 6172434 (feature1) feature 1 first commit
* | 822f824 &&
|/
* 6888ed0 branch test
* 51f676c (origin/master) remove test.txt
* 4bf3c4e add test.txt
* 81d3776 try
* 581c101 understand stage
* 963bfb8 3
* d253da7 2
* ea6a4fa 1
可以根据git log的内容查看要回退的版本
git reset --hard HEAD^
或者可以将HEAD^换成版本号(根据git log),既可以前进又可以回退
查看 head指针的具体情况,也可以使用命令git reflog
可以通过git status
来查看是否有文件没有被加入暂存区,是否有暂存区的文件没有提交到对应的分支。
检查working directory和分支上的不同
git diff HEAD -- readme.txt
HEAD
指针
如果文件已经添加到了暂存区,然后在工作区做了一些修改,但是想丢弃修改,可以使用git status查看修改的命令,目前的命令是git restore <file>
$ git clone [email protected]:michaelliao/gitskills.git
默认使用ssh,也可以使用https
将working directory的制定file添加到暂存区(stage,index)
git add <files>
将暂存区内容提交到当前分支。
git commit -m "info"
git rm <file>
git checkout -b dev
创建并切换到dev分支
相当于下面两条指令
git branch dev
git checkout dev
通过git branch
来查看所有分支。现在HEAD指针只想dev
在dev上修改内容提交之后,回到master分支,现在HEAD指针指向master,发现master的文件并没有修改。可以通过git merge dev
来进行合并。
git branch dev #创建dev分支
git branch -d dev #删除分支
git merge [branchname] #没有冲突的情况下可能进入fast forward 模式,删除分支后就会丢掉分支信息
可能会有conflict可以进入merging修改文件内容
git merge --no-ff -m "merge with no-ff" dev #这样就不会丢掉分支dev的信息
也可以通过switch来切换分支。
git switch -c dev #创建并切换
git switch master #切换到已有的分支
git restore file #放在暂存区和工作区的file内容不同,将工作区的file还原为暂存区的file
git restore --staged file # 撤回放在暂存区的file
git fetch
是将远程主机的最新内容拉到本地,用户在检查了以后决定是否合并到工作本机分支中。
而git pull
则是将远程主机的最新内容拉下来后直接合并,即:git pull = git fetch + git merge
,这样可能会产生冲突,需要手动解决。
git fetch 远程主机名 [分支名]
在多人协作中,git push会有冲突。需要先pull下最新的内容,注意需要指定本地dev
分支与远程origin/dev
分支的链接
git branch --set-upstream-to=origin/dev dev
git remote add origin "" #关联远程库
git remote -v
git remote rm origin
git push -u origin master
由于远程库是空的,我们第一次推送master
分支时,加上了-u
参数,Git不但会把本地的master
分支内容推送的远程新的master
分支,还会把本地的master
分支和远程的master
分支关联起来,在以后的推送或者拉取时就可以简化命令。
后面并不需要加上-u
参数
也可以
git push origin dev
加入dev分支
gitclone之后只有master一个分支,还需要创建其他分支:
git checkout -b dev origin/dev
在多人协作中,git push会有冲突。需要先pull下最新的内容,注意需要指定本地dev
分支与远程origin/dev
分支的链接
git branch --set-upstream-to=origin/dev dev
手动解决conflict后,再push
修复bug时,我们会通过创建新的bug分支进行修复,然后合并,最后删除;
当手头工作没有完成时,先把工作现场git stash
一下,然后去修复bug,修复后,再git stash pop
,回到工作现场;
通过
git stash list
进行查看
git stash apply
git stash list
在master分支上修复的bug,想要合并到当前dev分支,可以用git cherry-pick <commit>
命令,把bug提交的修改“复制”到当前分支,避免重复劳动。commit可以通过log进行查看
面向vimtutor的学习。
^
k Hint: The h key is at the left and moves left.
< h l > The l key is at the right and moves right.
j The j key looks like a down arrow.
v
i
insert
a
append
o
在光标下再创建一行
dw
delete a word from the cursor's next character,不留下空格
d$
delete from the cursor to the end of the line
de
会删除从cursor开始的一个词语,但留下空格
d number motion: like
d2wdelete 2 word while
dwhas the same function of
d1w`
[number]dd
: delete lines of certain number(d[number]d
也有同样作用)
2w
two words end
3e
three word start
0
start of the line
u
undo command
U
return the whole line into the original state(but what is original state? how to define it?)
ctrl-R
redo functions; undo the undos
p
put (use after dd, dw ...) after the cursor
r[ ]
replace the character under the cursor with [ ]
ce
delete characters in one word from the cursor, including the word under cursor and will change into insert mode
cc
does the same job as ce
but... delete the whole line and go to insert mode
c[number][motion]
work the same as delete, but when finish deletion, it change into insert mode
motion: $ w
c1w
= ce
c1$
= cc
ctrl + G
将会有提示:"/tmp/tutortQa0wH" [Modified] line 512 of 969 --52%-- col 11
[number]G
: jump to number line
G
move to bottom of the file(cursor在最前面)
gg
移到最开始处(cursor在最前面)
/[word]
search from the beginning
?[word]
search from the end
n
next one
N
previous one
返回(go back)search结果:ctrl+O
"undo the undo" search结果:ctrl + I
将光标放置在一个括号上,输入%
将回跳到对应括号
:s/old/new/g
不加g
说明只作用于该行的一个单词
g
: in one line globally
:%s/old/new/g
change in whole file
:%s/old/new/g
有提示符
:#1,#2s/old/new/g
from #1 line to #2 line change old to new globally
:![command]
:!ls
:!dir
list directory
:w filename
将当前文件保存到当前目录
:!rm filename
v
enter into visual mode
选择某一段保存到文件的方法
2. Press v and move the cursor to the fifth item below. Notice that the
text is highlighted.
3. Press the : character. At the bottom of the screen :'<,'> will appear.
4. Type w TEST , where TEST is a filename that does not exist yet. Verify
that you see :'<,'>w TEST before you press <ENTER>.
5. Vim will write the selected lines to the file TEST. Use :!dir or :!ls
to see it. Do not remove it yet! We will use it in the next lesson.
利用v还有对应操作:
vc
vd
:r filename
:r !dir
将shell输出的信息添加入文件中
o
在cursor下方插入一行并进入insert mode
O
在cursor上方插入一行并进入insert mode
a
insert 从cursor的下一个开始输入
i
insert 从cursor前加入
e
跳转到下一个单词的结尾
R
y
yank
- 进入visual mode进行选择文字
yw
复制一个单词yy
复制一行内容
p
paste,put
贴上原文以便查询
Lesson 6.5: SET OPTION
** Set an option so a search or substitute ignores case **
1. Search for 'ignore' by entering: /ignore <ENTER>
Repeat several times by pressing n .
2. Set the 'ic' (Ignore case) option by entering: :set ic
3. Now search for 'ignore' again by pressing n
Notice that Ignore and IGNORE are now also found.
4. Set the 'hlsearch' and 'incsearch' options: :set hls is
5. Now type the search command again and see what happens: /ignore <ENTER>
6. To disable ignoring case enter: :set noic
NOTE: To remove the highlighting of matches enter: :nohlsearch
NOTE: If you want to ignore case for just one search command, use \c
in the phrase: /ignore\c <ENTER>
所有相关命令展示ctrl + D
see detailed information in vim-tutorial from github
If you want to open the file hello.txt
and immediately execute a command, you can pass to the vim
command the +{cmd}
option.
In Vim, you can substitute texts with the :s
command (short for :substitute
). If you want to open hello.txt
and substitute all "pancake" with "bagel", run:
vim +%s/pancake/bagel/g hello.txt
vim -c %s/pancake/bagel/g hello.txt
两种方式都可以,-c可以代替加号
suspend ctrl+z
continue: fg
:qall exit all files
打开一个文件后,用命令:split anotherfile来打开另一个文件
Ctrl-W H Moves the cursor to the left window
Ctrl-W J Moves the cursor to the window below
Ctrl-W K Moves the cursor to the window upper
Ctrl-W L Moves the cursor to the right window
:buffer hello2 #在另一个窗口输入这个指令,将该文件的内容与hello2的buffer同步
:tabnew file.txt Open file.txt in a new tab
:tabclose Close the current tab
:tabnext Go to next tab
:tabprevious Go to previous tab
:tablast Go to last tab
:tabfirst Go to first tab
To start Vim with multiple tabs, you can do this from the terminal:
vim -p file1.js file2.js file3.js
gt/gT 也可以跳转tab
h Left
j Down
k Up
l Right
w Move forward to the beginning of the next word
} Jump to the next paragraph
$ Go to the end of the line
y Yank text (copy)
d Delete text and save to register
c Delete text, save to register, and start insert mode
gug 大小写
gUg
b ge 上移一个单词
w e 下移一个单词
移动
0
^
g_
$
n|
gg #jump to 1 line
G # jump to last line
nG # jump to nth
line
n%
H
M
L
nH
nL
最最基础的用法
main.c文件:
#include <stdio.h>
int main()
{
printf("Hello world");
return 0;
}
CMakeLists.txt
PROJECT (HELLO)
SET(SRC_LIST main.c)
MESSAGE(STATUS "This is binary dir " ${HELLO_BINARY_DIR})
MESSAGE(STATUS "This is source dir " ${HELLO_SOURCE_DIR})
ADD_EXECUTABLE(hello main.c)
PROJECT(projectname [language])
SET(SRC_LIST [value ... (file)])