Linux (四) 基础命令 下

个人博客网:https://wushaopei.github.io/    (你想要这里多有)

一、查看文件内容

1、命令  cat

对应单词:concatenate

作用:查看文件内容

  • 常用参数:

参数名

作用

-b

显示行号,仅针对非空白行做行号显示,空白行不标行号

-n

显示行号,连同空白行也会有行号,与 -b 的选项不同

-E

将结尾的断行字节 $ 显示出来

-T

将 [tab] 按键以 ^I 显示出来

2、命令  tac

按照和cat相反的方式显示文件内容

3、命令  more

作用:一页一页显示文件内容

控制方式:

[1]空格键:向下翻一页

[2]回车键:向下一行

[3]q:离开more命令的显示结果

[4]f:向下滚动一屏   fall下降

[5]b:向上滚动一屏 back回退

[6]=:输出当前行行号 

[7]:f:输出文件名和当前行号  

4、命令  less

作用:和more一样,但支持更多功能

控制方式

[1]空格键:向下翻一页

[2]回车键:向下一行

[3]q:离开less命令的显示结果

[4]f:向下滚动一屏

[5]b:向上滚动一屏

[6]=:输出当前位置信息

[7]pageDown:向下翻动一页

[8]pageUp:向上翻动一页

[9]/字串:向下搜索字串

[10]?字串:向上搜索字串

[11]n :重复前一个搜寻 (与 / 或 ? 有关)

[12]N :反向的重复前一个搜寻 (与 / 或 ? 有关)

5、命令  head

作用:查看前面几行

用法:head -n 10 example.txt

6、命令  tail

作用:查看文件最后几行

普通用法:tail -n 10 example.txt 查看后10行

高端用法:tail -f example.txt 实时查看文件末尾新值内容

此时还可以指定行数:tail -f 20 example.txt

二、查找

1、命令find

作用:查找文件或目录

格式:find [查找范围] [参数] [表达式]

注意:表达式最好放在引号中

例如:find /root -name '*.txt'

2、命令grep

作用:在文件内部查找内容

格式:grep+参数+查找内容+源文件

如果想要在多个源文件中查找,则多个文件名用空格分开

参数

-c

只输出匹配行的数量

-h

查询多文件时不显示文件名

-n

显示匹配行及行号

-s

不显示不存在或无匹配文本的错误信息

-v

显示不包含匹配文本的所有行

更多的时候用在管道中。

 

三、进程端口号操作

1、ps命令

①作用:查看进程

②对应单词:process status

③相关进程信息

  PID:进程ID

 TTY:进程所在终端

 TIME:进程所占用的CPU时间

 CMD:启动进程的命令

 UID:用户ID

 PPID:父进程ID

 C:用整数表示的CPU使用率

 STIME:进程启动时间

④经典用法

ps -ef

ps aux

效果相同,仅仅是不同发行版中的不同风格差异

⑤杀死进程

使用ps -ef|grep 进程名查看进程id

ps -ef|grep bash|grep -v grep

使用kill -9 进程id实现杀死进程

kill -9 39537

⑥多重管道举例:

ps -ef|grep bin/bash|grep -v grep|wc -l

2、netstat命令

①作用:

查看网络状态

②对应单词:

net status

③经典用法

[1]单独使用:netstat -anp

[2]组合使用:netstat -anp|grep :8080查看8080端口监听情况

四、压缩解压缩操作

1、 tar命令

1.1 压缩

tar -zcvf [压缩包文件名] [要压缩的内容]

1.1.1 查看压缩包内容

tar -tvf [压缩包文件名]

1.1.2 解压缩:解压到当前目录下

tar -zxvf [压缩包文件名]

1.1.3 解压缩:解压到目标目录下

tar -zxvf [压缩包文件名] -C [目标目录]

注意:参数C是大写的。

1.2 zip/unzip命令[了解]

1.2.1 unzip命令查看压缩包内的内容

[root@pc0420 zip_test]# unzip -v total.zip

Archive:  total.zip

Length   Method    Size  Cmpr    Date    Time   CRC-32   Name

--------  ------  ------- ---- ---------- ----- --------  ----

      33  Defl:N        9  73% 08-15-2017 10:15 d4cff4ae  aaa.txt

      33  Defl:N        9  73% 08-15-2017 10:15 9ae6a16f  bbb.txt

      42  Defl:N        9  79% 08-15-2017 10:15 78b08634  ccc.txt

--------          -------  ---                            -------

     108               27  75%                            3 files

1.2.2 解压到当前目录下

解压到当前目录下,如果当前目录下已经存在同名文件,则询问处理方式

unzip [压缩包文件名]

[root@pc0420 zip_test]# unzip total.zip

Archive:  total.zip

replace aaa.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: r

new name: aaa_new.txt

inflating: aaa_new.txt

1.2.3 解压到指定目录

unzip命名把压缩包内容解压到指定目录下,目标目录即使不存在都无妨,会自动创建

unzip [压缩包文件名] -d [目标目录路径]

[root@pc0420 zip_test]# unzip total.zip -d target/

Archive:  total.zip

  inflating: target/aaa.txt          

  inflating: target/bbb.txt          

  inflating: target/ccc.txt  

1.2.4 压缩路径对压缩包内目录结构的影响

压缩时使用相对路径,压缩包内只有被压缩的文件。

zip total.zip ./*

Archive:  total.zip

Length   Method    Size  Cmpr    Date    Time   CRC-32   Name

--------  ------  ------- ---- ---------- ----- --------  ----

      33  Defl:N        9  73% 08-15-2017 10:15 d4cff4ae  aaa.txt

      33  Defl:N        9  73% 08-15-2017 10:15 9ae6a16f  bbb.txt

      42  Defl:N        9  79% 08-15-2017 10:15 78b08634  ccc.txt

--------          -------  ---                            -------

     108               27  75%                            3 files

压缩时使用绝对路径,压缩包内包含完整的目录结构

zip path_target.zip /tmp/yasuo/zip_test/*

Archive:  path_target.zip

Length   Method    Size  Cmpr    Date    Time   CRC-32   Name

--------  ------  ------- ---- ---------- ----- --------  ----

      33  Defl:N        9  73% 08-15-2017 10:15 d4cff4ae  tmp/yasuo/zip_test/aaa_new.txt

      33  Defl:N        9  73% 08-15-2017 10:15 d4cff4ae  tmp/yasuo/zip_test/aaa.txt

      33  Defl:N        9  73% 08-15-2017 10:15 9ae6a16f  tmp/yasuo/zip_test/bbb_new.txt

      33  Defl:N        9  73% 08-15-2017 10:15 9ae6a16f  tmp/yasuo/zip_test/bbb.txt

      42  Defl:N        9  79% 08-15-2017 10:15 78b08634  tmp/yasuo/zip_test/ccc_new.txt

      42  Defl:N        9  79% 08-15-2017 10:15 78b08634  tmp/yasuo/zip_test/ccc.txt

     162  Stored      162   0% 08-15-2017 10:18 e53eaf77  tmp/yasuo/zip_test/directory.zip

       0  Stored        0   0% 08-15-2017 10:18 00000000  tmp/yasuo/zip_test/happy/

       0  Stored        0   0% 08-15-2017 10:25 00000000  tmp/yasuo/zip_test/target/

     475  Stored      475   0% 08-15-2017 10:17 80788e43  tmp/yasuo/zip_test/total.zip

--------          -------  ---                            -------

     853              691  19%                            10 files

 

1.3 gzip/gunzip命令[了解]

gzip 文件名

gunzip 文件名

 

只能压缩文件,不能压缩目录

不保留原文件

五 、 其他命令

1、history

查看到目前为止Linux系统中当前用户所有执行过的命令。

如果有需要可以使用“>”或“>>”操作符将命令执行结果写入文件。

>:覆盖写

>>:追加写

2、pwd

对应单词:print working directory

打印当前所在目录

3、echo

输出数据。通常用来输出系统环境变量的值,系统环境变量可以通过“$变量名”的方式引用。

例如:echo $PATH

 

六、 命令帮助

1、man命令

    1.1 对应单词:manual手册

    1.2 用法:man 命令名

     1.3 举例:man ls

LS(1)                            User Commands                           LS(1)

 

NAME

       ls - list directory contents

 

SYNOPSIS

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

 

DESCRIPTION

       List  information  about  the  FILEs (the current directory by default).  Sort

       entries alphabetically if none of -cftuvSUX nor --sort.

 

       Mandatory arguments to long options are mandatory for short options too.

 

       -a, --all

              do not ignore entries starting with .

 

       -A, --almost-all

              do not list implied . and ..

 

       --author

              with -l, print the author of each file

 

:

……

       Written by Richard M. Stallman and David MacKenzie.

 

REPORTING BUGS

       Report ls bugs to bug-coreutils@gnu.org

       GNU coreutils home page: <http://www.gnu.org/software/coreutils/>

       General help using GNU software: <http://www.gnu.org/gethelp/>

       Report ls translation bugs to <http://translationproject.org/team/>

 

COPYRIGHT

       Copyright  © 2010 Free Software Foundation, Inc.  License GPLv3+: GNU GPL ver-

       sion 3 or later <http://gnu.org/licenses/gpl.html>.

       This is free software: you are free to change and redistribute it.   There  is

       NO WARRANTY, to the extent permitted by law.

 

SEE ALSO

       The  full documentation for ls is maintained as a Texinfo manual.  If the info

       and ls programs are properly installed at your site, the command

 

              info coreutils 'ls invocation'

 

       should give you access to the complete manual.

 

GNU coreutils 8.4                  May 2016                              LS(1)

(END)

1.4  操作

翻页:PageUp、PageDown

上下:Up、Down

退出:q

1.5 数字说明

[1]用户在Shell环境中可以操作的命令或是可执行的文件

[2]系统内核(kernel)可以调用的函数

[3]常用的函数or函数库

[4]设备配置文件

[5]配置文件的格式

[6]游戏相关

[7]Linux网络协议和文件系统

[8]系统管理员可以用的命令

[9]跟内核有关系的文件

2、 help参数

  2.1 格式:具体命令名 --help

  2.2 原理:很多命令都包含help参数

  2.3 举例:ls --help

[root@Computer 桌面]# ls --help

用法:ls [选项]... [文件]...

列出 FILE 的信息(默认为当前目录)。

如果不指定-cftuvSUX 或--sort 选项,则根据字母大小排序。

 

长选项必须使用的参数对于短选项时也是必需使用的。

  -a, --all            不隐藏任何以. 开始的项目

  -A, --almost-all        列出除. 及.. 以外的任何项目

      --author            与-l 同时使用时列出每个文件的作者

  -b, --escape            以八进制溢出序列表示不可打印的字符

      --block-size=大小        块以指定大小的字节为单位

  -B, --ignore-backups        不列出任何以"~"字符结束的项目

  -c                配合-lt:根据ctime 排序并显示ctime(文件

                状态最后更改的时间)

                配合-l:显示ctime 但根据名称排序                其他情况:按ctime 排序

  -C                每栏由上至下列出项目

      --color[=WHEN]        控制是否使用色彩分辨文件。WHEN 可以是

                "never"(默认)、"always"或"auto"其中之一

  -d, --directory        当遇到目录时列出目录本身而非目录内的文件

  -D, --dired            产生适合Emacs 的dired 模式使用的结果

  -f                不进行排序,-aU 选项生效,-lst 选项失效

  -F, --classify        加上文件类型的指示符号(*/=@| 其中一个)

      --format=关键字        交错-x,逗号分隔-m,水平-x,长-l,

                单栏-1,详细-l,垂直-C

      --full-time        即-l --time-style=full-iso

  -g                类似-l,但不列出所有者

      --group-directories-first

            在文件前分组目录。此选项可与--sort 一起使用,

            但是一旦使用--sort=none (-U)将禁用分组

  -G, --no-group        以一个长列表的形式,不输出组名

  -h, --human-readable        与-l 一起,以易于阅读的格式输出文件大小

                (例如 1K 234M 2G)

      --si            同上面类似,但是使用1000 为基底而非1024

  -H, --dereference-command-line

                跟随命令行列出的符号链接

      --dereference-command-line-symlink-to-dir

                跟随命令行列出的目录的符号链接

      --hide=PATTERN             隐藏符合PATTERN 模式的项目

                (-a 或 -A 将覆盖此选项)

      --indicator-style=方式    指定在每个项目名称后加上指示符号方式:

                none (默认),classify (-F),file-type (-p)

  -i, --inode            显示每个文件的inode 号

  -I, --ignore=PATTERN        不显示任何符合指定shell PATTERN 的项目

  -k                即--block-size=1K

  -l                使用较长格式列出信息

  -L, --dereference        当显示符号链接的文件信息时,显示符号链接所指示

                的对象而并非符号链接本身的信息

  -m                所有项目以逗号分隔,并填满整行行宽

  -n, --numeric-uid-gid        类似 -l,但列出UID 及GID 号

  -N, --literal            输出未经处理的项目名称 (如不特别处理控制字符)

  -o                类似 -l,但不列出有关组的信息

  -p,  --indicator-style=slash    对目录加上表示符号"/"

  -q, --hide-control-chars    以"?"字符代替无法打印的字符

      --show-control-chars    直接显示无法打印的字符 (这是默认方式,除非调用

                的程序名称是"ls"而且是在终端输出结果)

  -Q, --quote-name        将条目名称括上双引号

      --quoting-style=方式    使用指定的quoting 方式显示条目的名称:

                literal、locale、shell、shell-always、c、escape

  -r, --reverse            排序时保留顺序

  -R, --recursive        递归显示子目录

  -s, --size            以块数形式显示每个文件分配的尺寸

  -S                根据文件大小排序

      --sort=WORD        以下是可选用的WORD 和它们代表的相应选项:

                extension -X       status   -c

                none      -U       time     -t

                size      -S       atime    -u

                time      -t       access   -u

                version   -v       use      -u

      --time=WORD        和-l 同时使用时显示WORD 所代表的时间而非修改时

                间:atime、access、use、ctime 或status;加上

                --sort=time 选项时会以指定时间作为排序关键字

      --time-style=STYLE    和-l 同时使用时根据STYLE 代表的格式显示时间:

                full-iso、iso、locale、posix-iso、+FORMAT。

                FORMAT 即是"date"所用的时间格式;如果FORMAT

                是FORMAT1<换行>FORMAT2,FORMAT1 适用于较旧

                的文件而FORMAT2 适用于较新的文件;如果STYLE

                以"posix-"开头,则STYLE 仅在POSIX 语系之外

                生效。

  -t                根据修改时间排序

  -T, --tabsize=宽度    指定制表符(Tab)的宽度,而非8 个字符

  -t            按修改时间排序

  -T, --tabsize=COLS    指定制表符(Tab)的宽度,而非8个字符

  -u            同-lt 一起使用:按照访问时间排序并显示

            同-l一起使用:显示访问时间并按文件名排序

            其他:按照访问时间排序

  -U            不进行排序;按照目录顺序列出项目

  -v            在文本中进行数字(版本)的自然排序

  -w, --width=COLS           assume screen width instead of current value

  -x                         list entries by lines instead of by columns

  -X                         sort alphabetically by entry extension

  -1                         list one file per line

 

SELinux options:

 

  --lcontext                 Display security context.   Enable -l. Lines

                             will probably be too wide for most displays.

  -Z, --context              Display security context so it fits on most

                             displays.  Displays only mode, user, group,

                             security context and file name.

  --scontext                 Display only security context and file name.

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

      --version        显示版本信息并退出

 

SIZE 可以是一个可选的整数,后面跟着以下单位中的一个:

KB 1000,K 1024,MB 1000*1000,M 1024*1024,还有 G、T、P、E、Z、Y。

 

使用色彩来区分文件类型的功能已被禁用,默认设置和 --color=never 同时禁用了它。

使用 --color=auto 选项,ls 只在标准输出被连至终端时才生成颜色代码。

LS_COLORS 环境变量可改变此设置,可使用 dircolors 命令来设置。

 

 

退出状态:

 0  正常

 1  一般问题 (例如:无法访问子文件夹)

 2  严重问题 (例如:无法使用命令行参数)

 

请向bug-coreutils@gnu.org 报告ls 的错误

GNU coreutils 项目主页:<http://www.gnu.org/software/coreutils/>

GNU 软件一般性帮助:<http://www.gnu.org/gethelp/>

请向<http://translationproject.org/team/zh_CN.html> 报告ls 的翻译错误

要获取完整文档,请运行:info coreutils 'ls invocation'

3、 info命令

  info 具体命令

File: coreutils.info,  Node: What information is listed,  Next: Sorting the out

put,  Prev: Which files are listed,  Up: ls invocation

10.1.2 What information is listed

---------------------------------

These options affect the information that `ls' displays.  By default,

only file names are shown.

`--author'

     List each file's author when producing long format directory

     listings.  In GNU/Hurd, file authors can differ from their owners,

     but in other operating systems the two are the same.

`-D'

`--dired'

     With the long listing (`-l') format, print an additional line after

     the main output:

          //DIRED// BEG1 END1 BEG2 END2 ...

     The BEGN and ENDN are unsigned integers that record the byte

--zz-Info: (coreutils.info.gz)What information is listed, 278 lines --Top-------

空格键翻页

q键退出

 

七、 Linux关机命令

1、 慎重

关机前一定要确认确实要执行这个操作,否则可能造成数据丢失或服务出错等严重问题。千万不要主观的认为关机就是关机,直接关就是了,而是要考虑数据同步,通知已登录用户,保证安全操作。就真实生产环境而言,应用服务器极少关机,有可能出于维护等目的重启。

2、数据同步

使用sync命令可以将内存中的数据修改保存到硬盘上,为关机或重启做准备。执行关机重启前要先做这个操作。

3、关机命令

  3.1 延迟关机

shutdown -h 延迟分钟数 "群发消息"

shutdown -h 10 "It is going to rain,get your clothes"

                    

3.2 立即关机

shutdown -h now

halt

poweroff

3.3 阻止关机

shutdown -c

 

4、重启命令

   4.1 延迟重启

shutdown -r 延迟分钟数 "群发消息"

shutdown -r 10 "It is going to rain,get your clothes"

 

   4.2 立即重启

shutdown -r now

reboot

原文地址:https://www.cnblogs.com/wushaopei/p/11721140.html