学习linux命令,看这篇2W多字的linux命令详解

6,483 阅读20分钟

用心分享,共同成长

没有什么比每天进步一点点更重要了

本文已收录到我的github:github.com/midou-tech/… ,欢迎star和issues。

序言

 本篇文章主要讲解了一些linux常用命令,主要讲解模式是,命令介绍、命令参数格式、命令参数、命令常用参数示例。由于linux命令较多,我还特意选了一些日常使用较为频繁的命令进行讲解,但还是免不了文章很长,建议大家收藏起来,用到的时候不会了再来阅读。当然学习linux命令最好的方法是学会使用linux自带的man手册,所有linux命令规范和使用细则都会在该手册中讲解的很清楚,我在书写的过程中也是参考该手册和日常使用情况。

如果还有哪些是大家希望再补充的命令,可以留言给我,我会持续更新该文章,同时也作为我学习和工作的手册。

Linux初级指令

ls ——List

ls 介绍

 这是我学Linux的第一个命令,相信也是很多人学习Linux的第一个命令。ls全称list.

List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

列出有关文件的信息(默认为当前目录)。如果没有指定-cftuvSUX或——Sort,则按字母顺序排序。

 官方的说的很清楚,默认列出当前目录,所以可以列出其他目录或者路径下的文件信息或者目录信息。

eg:

$ls /etc/hosts
/etc/hosts

 ls还可以列出指定目录下的文件列表。

image-20191221161638964
image-20191221161638964

ls 参数格式

ls [OPTION]... [FILE]...

ls命令参数

-a 列出指定目录下的所有文件,包括隐藏文件

-c 使用最后一次更改文件状态以进行排序(-t)或长时间打印(-l)的时间

-h 与-l选项一起使用时,请使用单位后缀:Byte、Kilobyte、mete、gb、tb和Petabyte,以便使用以2为基数的大小将数字减少到3或更少

-l 长格式列表。(见下文)。如果输出到终端,则所有文件大小的总和将输出到长清单前面的一行中

-n 以数字形式显示用户和组id,而不是在长(-l)输出中转换为用户或组名。这个选项默认打开-l选项

-o 以长格式列出,但省略组id

-s 显示每个文件实际使用的文件系统块的数量,以512字节为单位,其中部分单元四舍五入为下一个整数值

-t 在按照字典顺序对操作数排序之前,先按修改的时间排序(最近修改的是first)

-u 使用最后一次访问的时间,而不是最后一次修改文件进行排序

ls 用法示例:

$ ls
test  tmp
$ ls -a
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  test  tmp
#可以看到通过ls -a 可以查看当前目录影藏的文件,Linux下(.)开头的文件是隐藏文件。
$ ls -l
total 4
-rw-r--r-- 1 test hero    0 Dec 21 19:54 test
drwxr-xr-x 2 test hero 4096 Dec 21 19:54 tmp
$ ls -lh
total 4.0K
-rw-r--r-- 1 test hero    0 Dec 21 19:54 test
drwxr-xr-x 2 test hero 4.0K Dec 21 19:54 tmp
$ ls -ll
total 4
-rw-r--r-- 1 test hero    0 Dec 21 19:54 test
drwxr-xr-x 2 test hero 4096 Dec 21 19:54 tmp
$ ls -alh
total 28K
drwx------   3 test hero 4.0K Dec 21 19:54 .
drwxr-xr-x. 19 root  root 4.0K Aug  1 10:41 ..
-rw-------   1 test hero  226 Dec 21 19:54 .bash_history
-rw-r--r--   1 test hero   18 Aug  3  2016 .bash_logout
-rw-r--r--   1 test hero  193 Aug  3  2016 .bash_profile
-rw-r--r--   1 test hero  231 Aug  3  2016 .bashrc
-rw-r--r--   1 test hero    0 Dec 21 19:54 test
drwxr-xr-x   2 test hero 4.0K Dec 21 19:54 tmp
$ ls -o
total 4
-rw-r--r-- 1 test    0 Dec 21 19:54 test
drwxr-xr-x 2 test 4096 Dec 21 19:54 tmp
$ ls -oh
total 4.0K
-rw-r--r-- 1 test    0 Dec 21 19:54 test
drwxr-xr-x 2 test 4.0K Dec 21 19:54 tmp

pwd —— Print Working Directory

pwd介绍

 打印当前工作目录的完整路径名。(print name of current/working directory)

参数格式

pwd [OPTION]...

pwd 用法展示

[test@Mfate171193 /home/test] 20:06
pwd
/home/test

touch (change file timestamps)

touch介绍

Update the access and modification times of each FILE to the current time.

A FILE argument that does not exist is created empty, unless -c or -h is supplied.

 将每个文件的访问和修改时间更新为当前时间。除非提供-c或-h,否则将不存在的FILE参数创建为空。

touch参数格式

touch [OPTION]... FILE...

touch命令参数

-a 或--time=atime或--time=access或--time=use 只更改存取时间。

-c 或--no-create 不建立任何文档。

-d 使用指定的日期时间,而非现在的时间。

-f 此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性问题。

-m 或--time=mtime或--time=modify 只更改变动时间。

-r 把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。

-t 使用指定的日期时间,而非现在的时间。

用法示例

#创建三个文件
$ touch test1 test2 test3
#不创建文档
$ touch -c test5  
$ ls
test1  test2  test3
#可以看到只创建了test1、test2、test3 , -c不建立任何文件
$ touch -t 201911110000 test1
stat test*
#stat命令可以查看文件的详细变更时间,可以test1文件的最后修改时间为201911110000,说明touch -t可以修改文件最后访问时间。这个参数还是很有用的,你可以把你最近访问的时间修改为一个很早的时间。可以做一些有趣的事情,哈哈。
  File: ‘test1’
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 360736      Links: 1
Access: (0644/-rw-r--r--)  Uid: (14060/   localhost)   Gid: ( 1001/    hero)
Access: 2019-11-11 00:00:00.000000000 +0800
Modify: 2019-11-11 00:00:00.000000000 +0800
Change: 2019-12-21 20:58:11.290761038 +0800
 Birth: -
  File: ‘test2’
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 360738      Links: 1
Access: (0644/-rw-r--r--)  Uid: (14060/   localhost)   Gid: ( 1001/    hero)
Access: 2019-12-21 20:56:34.523761038 +0800
Modify: 2019-12-21 20:56:34.523761038 +0800
Change: 2019-12-21 20:56:34.523761038 +0800
 Birth: -
  File: ‘test3’
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 360740      Links: 1
Access: (0644/-rw-r--r--)  Uid: (14060/   localhost)   Gid: ( 1001/    hero)
Access: 2019-12-21 20:56:34.523761038 +0800
Modify: 2019-12-21 20:56:34.523761038 +0800
Change: 2019-12-21 20:56:34.523761038 +0800
 Birth: -

cat&tac (Concatenate FILE(s), or standard input, to standard output.)

cat介绍

Concatenate FILE(s), or standard input, to standard output.

将FILE或标准输入连接到标准输出。

cat 参数格式

cat [OPTION]... [FILE]...

cat命令参数

-A, --show-all 等价于 -vET

-b, --number-nonblank 对非空输出行编号

-e 等价于 -vE

-E, --show-ends 在每行结束处显示

-n, --number 对输出的所有行编号,由1开始对所有输出的行数编号

-s, --squeeze-blank 有连续两行以上的空白行,就代换为一行的空白行

-t 与 -vT 等价

-T, --show-tabs 将跳格字符显示为 ^I

-u (被忽略)

-v, --show-nonprinting 使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外

cat常用参数示例

$ cat test  #展示文件内容
-A, --show-all      等价于 -vET
-b, --number-nonblank  对非空输出行编号
-e            等价于 -vE

$ cat -n test  #展示文件内容并且展示行号
     1    -A, --show-all      等价于 -vET
     2    -b, --number-nonblank  对非空输出行编号
     3    -e            等价于 -vE

tac命令与cat命令展示内容相反,不能带行号输出。

$ tac test
-e            等价于 -vE
-b, --number-nonblank  对非空输出行编号
-A, --show-all      等价于 -vET

mkdir —— Make Directory

mkdir介绍

Create the DIRECTORY(ies), if they do not already exist.

如果目录不存在,则创建目录。

mkdir参数格式

mkdir [OPTION]... DIRECTORY...

mkdir命令参数

-m, --mode=模式,设定权限<模式> (类似 chmod),而不是 rwxrwxrwx 减 umask

-p, --parents 可以是一个路径名称。此时若路径中的某些目录尚不存在,加上此选项后,系统将自动建立好那些尚不存在的目录,即一次可以建立多个目录;

-v, --verbose 每次创建新目录都显示信息

--help 显示此帮助信息并退出

--version 输出版本信息并退出

mkdir常用参数示例

#创建目录文件test
$ mkdir test
#连续创建
$ mkdir -p test1/tmp
$ ls
test  test1
#创建时置顶目录权限
#tmp目录拥有可执行权限,Linux文件权限问题后期文章会详细讲到,记得关注我
$ mkdir -pm 777 test2/tmp
$ ls -lh
total 12K
drwxr-xr-x 2 localhost hero 4.0K Dec 21 21:39 test
drwxr-xr-x 3 localhost hero 4.0K Dec 21 21:40 test1
drwxr-xr-x 3 localhost hero 4.0K Dec 21 21:40 test2
$ ls
test  test1  test2
#-v 参数可确定文件是否已经存在,如果不存在则会创建,并显示如下信息
$ mkdir -v test
mkdir: cannot create directory ‘test’: File exists

$ mkdir -v test7
mkdir: created directory ‘test7’

cd —— Change Directory

cd介绍

 切换当前目录至指定目录

常用参数示例

#打印当前目录到标准输出
pwd
/Users/localhost
#切换到目录/
$cd /

 cd命令没什么参数就是切换目录到指定路径下,较为简单,但是使用评率极高。

rm&rmdir —— Remove Directory

rm介绍

The rm utility attempts to remove the non-directory type files specified on the command line. If the permissions of the file do not permit writing, and the standard input device is a terminal, the user is prompted (on the standard error output) for confirmation.

rm实用程序尝试删除命令行上指定的非目录类型文件。 如果文件的权限不允许写入,并且标准输入设备是终端,则会提示用户(在标准错误输出上)进行确认。

 rm命令使用时还是需要注意的,他的删除恢复比较麻烦,有些系统会自带-i参数,输入命令之后还有一个确认步骤,有些是直接删掉了,是真删掉,从内存抹掉那种(其实底层是让该文件指针不指向该文件的内存块,内存上的内容原则上是存在的,但是恢复会比较复杂,需要扫描整块内存块才能拿到内容)。不要轻易删掉你写的重要代码,hh。

rm参数格式

rm [-dfiPRrvW] file ...

rm命令参数

-f, --force 忽略不存在的文件,从不给出提示。

-i, --interactive 进行交互式删除

-r, -R, --recursive 指示rm将参数中列出的全部目录和子目录均递归地删除。

-d, --dir 删除空目录

rm常用参数示例

# 创建三个文件
$ touch tmp.cc tmp.java tmp.py tmp.go
#创建目录文件
$ mkdir -p linux/test
#查看文件是否创建成功
$ ls
linux    tmp.cc   tmp.go   tmp.java tmp.py
#删除文件,并进行提示
$ rm -i tmp.cc
remove tmp.cc? y
#强制删除
$ rm  -f tmp.go
#删除目录
$ rm -f linux  #删除目录失败
rm: linux: is a directory
#循环删除目录下所有文件
$ rm -rf linux  #删除目录成功,
$ ls
tmp.java tmp.py  

 rmdir==rm -d 删除空目录

mv —— Move

mv介绍

In its first form, the mv utility renames the file named by the source operand to the destination path named by the target operand. This form is assumed when the last operand does not name an already existing directory.

In its second form, mv moves each file named by a source operand to a destination file in the existing directory named by the directory operand. The destination path for each operand is the pathname produced by the concatenation of the last operand, a slash, and the final pathname component of the named file.

总结下,就是移动目录或者文件到置顶目录下,同时具有重命名的功能。

mv参数格式

mv [-f | -i | -n] [-v] source target mv [-f | -i | -n] [-v] source ... directory

mv命令参数

-b :若需覆盖文件,则覆盖前先行备份。

-f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;

-i :若目标文件 (destination) 已经存在时,就会询问是否覆盖

-n:不要覆盖现有文件。 (-n选项将覆盖以前的任何-f或-i选项。)

-u :若目标文件已经存在,且 source 比较新,才会更新(update)

mv常用参数示例

##修改文件名
$ touch tmp.cc

$ ls
tmp.cc

$ mv tmp.cc tmp.java

$ ls
tmp.java
#移动文件或者目录
pwd
/Users/localhost/test
#移动文件并重命名
$ mv /Users/localhost/logs/tmp.txt ./tmp.log 

$ ls /Users/localhost/logs/
discover-client metabase        tesla

$ ls ./
tmp.java tmp.log
#移动目录并重命名
$ mv /Users/localhost/logs/tesla  ./tesla.ba 

$ ls
tesla.ba tmp.java tmp.log

 这个命令在写makefile文件的时候用起来很舒服,可以把编译的结果移到指定目录并重命名。

cp —— Copy

cp介绍

In the first synopsis form, the cp utility copies the contents of the source_file to the target_file. In the second synopsis form, the contents of each named source_file is copied to the destination target_directory. The names of the files themselves are not changed. If cp detects an attempt to copy a file to itself, the copy will fail.

cp实用程序将source_file的内容复制到target_file。 在第二个大纲格式中,每个命名的source_file的内容都复制到目标target_directory。 文件本身的名称不会更改。 如果cp检测到尝试将文件复制到自身的尝试,则复制将失败。

cp参数格式

cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory

命令参数

-a, --archive 等于-dR --preserve=all

--backup[=CONTROL 为每个已存在的目标文件创建备份

-b 类似--backup 但不接受参数

--copy-contents 在递归处理是复制特殊文件内容

-d 等于--no-dereference --preserve=links

-f, --force 如果目标文件无法打开则将其移除并重试(当 -n 选项

​ 存在时则不需再选此项)

-i, --interactive 覆盖前询问(使前面的 -n 选项失效)

-H 跟随源文件中的命令行符号链接

-l, --link 链接文件而不复制

-L, --dereference 总是跟随符号链接

-n, --no-clobber 不要覆盖已存在的文件(使前面的 -i 选项失效)

-P, --no-dereference 不跟随源文件中的符号链接

-p 等于--preserve=模式,所有权,时间戳

--preserve[=属性列表 保持指定的属性(默认:模式,所有权,时间戳),如果

​ 可能保持附加属性:环境、链接、xattr 等

-R, -r, --recursive 复制目录及目录内的所有项目

常用参数示例

$ cat tmp.cc
change world

#拷贝文件内容
$ cp tmp.cc tmp.java

$ cat tmp.java
change world

echo

echo介绍

The echo utility writes any specified operands, separated by single blank (') characters and followed by a newline (\n') character, to the standard output.

echo实用程序将任何指定的操作数写入标准输出,这些操作数由单个空格()字符分隔,后跟换行符(\ n')字符。

这条命令较为简单,常用来打印变量、文本内容到,例如:

echo "change world"
change world

#s输出PWD环境变量的值
echo $PWD
/Users/localhost/test

$PWD 是取当前路径,然后echo到标准输出,一般echo \$name 用来查看某个环境变量的值

head&tail

head介绍

This filter displays the first count lines or bytes of each of the specified files, or of the standard input if no files are specified. If count is omitted it defaults to 10.

此过滤器显示每个指定文件或标准输入(如果未指定文件)的前几行或字节。

If more than a single file is specified, each file is preceded by a header consisting of the string ==> XXX <=='' whereXXX'' is the name of the file.

如果省略count,则默认为10.如果指定了多个文件,则每个文件的头均由字符串==> XXX <==''组成,其中XXX''为文件名 文件。

head参数格式

head [-n count | -c bytes] [file ...]

head常用参数示例

-n 展示前n行

-c 展示前n个字符

head常用参数示例

$ cat -n test.txt #这就用上前面的cat命令的-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    在
    25    了
    26    .

$ head test.txt  #默认展示10行



享,



长.



$ head -n15 test.txt #展示15行



享,



长.








$ head -c23 test.txt #展示前23个字符,中文一个汉字并非一个字符的(utf-8编码中文字符长度是可变的)



享,

tail命令完全和他相反,是从尾部开始展示文本,但是他的参数用法更多。

-f 循环读取

-q 不显示处理信息

-v 显示详细的处理信息

-c<数目> 显示的字节数

-n<行数> 显示行数

--pid=PID 与-f合用,表示在进程ID,PID死掉之后结束.

-q, --quiet, --silent 从不输出给出文件名的首部

-s, --sleep-interval=S 与-f合用,表示在每次反复的间隔休眠S秒

tail和head命令经常用来查看日志,像我现在基本每天都会用,我的日志文件没办法用vim或者cat这样去看(因为我负责的业务日志量每天都是几十个G),要么用tail,要么用more&less(下面会讲)。

more&less

more介绍

Less is a program similar to more (1), but which allows backward movement in the file as well as forward movement. Also, less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi (1). Less uses termcap (or terminfo on some systems), so it can run on a variety of terminals. There is even limited support for hardcopy terminals. (On a hardcopy terminal, lines which should be printed at the top of the screen are prefixed with a caret.)

more每次打开文件不是全部把文件读入内存而是流式读取,不会因为vi|vim某个大文件而造成系统oom。

more&less最重要的一点就是流式读取,支持翻页,像cat命令是全部读取输出到标准输出,如果文件太大会把屏幕刷满的,根本没办法看。

more参数格式

more [-dlfpcsu ] [-num ] [+/ pattern] [+ linenum] [file ... ]

more命令参数

+n 从笫n行开始显示

-n 定义屏幕大小为n行

+/pattern 在每个档案显示前搜寻该字串(pattern),然后从该字串前两行之后开始显示

-c 从顶部清屏,然后显示

-d 提示“Press space to continue,’q’ to quit(按空格键继续,按q键退出)”,禁用响铃功能

-l 忽略Ctrl+l(换页)字符

-p 通过清除窗口而不是滚屏来对文件进行换页,与-c选项相似

-s 把连续的多个空行显示为一行

-u 把文件内容中的下画线去掉

less 与 more 类似,但使用 less 可以随意浏览文件,而 more 仅能向前移动,却不能向后移动,而且 less 在查看之前不会加载整个文件

wc

wc介绍

The wc utility displays the number of lines, words, and bytes contained in each input file, or standard input (if no file is specified) to the standard output. A line is defined as a string of characters delimited by a charac-ter. Characters beyond the final character will not be included in the line count.

wc实用程序显示每个输入文件或标准输入(如果未指定文件)中每个输入文件中包含的行数,字数和字节数。 一行定义为由字符分隔的字符串。 最后一个字符之后的字符将不包括在行数中。

这条命令对我来说还是比较深刻的,我刚学习编程不久的时候,我感觉我写的代码很多了,那个时候我就很想知道我写了多少行代码了,一时兴起,说干就干,直接写了个程序去统计了一把,写完之后还感觉自己蛮厉害的,谁知道之后学习到这个wc,然后就觉得自己还是嫩了点,还是要多学习。

wc参数格式

wc [-clmw] [file ...]

wc命令参数

-c 统计字节数。

-l 统计行数。

-m 统计字符数。这个标志不能与 -c 标志一起使用。

-w 统计字数。一个字被定义为由空白、跳格或换行字符分隔的字符串。

-L 打印最长行的长度。

常用参数示例

$ ls
test.txt

$ wc test.txt
      26      26     103 test.txt

$ wc -l test.txt  #直接一把统计行数
      26 test.txt

$ wc -c test.txt
     103 test.txt

date & cal

date介绍

When invoked without arguments, the date utility displays the current date and time. Otherwise, depending on the options specified, date will set the date and time or print it in a user-defined way.

The date utility displays the date and time read from the kernel clock. When used to set the date and time, both the kernel clock and the hardware clock are updated.

Only the superuser may set the date, and if the system securelevel (see securelevel(7)) is greater than 1, the time may not be changed by more than 1 second.

当不带参数调用时,date实用程序将显示当前日期和时间。 否则,根据指定的选项,日期将设置日期和时间或以用户定义的方式打印日期和时间。

date实用程序显示从内核时钟读取的日期和时间。 当用于设置日期和时间时,内核时钟和硬件时钟都将更新。

只有超级用户可以设置日期,并且如果系统安全级别(请参阅securelevel(7))大于1,则时间更改不得超过1秒。

date参数格式

date [-jRu] [-r seconds | filename] [-v [+|-]val[ymwdHMS]] ... [+output_fmt] date [-jnu] [[[mm]dd]HH]MM[[cc]yy][.ss] date [-jnRu] -f input_fmt new_date [+output_fmt] date [-d dst] [-t minutes_west]

date命令参数

%H 小时(以00-23来表示)。

%I 小时(以01-12来表示)。

%K 小时(以0-23来表示)。

%l 小时(以0-12来表示)。

%M 分钟(以00-59来表示)。

%P AM或PM。

%r 时间(含时分秒,小时以12小时AM/PM来表示)。

%s 总秒数。起算时间为1970-01-01 00:00:00 UTC。

%S 秒(以本地的惯用法来表示)。

%T 时间(含时分秒,小时以24小时制来表示)。

%X 时间(以本地的惯用法来表示)。

%Z 市区。

%a 星期的缩写。

%A 星期的完整名称。

%b 月份英文名的缩写。

%B 月份的完整英文名称。

%c 日期与时间。只输入date指令也会显示同样的结果。

%d 日期(以01-31来表示)。

%D 日期(含年月日)。

%j 该年中的第几天。

%m 月份(以01-12来表示)。

%U 该年中的周数。

%w 该周的天数,0代表周日,1代表周一,异词类推。

%x 日期(以本地的惯用法来表示)。

%y 年份(以00-99来表示)。

%Y 年份(以四位数来表示)。

%n 在显示时,插入新的一行。

%t 在显示时,插入tab。

MM 月份(必要)

DD 日期(必要)

hh 小时(必要)

mm 分钟(必要)

ss 秒(选择性)

-d<字符串> 显示字符串所指的日期与时间。字符串前后必须加上双引号。

-s<字符串> 根据字符串来设置日期与时间。字符串前后必须加上双引号。

-u 显示GMT。

常用参数示例

$ date
2019年12月22日 星期日 21时28分29秒 CST

$ date '+%c'
日 12/22 21:28:33 2019

$  date '+%D'
12/22/19

$  date '+%T'
21:28:50

$ date '+%X'
21时29分00秒

cal介绍

cal命令可以用来显示公历(阳历)日历。

cal参数格式

cal [-3hjy] [-A number] [-B number] [[month] year] cal [-3hj] [-A number] [-B number] -m month [year] ncal [-3hjJpwy] [-A number] [-B number] [-s country_code] [[month] year] ncal [-3hJeo] [-A number] [-B number] [year] ncal [-CN] [-H yyyy-mm-dd] [-d yyyy-mm]

cal命令参数

-1 显示一个月的月历

-3 显示系统前一个月,当前月,下一个月的月历

-s 显示星期天为一个星期的第一天,默认的格式

-m 显示星期一为一个星期的第一天
-j 显示在当年中的第几天(一年日期按天算,从1月1号算起,默认显示当前月在一年中的天数)
-y 显示当前年份的日历

cal常用参数示例

$ cal
      十二月 2019
日 一 二 三 四 五 六
 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


$ ncal
    十二月 2019
一      2  9 16 23 30
二      3 10 17 24 31
三      4 11 18 25
四      5 12 19 26
五      6 13 20 27
六      7 14 21 28
日   1  8 15 22 29

which

which介绍

The which utility takes a list of command names and searches the path for each executable file that would be run had these commands actually been invoked.

which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。

which参数格式

-n 指定文件名长度,指定的长度必须大于或等于所有文件中最长的文件名。

-p 与-n参数相同,但此处的包括了文件的路径。

-w 指定输出时栏位的宽度。

which常用参数示例

which ls
alias ls='ls --color=auto'
    /bin/ls

which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    /bin/alias
    /usr/bin/which

whereis

whereis介绍

whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。

whereis参数格式

whereis [program ...]

whereis命令参数

-b 定位可执行文件。

-m 定位帮助文件。

-s 定位源代码文件。

-u 搜索默认路径下除可执行文件、源代码文件、帮助文件以外的其它文件。

-B 指定搜索可执行文件的路径。

-M 指定搜索帮助文件的路径。

-S 指定搜索源代码文件的路径。

whereis常用参数示例

$ whereis ls
/bin/ls

$ whereis whereis
/usr/bin/whereis

nl

nl介绍

nl命令在linux系统中用来计算文件中行号。nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等等的功能。

nl参数格式

nl [-p] [-b type] [-d delim] [-f type] [-h type] [-i incr] [-l num] [-n format] [-s sep] [-v startnum] [-w width] [file]

nl命令参数

-b :指定行号指定的方式,主要有两种:

-b a :表示不论是否为空行,也同样列出行号(类似 cat -n);

-b t :如果有空行,空的那一行不要列出行号(默认值);

-n :列出行号表示的方法,主要有三种:

-n ln :行号在萤幕的最左方显示;

-n rn :行号在自己栏位的最右方显示,且不加 0 ;

-n rz :行号在自己栏位的最右方显示,且加 0 ;

-w :行号栏位的占用的位数。

-p 在逻辑定界符处不重新开始计算。

nl常用参数示例

$ nl 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    .

ps

ps介绍

The ps utility displays a header line, followed by lines containing information about all of your processes that have controlling terminals.

ps实用程序显示标题行,其后是包含有关具有控制终端的所有进程的信息的行。

ps参数格式

ps [-AaCcEefhjlMmrSTvwXx] [-O fmt | -o fmt] [-G gid[,gid...]] [-g grp[,grp...]] [-u uid[,uid...]] [-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]] ps [-L]

ps命令参数

a 显示所有进程

-a 显示同一终端下的所有程序

-A 显示所有进程

c 显示进程的真实名称

-N 反向选择

-e 等于“-A”

e 显示环境变量

f 显示程序间的关系

-H 显示树状结构

r 显示当前终端的进程

T 显示当前终端的所有程序

u 指定用户的所有进程

-au 显示较详细的资讯

-aux 显示所有包含其他使用者的行程

-C<命令> 列出指定命令的状况

--lines<行数> 每页显示的行数

--width<字符数> 每页显示的字符数

ps常用参数示例

#查看所有进程
$ps -a
#查看进程的环境变量和程序间的关系
$ps -ef

kill&killall

kill介绍

The command kill sends the specified signal to the specified process or process group. If no signal is specified, the TERM signal is sent. The TERM signal will kill processes which do not catch this signal. For other processes, it may be necessary to use the KILL (9) signal, since this signal cannot be caught.

命令kill将指定的信号发送到指定的进程或进程组。 如果未指定信号,则发送TERM信号。 TERM信号将杀死不捕获该信号的进程。 对于其他过程,可能需要使用KILL(9)信号,因为无法捕获该信号。

kill参数格式

kill [-s signal|-p] [-q sigval] [-a] [--] pid...
kill -l [signal]

kill命令参数

-l 信号,若果不加信号的编号参数,则使用“-l”参数会列出全部的信号名称

-a 当处理当前进程时,不限制命令名和进程号的对应关系

-p 指定kill 命令只打印相关进程的进程号,而不发送任何信号

-s 指定发送信号

-u 指定用户

kill常用参数示例

#查看当前系统信号
kill -l
 1) SIGHUP     2) SIGINT   3) SIGQUIT  4) SIGILL   5) SIGTRAP
 6) SIGABRT     7) SIGBUS   8) SIGFPE   9) SIGKILL 10) SIGUSR1
11) SIGSEGV    12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT    17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN    22) SIGTTOU 23) SIGURG  24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM    27) SIGPROF 28) SIGWINCH    29) SIGIO   30) SIGPWR
31) SIGSYS    34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4    39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9    44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14    49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11    54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6    59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1    64) SIGRTMAX
解释

HUP 1 终端断线
INT 2 中断(同 Ctrl + C)
QUIT 3 退出(同 Ctrl + \)
TERM 15 终止
KILL 9 强制终止
CONT 18 继续(与STOP相反, fg/bg命令)
STOP 19 暂停(同 Ctrl + Z)

kill -9 是我们使用的最多的信号,其实这种方式一点也不优雅,应该使用kill -15信号,大部分程序接收到SIGTERM信号后,会先释放自己的资源,然后再停止。但是也有程序可能接收信号后,做一些其他的事情(如果程序正在等待IO,可能就不会立马做出响应,等到io完成后在结束),也就是说,SIGTERM多半是会被阻塞的。

讲到这里,linux基本指令差不多了够用了,差不多可以上你的服务器上随便搞搞了。但是也只是简单看看,更多的操作指令请看进阶部分。


由于掘金长度要求,文章进阶部分命令详解 同步发在微信公众号

微信搜索 DC952721 或者扫描二维码,即可订阅。

qrcode_for_gh_c0c6a2272e93_258
qrcode_for_gh_c0c6a2272e93_258