Linux命令记录

2021-02-04, updated 2021-09-12

grep

Linux 命令大全 Linux 命令大全

ln

建立软链接命令

lua5.3链接到lua

1
ln -s /usr/bin/lua /usr/bin/lua5.3

man

 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
42
43
44
45
46
47
用于查看linux命令等的用法的命令

1   Executable programs or shell commands

2   System calls (functions provided by the kernel)

3   Library calls (functions within program libraries)

4   Special files (usually found in /dev)

5   File formats and conventions eg /etc/passwd

6   Games

7   Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)

8   System administration commands (usually only for root)

9   Kernel routines [Non standard]

man ls

​    Display the manual page for the item (program) ls.

man -a intro

​    Display, in succession, all of the available intro manual pages contained within the manual.  It is possible to quit between successive displays or skip any of them.

man -t alias | lpr -Pps

​    Format  the manual page referenced by `alias', usually a shell manual page, into the default troff or groff format and pipe it to the printer named ps.  The default output for groff is usually PostScript.  man --help

​    should advise as to which processor is bound to the -t option.

man -l -Tdvi ./foo.1x.gz > ./foo.1x.dvi

​    This command will decompress and format the nroff source manual page ./foo.1x.gz into a device independent (dvi) file.  The redirection is necessary as the -T flag causes output to  be  directed  to  stdout  with  no

​    pager.  The output could be viewed with a program such as xdvi or further processed into PostScript using a program such as dvips.

man -k printf

​    Search the short descriptions and manual page names for the keyword printf as regular expression.  Print out any matches.  Equivalent to apropos -r printf.

man -f smail

​    Lookup the manual pages referenced by smail and print out the short descriptions of any found.  Equivalent to whatis -r smail.

pidof

pidof命令用于查找指定名称的进程的进程号id号。

语法

1
pidof(选项)(参数)

选项

1
2
3
4
-s:仅返回一个进程号;
-c:仅显示具有相同“root”目录的进程;
-x:显示由脚本开启的进程;
-o:指定不显示的进程ID。

参数 进程名称:指定要查找的进程名称。

实例

1
2
3
4
5
6
7
8
pidof nginx
13312 5371

pidof crond
1509

pidof init
1

unzip

unzip命令用于解压缩由zip命令压缩的“.zip”压缩包。

语法 unzip(选项)(参数) 选项

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-c:将解压缩的结果显示到屏幕上,并对字符做适当的转换;
-f:更新现有的文件;
-l:显示压缩文件内所包含的文件;
-p:与-c参数类似,会将解压缩的结果显示到屏幕上,但不会执行任何的转换;
-t:检查压缩文件是否正确;
-u:与-f参数类似,但是除了更新现有的文件外,也会将压缩文件中的其他文件解压缩到目录中;
-v:执行时显示详细的信息;
-z:仅显示压缩文件的备注文字;
-a:对文本文件进行必要的字符转换;
-b:不要对文本文件进行字符转换;
-C:压缩文件中的文件名称区分大小写;
-j:不处理压缩文件中原有的目录路径;
-L:将压缩文件中的全部文件名改为小写;
-M:将输出结果送到more程序处理;
-n:解压缩时不要覆盖原有的文件;
-o:不必先询问用户,unzip执行后覆盖原有的文件;
-P<密码>:使用zip的密码选项;
-q:执行时不显示任何信息;
-s:将文件名中的空白字符转换为底线字符;
-V:保留VMS的文件版本信息;
-X:解压缩时同时回存文件原来的UID/GID;
-d<目录>:指定文件解压缩后所要存储的目录;
-x<文件>:指定不要处理.zip压缩文件中的哪些文件;
-Z:unzip-Z等于执行zipinfo指令。

参数 压缩包:指定要解压的“.zip”压缩包。

实例 将压缩文件text.zip在当前目录下解压缩。

unzip test.zip 将压缩文件text.zip在指定目录/tmp下解压缩,如果已有相同的文件存在,要求unzip命令不覆盖原先的文件。

unzip -n test.zip -d /tmp 查看压缩文件目录,但不解压。

unzip -v test.zip 将压缩文件test.zip在指定目录/tmp下解压缩,如果已有相同的文件存在,要求unzip命令覆盖原先的文件。

unzip -o test.zip -d tmp/

zip

1
zip -rP abc#123 test.zip test.txt
 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
-r 递归,将指定目录下的所有文件和子目录一并处理
-S 包含系统和隐藏文件
-y 直接保存符号连接,而非该连接所指向的文件
-X 不保存额外的文件属性
-m 将文件压缩并加入压缩文件后,删除源文件
-<压缩级别> 1~9,数字越大,压缩率越高
-F 尝试修复已损坏的压缩文件
-T 检查备份文件内的每个文件是否正确无误
-q 不显示指令执行过程
-g 将文件压缩后附加在既有的压缩文件之后,而非另行建立新的压缩文件
-u 更新压缩包内文件
-f 更新压缩包内文件。如果符合条件的文件没有包含在压缩包中,则压缩后添加
-$ 保存第一个被压缩文件所在磁盘的卷标
-j 只保存文件名称及其内容
-D 压缩文件内不建立目录名称
-i <表达式> 压缩目录时,只压缩符合条件的文件
-x <表达式> 排除符合条件的文件
-n <文件名后缀> 排除指定文件名后缀的文件
-b <缓存路径> 指定临时文件目录
-d <表达式> 从压缩文件内删除指定的文件
-t <日期时间> 把压缩文件的日期设成指定的日期
-o 以压缩文件内拥有最新更改时间的文件为准,将压缩文件的更改时间设成和该文件相同
-A 调整可执行的自动解压缩文件
-c 替每个被压缩的文件加上注释
-z 替压缩文件加上注释
-k 使用MS-DOS兼容格式的文件名称。
-l 压缩文件时,把LF字符置换成LF+CR字符。
-ll 压缩文件时,把LF+CR字符置换成LF字符。

inittab

1
sed -i 's/^::respawn:\/usr\/bin\/netmanager/::once:\/usr\/bin\/netmanager/' $(DIR_ROMFS)/etc/inittab

uname

uname –help Usage: uname [OPTION]… Print certain system information.  With no OPTION, same as -s.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
  -a, --all                print all information, in the following order,
                             except omit -p and -i if unknown:
  -s, --kernel-name        print the kernel name
  -n, --nodename           print the network node hostname
  -r, --kernel-release     print the kernel release
  -v, --kernel-version     print the kernel version
  -m, --machine            print the machine hardware name
  -p, --processor          print the processor type or "unknown"
  -i, --hardware-platform  print the hardware platform or "unknown"
  -o, --operating-system   print the operating system
      --help     display this help and exit
      --version  output version information and exit
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
$ uname -p
x86_64
$ uname -m
x86_64
$ uname -i
x86_64
$ uname -o
GNU/Linux
$ uname -v
#140~14.04.1-Ubuntu SMP Fri Feb 16 09:25:20 UTC 2018
$ uname -r
4.4.0-116-generic
$ uname -n
ubuntu-PowerEdge-R740
$ uname -s
Linux

dropbear

  1. 介绍 小型的嵌入式系统专用的SSH服务端和客户端工具 服务器端:dropbear、dropbearkey 客户端:dbclient dropbear默认使用nsswitch实现名称解析(账户名称) dropbear会在用户登录时检查其默认shell是否为安全shell,/etc/shells中的shell一般称为安全shell
  2. 安装编译 下载链接:https://matt.ucc.asn.au/dropbear/ 下载完成并解压后执行如下命令进行编译安装 dropbear默认编译方式:
1
2
3
./configure

make && make install
  1. 主机密钥配置 默认情况下,dropbear的配置文件存放在/etc/dropbear目录下(如果没有,则需要手动创建) 为dropbear生成配置文件:
1
2
3
4
5
dropbearkey -t TYPE -f dropbear_TYPE_host_key -s SIZE
        
-t TYPE: 秘钥配置文件的类型,一般有rsa,dss,ecdsa等
-f dropbear_TYPE_host_key: 指定该TYPE加密类型的配置文件的存放路劲
-s SIZE:指定加密的位数,默认情况下rsa为1024,最多4096 只要是8的倍数即可,ecdsa默认为256,长度限制为112-571
  1. 般情况下只需要为常用的加密类型生成其配置文件即可
  2. ssh登录主机
  1. dbclient登录其它主机
1
dbclient [email protected]

包管理工具

pacman

pacman 用法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
pacman -Sy abc                  #和源同步后安装名为abc的包
pacman -S   abc                  #从本地数据库中得到abc的信息,下载安装abc包
pacman -Sf abc                  #强制安装包abc
pacman -Ss abc                  #搜索有关abc信息的包
pacman -Si abc                  #从数据库中搜索包abc的信息
pacman -Q                         # 列出已经安装的软件包
pacman -Q abc                  # 检查 abc 软件包是否已经安装
pacman -Qi abc                  #列出已安装的包abc的详细信息
pacman -Ql abc                 # 列出abc软件包的所有文件
pacman -Qo /path/to/abc # 列出abc文件所属的软件包
pacman -Syu                      #同步源,并更新系统
pacman -Sy                        #仅同步源
pacman -Su                        #更新系统
pacman -R   abc                  #删除abc包
pacman -Rd abc               #强制删除被依赖的包
pacman -Rc abc                  #删除abc包和依赖abc的包
pacman -Rsc abc                #删除abc包和abc依赖的包
pacman -Sc                       #清理/var/cache/pacman/pkg目录下的旧包
pacman -Scc                      #清除所有下载的包和数据库
pacman -U   abc                  #安装下载的abs包,或新编译的abc包
pacman -Sd abc                  #忽略依赖性问题,安装包abc
pacman -Su --ignore foo      #升级时不升级包foo
pacman -Sg abc                  #查询abc这个包组包含的软件包

grub命令行启动windows

对于Windows和Linux双系统的用户,你可能会陷入这样一种窘境:在你的Grub2引导中,没有Windows的选项,这时,一般的选择是先启动Linux系统,修改Grub2的设置后再重启。 实际上,考虑到用户的奇思妙想与鬼畜操作,Grub还提供了一个命令行终端,不需要启动Linux就可以直接引导电脑上的其他系统(Grub理论上也并非是和Linux绑定,所以其自成一体也在情理之中)。

在开机后的Grub引导页面,按下ESC,即进入Grub的命令行模式。下面,我们依次介绍几条我们会用到的命令,同时也是这个终端的几条核心命令。

有了上述的四条命令,我们就可以实现用grub命令引导启动windows了。基本的操作步骤:

  1. 在引导列表页按ESC进入Grub2终端 。
  2. insmod part_gpt 导入模块,让grub可以看到gpt格式的硬盘(这个有没有必要我也不清楚,但执行一下也不妨事。)。
  3. 借助 ls 命令找到windows系统的 bootmgfw.efi 所在的分区(hd_, _)。
  4. 修改当前目录 set root=(hd*, *)
  5. chainloader /efi/Microsoft/Boot/bootmgfw.efi (可以用 ls 再确认一下这个路径)。
  6. boot 系统启动

Tips: ls命令打印出来的分区名称,可能是”(hd_, gpt_)”,在我们键入分区时,这个gpt可以省略。

按照上述步骤操作,如果你的windows系统本身没什么问题的话,应该是可以正常启动的。

P.S. 笔者接触Grub2的命令行模式其实源于一个非常低能的错误——在更新了BIOS后,忘记把装双系统用的系统盘从USB口上拔下来,导致系统自动U盘启动,连个Windows10的引导项都没得,只有ubuntu 的安装选项(没错,在Linux还没有正式安装时,Grub2就已经可以用了),以至于鼓捣了半个多钟头,后来才想到,如果当时把U盘拔下来,就没有这些破事儿了。

start-stop-daemon

功能

启动和停止系统守护程序

参数

 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
Commands:
-S|--start -- <argument> ... 开启一个系统守护程序,并传递参数给它
-K|--stop 停止一个程序
-T|--status 得到程序的状态
-H|--help 显示帮助信息
-V|--version 打印版本信息
Matching options (at least one is required):
-p|--pidfile <pid-file> pid file to check
-x|--exec <executable> program to start/check if it isrunning
-n|--name <process-name> process name to check
-u|--user <username|uid> process owner to check
Options:
-g|--group <group|gid> 按指定用户组权限运行程序
-c|--chuid <name|uid[:group|gid]>
按指定用户、用户组权限运行程序
-s|--signal <signal> signal to send (default TERM)
-a|--startas <pathname> program to start (default is<executable>)
-r|--chroot <directory> chroot to <directory>before starting
-d|--chdir <directory> change to <directory>(default is /)
-N|--nicelevel <incr> add incr to the process' nicelevel
-P|--procsched <policy[:prio]>
use <policy> with <prio> for the kernel
process scheduler (default prio is 0)
-I|--iosched <class[:prio]> use <class> with<prio> to set the IO
scheduler (default prio is 4)
-k|--umask <mask> 在开始运行前设置<mask>
-b|--background 后台运行
-m|--make-pidfile 当命令本身不创建pidfile时,由start-stop-daemon创建
-R|--retry <schedule> 等待timeout的时间,检查进程是否停止,如果没有发送KILL信号;
-t|--test 测试模式
-o|--oknodo exit status 0 (not 1) if nothing done
-q|--quiet 不要输出警告
-v|--verbose 显示运行过程信息
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
1. -S, --start [--] arguments
              Check for the existence of a specified process.  If such a
              process exists, start-stop-daemon does nothing, and exits with
              error status 1 (0 if --oknodo is specified).  If such a
              process does not exist, it starts an instance, using either
              the executable specified by --exec or, if specified, by
              --startas.  Any arguments given after -- on the command line
              are passed unmodified to the program being started.
    2. 如上所述,在--之后加入命令行参数:
        start-stop-daemon -S -b -x /usr/sbin/httpd -- -h /var/www

应用

  1. 开启一个daemon进程
1
start-stop-daemon --start --background --exec /root/proxy.py
  1. 关闭一个daemon进程
1
start-stop-daemon --stop --name proxy.py

参考启动文件

 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
cat /etc/init.d/S71httpd
        #! /bin/sh
        
        set -e
        
        DESC="httpd"
        NAME=httpd
        DAEMON=/usr/sbin/$NAME
        
        case "$1" in
          start)
                printf "Starting $DESC: "
                start-stop-daemon -S -b -x $NAME -- -h /var/www
                echo "OK"
                ;;
          stop)
                printf "Stopping $DESC: "
                start-stop-daemon -K -x $NAME
                echo "OK"
                ;;
          restart|force-reload)
                echo "Restarting $DESC: "
                $0 stop
                sleep 1
                $0 start
                echo ""
                ;;
          *)
                echo "Usage: $0 {start|stop|restart|force-reload}" >&2
                exit 1
                ;;
        esac
        
        exit 0

signal

SIGINT、SIGTERM和SIGKILL区别

1. SIGINT与SIGTERM区别

  1. SIGINT关联ctrl+c
  2. SIGINT只能结束前台进程
  3. 通过ctrl+c对当前进程发送结束信号,信号被进程树接收到(即:不仅当前进程,子进程也会收到结束信号)

2. SIGTERM与SIGKILL

  1. SIGTERM可以被阻塞、处理和忽略;因此有的进程不能按预期的结束
  2. kill不使用参数:发送SIGTERM信号,只有当前进程收到信号,若当前进程被kill,则子进程的父进程就会更改为init,即pid为1
  3. kill命令的默认不带参数发生的信号就是SIGTERM,让程序友好的退出 ,当程序未退出时,可以使用kill -9强制退出

tail

tail -n 50 wx.log

示例:查看/var/log/boot.log,只显示最后一行。则执行

tail -n 1  /var/log/boot.log

tail -n 1000:显示最后1000行

tail -n +1000:从1000行开始显示,显示1000行以后的

head -n 1000:显示前面1000行

watch

watch可以帮你监测一个命令的运行结果,来监测你想要的一切命令的结果变化

参数:

1
2
3
4
5
6
7
Usage: watch [-dhntv] [--differences[=cumulative]] [--help] [--interval=<n>] [--no-title] [--version] <command>
  -d, --differences[=cumulative]        highlight changes between updates
                (cumulative means highlighting is cumulative)
  -h, --help                            print a summary of the options
  -n, --interval=<seconds>              seconds to wait between updates
  -v, --version                         print the version number
  -t, --no-title                        turns off showing the header

示例

1
2
3
4
5
6
7
watch -n 1 -d netstat -ant 【-n 设置间隔,-d,difference,高亮显示不同】
 
   watch -d 'ls /home/omd'       【-d 高亮显示】
 
   watch -t 'ls /home/omd'       【-t会关闭watch命令在顶部的时间间隔】
 
说明: 切换终端: Ctrl+x     退出watch:Ctrl+g
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
watch -n 1 -d 'pstree|grep http'
 
实时查看模拟攻击客户机建立起来的连接数
 
watch -n 1 -d 'netstat -an | grep "21" | egrep "192.168.25.100"| wc -l'
 
监测当前目录中 scf' 的文件的变化
 
watch -d 'ls -l|grep scf'
 
10秒一次输出系统的平均负载
 
watch -n 1 -d "uptime"

dd

Linux dd命令用于读取、转换并输出数据。 dd可从标准输入或文件中读取数据,根据指定的格式来转换数据,再输出到文件、设备或标准输出。 参数说明: • if=文件名:输入文件名,默认为标准输入。即指定源文件。 • of=文件名:输出文件名,默认为标准输出。即指定目的文件。 • ibs=bytes:一次读入bytes个字节,即指定一个块大小为bytes个字节。 obs=bytes:一次输出bytes个字节,即指定一个块大小为bytes个字节。 bs=bytes:同时设置读入/输出的块大小为bytes个字节。 • cbs=bytes:一次转换bytes个字节,即指定转换缓冲区大小。 • skip=blocks:从输入文件开头跳过blocks个块后再开始复制。 • seek=blocks:从输出文件开头跳过blocks个块后再开始复制。 • count=blocks:仅拷贝blocks个块,块大小等于ibs指定的字节数。 • conv=<关键字>,关键字可以有以下11种: • conversion:用指定的参数转换文件。 • ascii:转换ebcdic为ascii • ebcdic:转换ascii为ebcdic • ibm:转换ascii为alternate ebcdic • block:把每一行转换为长度为cbs,不足部分用空格填充 • unblock:使每一行的长度都为cbs,不足部分用空格填充 • lcase:把大写字符转换为小写字符 • ucase:把小写字符转换为大写字符 • swab:交换输入的每对字节 • noerror:出错时不停止 • notrunc:不截短输出文件 • sync:将每个输入块填充到ibs个字节,不足部分用空(NUL)字符补齐。 • –help:显示帮助信息 • –version:显示版本信息 实例 在Linux 下制作启动盘,可使用如下命令:

1
dd if=boot.img of=/dev/fd0 bs=1440k

将testfile文件中的所有英文字母转换为大写,然后转成为testfile_1文件,在命令提示符中使用如下命令:

1
dd if=testfile_2 of=testfile_1 conv=ucase

其中testfile_2 的内容为:

1
2
3
4
5
$ cat testfile_2 #testfile_2的内容  
HELLO LINUX!  
Linux is a free unix-type opterating system.  
This is a linux testfile!  
Linux test

转换完成后,testfile_1 的内容如下:

1
2
3
4
5
6
7
8
$ dd if=testfile_2 of=testfile_1 conv=ucase #使用dd 命令,大小写转换记录了0+1 的读入  
记录了0+1 的写出  
95字节(95 B)已复制,0.000131446 秒,723 KB/s  
[email protected]:~$ cat testfile_1 #查看转换后的testfile_1文件内容  
HELLO LINUX!  
LINUX IS A FREE UNIX-TYPE OPTERATING SYSTEM.  
THIS IS A LINUX TESTFILE!  
LINUX TEST #testfile_2中的所有字符都变成了大写字母

由标准输入设备读入字符串,并将字符串转换成大写后,再输出到标准输出设备,使用的命令为:

1
dd conv=ucase

输入以上命令后按回车键,输入字符串,再按回车键,按组合键Ctrl+D 退出,出现以下结果:

1
2
3
4
5
6
$ dd conv=ucase 
Hello Linux! #输入字符串后按回车键  
HELLO LINUX! #按组合键Ctrl+D退出,转换成大写结果  
记录了0+1 的读入  
记录了0+1 的写出  
13字节(13 B)已复制,12.1558 秒,0.0 KB/s

adb

问题

ubuntu14.04 adb安装及解决adb devices找不到设备的问题

安装

1
sudo apt-get install android-tools-adb

查看

命令:lsusb

1
2
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 011: ID 2207:0006

2207:0006这行内容就是设备的。

加入规则

1
sudo gedit /etc/udev/rules.d/51-android.rules

加入内容:

1
2
SUBSYSTEM=="usb", ATTRS{idVendor}=="2207", ATTRS{idProduct}=="0006",MODE="0666"
#注释:lsusb中内容2207:0006, idVendor对应2207,idProduct对应。

保存退出

验证

udev重启:

1
sudo service udev restart

查看是否有设备:

1
adb devices

如果没有设备,运行:

1
2
3
adb kill-server
adb start-server
adb devices

如果还是没有设备,尝试:

1
gedit ~/.android/adb_usb.ini

lsusb中内容2207,加入其中:

1
0x2207

然后就可以看到设备了。

adb shell 进入设备

env

显示系统中已存在的环境变量

语法

1
env(选项)(参数)

选项

1
2
-i:开始一个新的空的环境;
-u<变量名>:从当前环境中删除指定的变量。

参数

1
2
# env
SHELL=/bin/bash  # 终端类型

chsh

Linux chsh命令用于更改使用者 shell 设定。

1
2
3
# chsh -s /bin/csh  # 改变当前设置为 /bin/csh
Changing shell for root.
Shell not changed.

文件管理

cp

1
--preserve或-p:除复制文件的内容外,还把修改时间和访问权限也复制到新文件中。

find

1
2
-maxdepth :指定遍历搜索的最大深度。最大目录层级
-mindepth: 指定开始遍历搜索的最小深度。最小目录层级

问题

查看是csh还是bash和更改shell设置

  1. 判断终端是csh还是bash: 终端命令:env
1
SHELL=/bin/zsh
words: 7280 tags: linux command