Featured image of post 常用Shell命令

常用Shell命令

一些常见常用的shell命令

常用系统工作命令

echo

1
2
3
4
pos2@instance-20220901-0057:~$ echo "WDNMD"
WDNMD
pos2@instance-20220901-0057:~$ echo $SHELL
/bin/bash

data 参数以‘+’开头

1
2
3
4
pos2@instance-20220901-0057:~$ date "+%j"
260
pos2@instance-20220901-0057:~$ date "+%Y-%m-%d %H:%M:%S"
2022-09-17 17:25:30

reboot 重启

poweroff 关机

wget

  • -b 后台下载
  • -P 下载到指定目录
  • -t 最大尝试次数
  • -c 断点续传
  • -p 下载页面内所有资源,图片视频等
  • -r 递归下载

ps

  • -a 显示所有进程
  • -u 用户以及其他详细信息
  • -x 显示没有控制终端的进程
1
2
3
4
5
6
pos2@instance-20220901-0057:~$ ps -aux | grep nginx
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root        3984  0.0  0.1  55284  1528 ?        Ss   Sep02   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
pos2        3985  0.0  0.4  56120  4780 ?        S    Sep02   0:00 nginx: worker process
pos2        3986  0.0  0.8  58664  8636 ?        S    Sep02   0:10 nginx: worker process
pos2      175022  0.0  0.0   8168   720 pts/1    S+   17:36   0:00 grep --color=auto nginx

top

相当于任务管理器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
pos2@instance-20220901-0057:~$ top
top - 17:53:17 up 15 days,  7:20,  4 users,  load average: 0.05, 0.01, 0.00
Tasks: 143 total,   1 running, 142 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :    964.2 total,     89.7 free,    276.6 used,    597.9 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used.    534.8 avail Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
    731 root      20   0   81832   3064   2768 S   0.3   0.3   0:41.98 irqbalance
      1 root      20   0  169472  11624   7040 S   0.0   1.2   0:47.87 systemd
      2 root      20   0       0      0      0 S   0.0   0.0   0:00.17 kthreadd
      3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp
      4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp
      5 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 netns
      7 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/0:0H-events_highpri
     10 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 mm_percpu_wq
     11 root      20   0       0      0      0 S   0.0   0.0   0:00.00 rcu_tasks_rude_
     12 root      20   0       0      0      0 S   0.0   0.0   0:00.00 rcu_tasks_trace
     13 root      20   0       0      0      0 S   0.0   0.0   0:08.12 ksoftirqd/0
     14 root      20   0       0      0      0 I   0.0   0.0   0:31.91 rcu_sched
     15 root      rt   0       0      0      0 S   0.0   0.0   0:11.82 migration/0
     16 root     -51   0       0      0      0 S   0.0   0.0   0:00.00 idle_inject/0
     17 root      20   0       0      0      0 S   0.0   0.0   0:00.00 cpuhp/0
     18 root      20   0       0      0      0 S   0.0   0.0   0:00.00 cpuhp/1

pidof 查询每个指定服务进程的pid

1
2
3
4
pos2@instance-20220901-0057:~$ pidof sshd
175448 175355 175298 175280 175244 175166 175093 175076 175010 174932 174859 174842 174661 174602 174492 174473 785
pos2@instance-20220901-0057:~$ pidof nginx
3986 3985 3984

kill 杀死进程 killall 杀死服务对应全部进程

1
2
pos2@instance-20220901-0057:~$ kill 3986
pos2@instance-20220901-0057:~$ killall nginx

系统状态检查命令

uname 查看内核与系统版本

1
2
pos2@instance-20220901-0057:~$ uname -a
Linux instance-20220901-0057 5.15.0-1016-oracle #20~20.04.1-Ubuntu SMP Mon Aug 8 07:20:11 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

uptime 查看负载信息

1
2
3
# 最近1min, 5min, 15min的压力情况
pos2@instance-20220901-0057:~$ uptime
 17:59:09 up 15 days,  7:26,  4 users,  load average: 0.22, 0.07, 0.02

free 查看内存使用量

1
2
3
4
pos2@instance-20220901-0057:~$ free -h
              total        used        free      shared  buff/cache   available
Mem:          964Mi       272Mi        93Mi       1.0Mi       598Mi       538Mi
Swap:            0B          0B          0B

who 看当前登入主机的用户终端信息

1
2
3
4
5
6
# 网络不好,老是掉了重连。。
pos2@instance-20220901-0057:~$ who
pos2     pts/0        2022-09-17 17:22 (120.229.34.76)
pos2     pts/1        2022-09-17 17:35 (120.229.34.76)
pos2     pts/2        2022-09-17 17:46 (120.229.34.76)
pos2     pts/3        2022-09-17 17:52 (120.229.34.76)

last 查看所有系统登陆记录

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
pos2@instance-20220901-0057:~$ last
pos2     pts/3        120.229.34.76    Sat Sep 17 17:52   still logged in
pos2     pts/2        120.229.34.76    Sat Sep 17 17:46   still logged in
pos2     pts/1        120.229.34.76    Sat Sep 17 17:35   still logged in
pos2     pts/0        120.229.34.76    Sat Sep 17 17:22   still logged in
pos2     pts/1        116.24.64.8      Thu Sep 15 11:11 - 17:21 (1+06:10)
pos2     pts/0        116.24.64.8      Wed Sep 14 14:22 - 13:08  (22:46)
pos2     pts/0        116.24.64.8      Wed Sep 14 11:34 - 14:21  (02:46)
pos2     pts/1        121.35.45.70     Tue Sep 13 16:04 - 16:47  (00:42)
pos2     pts/0        121.35.45.70     Tue Sep 13 15:56 - 16:14  (00:18)
pos2     pts/0        121.35.45.70     Tue Sep 13 11:08 - 15:25  (04:17)
pos2     pts/1        116.26.74.219    Mon Sep 12 12:53 - 16:13  (03:19)
pos2     pts/0        116.26.74.219    Mon Sep 12 12:23 - 16:13  (03:49)
pos2     pts/0        116.26.72.117    Sun Sep 11 20:43 - 23:38  (02:55)
pos2     pts/0        120.239.23.128   Sun Sep 11 10:27 - 12:44  (02:16)
pos2     pts/1        120.239.23.128   Sun Sep 11 08:24 - 08:54  (00:29)
pos2     pts/0        120.239.23.128   Sun Sep 11 06:50 - 10:04  (03:13)
pos2     pts/1        121.35.45.70     Fri Sep  9 16:01 - 17:20  (01:18)
pos2     pts/0        121.35.45.70     Fri Sep  9 15:58 - 18:11  (02:12)

history

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
pos2@instance-20220901-0057:~$ history
  549  ls -l
  550  top
  551  pidof sshd
  552  pidof nginx
  553  uname -a
  554  uptime
  555  free -h
  556  who
  557  last
  558  history

工作目录切换命令

pwd

1
2
pos2@instance-20220901-0057:~$ pwd
/home/pos2

cd

1
2
3
pos2@instance-20220901-0057:~$ cd /etc
pos2@instance-20220901-0057:/etc$ pwd
/etc

ls

  • -a 查看所有文件包括隐藏文件
  • -l 查看文件属性
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
pos2@instance-20220901-0057:/etc$ ls -al
total 864
drwxr-xr-x 101 root        root         4096 Sep 17 06:23 .
drwxr-xr-x  19 root        root         4096 Sep  2 10:33 ..
-rw-------   1 root        root            0 Aug 16 09:44 .pwd.lock
drwxr-xr-x   3 root        root         4096 Aug 16 09:45 ModemManager
drwxr-xr-x   3 root        root         4096 Aug 16 09:45 NetworkManager
drwxr-xr-x   2 root        root         4096 Aug 16 09:46 PackageKit
drwxr-xr-x   4 root        root         4096 Aug 16 09:45 X11
-rw-r--r--   1 root        root         3028 Aug 16 09:44 adduser.conf
drwxr-xr-x   2 root        root         4096 Sep  1 10:45 alternatives
drwxr-xr-x   3 root        root         4096 Aug 16 09:45 apparmor
drwxr-xr-x   7 root        root         4096 Aug 16 09:46 apparmor.d
drwxr-xr-x   3 root        root         4096 Aug 16 09:46 apport
drwxr-xr-x   7 root        root         4096 Aug 16 09:44 apt
-rw-r-----   1 root        daemon        144 Nov 13  2018 at.deny

文本文件编辑命令

cat (-n可显示行号)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
pos2@instance-20220901-0057:~/mynigga$ cat content/post/cloudflare/index.md -n
     1  ---
     2  title: "在Cloudflare托管域名&部署证书"
     3  date: 2022-09-08T00:27:58+08:00
     4  draft: true
     5  categories: ["tech"]
     6  image: "https://www.cloudflare.com/static/b30a57477bde900ba55c0b5f98c4e524/Cloudflare_default_OG_.png"
     7  tags: []
     8  ---
     9
    10  ## 简单记录下在*cloudflare*代理域名的相关
    11  ### 添加站点-> 添加解析记录
    12  ![添加解析记录](p1.png)

more 查看长文本

head -n 查看文本前n行 tail -n 查看文本后n行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
pos2@instance-20220901-0057:~/mynigga$ head -n 10 config.yaml
baseurl: https://blog.pos2.fun
languageCode: en-us
theme: hugo-theme-stack
paginate: 5
title: Sup My G


# Change it to your Disqus shortname before using
disqusShortname: hugo-theme-stack

pos2@instance-20220901-0057:~/mynigga$ tail -n 10 config.yaml
        ordered: true
        startLevel: 2
    highlight:
        noClasses: false
        codeFences: true
        guessSyntax: true
        lineNoStart: 1
        lineNos: true
        lineNumbersInTable: true
        tabWidth: 4

# 可以持续刷新一个文件的内容,tail -f doc.txt

tr 替换文本字符

eg:小写变大写:cat anaconda-ks.cfg | tr [a-z] [A-Z]

wc 统计文本行数,字数,字节数

  • -l 只显示行数
  • -w 只显示单词数
  • -c 只显示字节数
1
2
pos2@instance-20220901-0057:~$ wc -l /etc/passwd
39 /etc/passwd

stat 查看文件的存储信息、时间信息

1
2
3
4
5
6
7
8
9
pos2@instance-20220901-0057:~/mynigga$ stat bk_config.toml
  File: bk_config.toml
  Size: 82              Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d      Inode: 774333      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1002/    pos2)   Gid: ( 1002/    pos2)
Access: 2022-09-15 14:01:27.246553794 +0800
Modify: 2022-09-02 14:37:45.475057378 +0800
Change: 2022-09-02 14:42:54.894439119 +0800
 Birth: -

cut 按列提取文本字符

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 参数-f 要看的列数 -d 分隔符
pos2@instance-20220901-0057:~$ cut -d: -f1 /etc/passwd | head -n 20
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
www-data
backup
list
irc
gnats
nobody
systemd-network
systemd-resolve

differ 比较文本文件差异

  • –brief 显示比较后结果,判断是否相同
  • -c 描述具体不同
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
pos2@instance-20220901-0057:~/mynigga$ cp bk_config.toml bb_config.toml
pos2@instance-20220901-0057:~/mynigga$ diff --brief bk_config.toml bb_config.toml
pos2@instance-20220901-0057:~/mynigga$ vim bb_config.toml
pos2@instance-20220901-0057:~/mynigga$ diff --brief bk_config.toml bb_config.toml
Files bk_config.toml and bb_config.toml differ

pos2@instance-20220901-0057:~/mynigga$ diff -c bk_config.toml bb_config.toml
*** bk_config.toml      2022-09-02 14:37:45.475057378 +0800
--- bb_config.toml      2022-09-17 18:38:50.352304805 +0800
***************
*** 1,3 ****
--- 1,4 ----
  baseURL = 'http://example.org/'
  languageCode = 'en-us'
  title = 'My New Hugo Site'
+ WDNMD

文件目录管理命令

touch 用于创建空白文件或设置文件时间

  • -a 读取时间
  • -m 修改时间
  • -d atime & mtime
1
2
3
4
5
6
7
8
9
pos2@instance-20220901-0057:~/trydir$ touch sth.txt
pos2@instance-20220901-0057:~/trydir$ ls -l sth.txt
-rw-rw-r-- 1 pos2 pos2 0 Sep 17 21:17 sth.txt
pos2@instance-20220901-0057:~/trydir$ echo "sup my nigga" >> sth.txt
pos2@instance-20220901-0057:~/trydir$ ls -l sth.txt
-rw-rw-r-- 1 pos2 pos2 13 Sep 17 21:18 sth.txt
pos2@instance-20220901-0057:~/trydir$ touch -d "2022-09-17 21:00" sth.txt
pos2@instance-20220901-0057:~/trydir$ ls -l sth.txt
-rw-rw-r-- 1 pos2 pos2 13 Sep 17 21:00 sth.txt

mkdir 创建空白文件夹 -p递归创建

1
2
3
4
pos2@instance-20220901-0057:~/trydir$ mkdir -p a/b/c/d
pos2@instance-20220901-0057:~/trydir$ cd a/b/c/d
pos2@instance-20220901-0057:~/trydir/a/b/c/d$ pwd
/home/pos2/trydir/a/b/c/d

cp 复制文件、目录

  1. 目标文件是目录:把源文件复制到该目录
  2. 目标文件是普通文件,询问是否覆盖
  3. 目标文件不存在,正常复制操作
  • -p 保留原始文件的属性
  • -d 若对象是链接文件,保留“链接文件”的属性
  • -r 递归持续复制(用于目录)
  • -i 若目标文件存在,询问是否覆盖
  • -a 相当于-pdr

mv 剪切或重命名

1
2
3
pos2@instance-20220901-0057:~/trydir$ mv sth.txt sb.txt
pos2@instance-20220901-0057:~/trydir$ ls
a  sb.txt

rm 删除文件或目录 -r删目录

1
2
3
4
5
pos2@instance-20220901-0057:~/trydir$ ls
a  sb.txt
pos2@instance-20220901-0057:~/trydir$ rm -rf a
pos2@instance-20220901-0057:~/trydir$ ls
sb.txt

dd 用于按指定大小和个数的数据块来复制文件或转换文件

  • -if 输入文件名
  • -of 输出文件名count
  • -bs 设置每个块大小
  • -count 要复制块的个数
1
2
3
4
5
# /dev/zero 是linux的黑洞
pos2@instance-20220901-0057:~/trydir$ dd if=/dev/zero of=10_file count=1 bs=5M
1+0 records in
1+0 records out
5242880 bytes (5.2 MB, 5.0 MiB) copied, 0.00632999 s, 828 MB/s

file 查看文件类型

1
2
pos2@instance-20220901-0057:~/trydir$ file sb.txt
sb.txt: ASCII text

打包压缩与搜索命令

tar 用于对文件进行打包压缩或解压

  • -c 创建压缩文件
  • -x 解压缩
  • -z 用Gzip压缩&解压缩
  • -j 用bzip2压缩&解压缩
  • -v 显示压缩&解压过程
  • -f 目标文件名
  • -p 保留原始权限和属性
  • -P 使用绝对路径来压缩
  • -C 指定解压目录
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
pos2@instance-20220901-0057:~/sup$ tar -czvf theme.tar ./themes/
./themes/
./themes/hugo-theme-stack/
./themes/hugo-theme-stack/go.mod
./themes/hugo-theme-stack/.gitignore
./themes/hugo-theme-stack/.git
./themes/hugo-theme-stack/i18n/
./themes/hugo-theme-stack/i18n/bn.yaml
./themes/hugo-theme-stack/i18n/ar.yaml
./themes/hugo-theme-stack/i18n/pl.yaml
......
pos2@instance-20220901-0057:~/sup$ ls -l
total 432
drwxrwxr-x  2 pos2 pos2   4096 Sep  1 11:25 archetypes
drwxrwxr-x  6 pos2 pos2   4096 Sep  1 15:12 assets
-rw-rw-r--  1 pos2 pos2    109 Sep  1 11:32 bk_config.toml
-rw-rw-r--  1 pos2 pos2   5049 Sep  2 13:52 config.yaml
drwxrwxr-x  3 pos2 pos2   4096 Sep  1 15:37 content
drwxrwxr-x  2 pos2 pos2   4096 Sep  1 11:25 data
drwxrwxr-x  3 pos2 pos2   4096 Sep  1 21:44 layouts
drwxrwxr-x 14 pos2 pos2   4096 Sep  2 13:52 public
drwxrwxr-x  3 pos2 pos2   4096 Sep  1 11:33 resources
drwxrwxr-x  2 pos2 pos2   4096 Sep  1 11:25 static
-rw-rw-r--  1 pos2 pos2 391473 Sep 17 22:00 theme.tar
drwxrwxr-x  3 pos2 pos2   4096 Sep  1 11:31 themes

pos2@instance-20220901-0057:~/sup$ mkdir theme_dir
pos2@instance-20220901-0057:~/sup$ tar xzvf ./theme.tar -C ./theme_dir/
./themes/
./themes/hugo-theme-stack/
./themes/hugo-theme-stack/go.mod
./themes/hugo-theme-stack/.gitignore
./themes/hugo-theme-stack/.git
./themes/hugo-theme-stack/i18n/
./themes/hugo-theme-stack/i18n/bn.yaml
./themes/hugo-theme-stack/i18n/ar.yaml
./themes/hugo-theme-stack/i18n/pl.yaml
......
pos2@instance-20220901-0057:~/sup$ cd theme_dir/
pos2@instance-20220901-0057:~/sup/theme_dir$ ls
themes

grep 在文本中执行关键词搜索

  • -b 将可执行文件当作text来搜索
  • -c 仅显示找到的行数
  • -i 忽略大小写
  • -n 显示行号
  • -v 仅列出没有关键词的行
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
pos2@instance-20220901-0057:~/sup$ grep /sbin/nologin /etc/passwd
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin

pos2@instance-20220901-0057:~/sup$ grep -v /sbin/nologin /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:4:65534:sync:/bin:/bin/sync
tss:x:106:111:TPM software stack,,,:/var/lib/tpm:/bin/false
pollinate:x:111:1::/var/cache/pollinate:/bin/false
snapd-range-524288-root:x:524288:524288::/nonexistent:/usr/bin/false
snap_daemon:x:584788:584788::/nonexistent:/usr/bin/false

find 查找文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
pos2@instance-20220901-0057:~/sup$ find . -name *.html
./themes/hugo-theme-stack/layouts/page/search.html
./themes/hugo-theme-stack/layouts/index.html
./themes/hugo-theme-stack/layouts/404.html
./themes/hugo-theme-stack/layouts/shortcodes/youtube.html
./themes/hugo-theme-stack/layouts/shortcodes/quote.html
./themes/hugo-theme-stack/layouts/shortcodes/bilibili.html
./themes/hugo-theme-stack/layouts/shortcodes/gitlab.html
......

pos2@instance-20220901-0057:~/sup$ find . -type d
.
./static
./.git
./.git/hooks
./.git/info
./.git/objects
./.git/objects/pack
./.git/objects/12
./.git/objects/info
./.git/refs
......

pos2@instance-20220901-0057:~/mynigga$ find ./content/ -mtime -3
./content/post
./content/post/buya
./content/post/buya/index.md
./content/post/buya/.index.md.swp
./content/post/shell
./content/post/shell/index.md
./content/post/shell/.index.md.swp

......

更多用法细节参见

Licensed under CC BY-NC-SA 4.0
this is the way