大数据开发基础入门与项目实战(二)Java Web数据可视化之4.Linux基本操作命令和功能

前言

本文主要介绍了Linux的基本操作命令和功能,包括常用Linux命令的基本使用、打包和解包命令、时间日期、搜索查找和vi编辑器的使用,是Linux操作的基础

1.常用Linux命令的基本使用

(1)Linux常用快捷键

Linux中常用的快捷键如下:

快捷键含义
tab键命令或者路径提示及补全
ctrl+c放弃当前输入,终止当前任务或程序
ctrl+l清屏;
clear命令也能达到相同的效果
ctrl + insert复制
鼠标右键粘贴
alt+c断开连接
ctrl + shfit + R重新连接
alt+1/2/3/4/5…切换会话窗口
上下键查找执行行过的命令,也可以使用history命令

(2)命令格式及帮助手册使用

终端的命令格式为:

command [-options] [parameter]

其中:

  • command

    命令名,相应功能的英文单词或单词的缩写。

  • [-options]

    选项, 可用来对命令进行控制,也可以省略。

  • parameter

    传给命令的参数,可以是零个、一个或者多个。

例如ls -ltouch abc.txt等。

因为一个命令有很多可选项,一般不可能全记住,所以需要借助手册查阅,由2种方式:

(1)—help 帮助信息

格式为:

command --help

用于显示 command命令的帮助信息。

例如:

[root@localhost /]# ls --help
用法:ls [选项]... [文件]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all                     不隐藏任何以. 开始的项目
  -A, --almost-all              列出除. 及.. 以外的任何项目
      --author                  与-l 同时使用时列出每个文件的作者
  -b, --escape                  以八进制溢出序列表示不可打印的字符
      --block-size=SIZE      scale sizes by SIZE before printing them; e.g.,
                               '--block-size=M' prints sizes in units of
                               1,048,576 bytes; see SIZE format below
  -B, --ignore-backups       do not list implied entries ending with ~
  -c                         with -lt: sort by, and show, ctime (time of last
                               modification of file status information);
                               with -l: show ctime and sort by name;
                               otherwise: sort by ctime, newest first
...
  -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 is an integer and optional unit (example: 10M is 10*1024*1024).  Units
are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).

使用色彩来区分文件类型的功能已被禁用,默认设置和 --color=never 同时禁用了它。
使用 --color=auto 选项,ls 只在标准输出被连至终端时才生成颜色代码。
LS_COLORS 环境变量可改变此设置,可使用 dircolors 命令来设置。


退出状态:
 0  正常
 1  一般问题 (例如:无法访问子文件夹)
 2  严重问题 (例如:无法使用命令行参数)

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告ls 的翻译错误
要获取完整文档,请运行:info coreutils 'ls invocation'

缺点:

虽然可以查询命令的帮助信息,但是没有提供翻页、搜索功能。

(2)man手册

格式为:

man command

查询 command 命令的使用手册。

其中,man是manual 的缩写,是Linux提供的一个手册,包含了绝大部分的命令、函数的详细使用说明。

使用man时的操作键如下:

操作键功能
空格键显示手册的下一屏
Enter键一次滚动一行
b回滚一屏
f前滚一屏
q(quit)退出
/word搜索word字符串
n(next)搜索下一个
N搜索上一个

例如:

stage2 module6 man commond

可以看到,可以查看和操作手册。

常用的命令如下:

命令对应英文作用
lslist查看当前目录下的内容
pwdprint working derectory查看当前所在目录
cd [目录名]change directory切换目录
touch [文件名]touch如果文件不存在,新建文件
mkdir [目录名]make directory创建目录
rm [文件名]remove删除指定的文件名
clearclear清屏

(3)切换目录的命令

pwd查看当前所在路径。

cd切换目录。常见用法如下:

命令含义
cd …切换到上级目录
cd -后退到上一次所在目录
cd /切换到根目录
cd ~切换到家目录,即root目录,也可以用cd命令

其中,cd后的参数可以是绝对路径,也可以是相对路径:

  • 绝对路径:/开始的目录,从根目录开始

  • 相对路径:直接目录,从当前目录开始

如下:

# 查看当前所在目录
[root@localhost /] pwd
/
# 切换到 /usr/local(绝对路径)
[root@localhost /] cd /usr/local/
# 切换到 上一级 /usr
[root@localhost local] cd ..
# 切换到 /usr/tmp (相对路径)
[root@localhost usr] cd tmp/
# 切换回 /usr/local
[root@localhost tmp] cd /usr/local/
# 后退到上一次所在目录
[root@localhost local] cd -
/usr/tmp

(4)展示目录的命令

列出目录内容使用命令ls

常见选项如下:

选项含义
-a(all)所有,包含隐藏文件
-l查看内容的详细信息,效果等同于ll命令
-h(human)人性化的显示(单位为K、G等)
-R树结构显示某文件夹下的所有文件(包括子文件夹)

使用如下:

# 查看/usr内容
[root@localhost /] cd /usr/
[root@localhost usr]# ls
bin  etc  games  include  lib  lib64  libexec  local  sbin  share  src  tmp

# 查看所有/usr内容(既包含隐藏,也包含非隐藏)
[root@localhost usr] ls -a
.  ..  bin  etc  games  include  lib  lib64  libexec  local  sbin  share  src  tmp
# 查看/usr详细内容
[root@localhost usr] ls -l
总用量 100
dr-xr-xr-x.  2 root root 20480 819 03:24 bin
drwxr-xr-x.  2 root root     6 411 2018 etc
drwxr-xr-x.  2 root root     6 411 2018 games
drwxr-xr-x.  3 root root    23 819 03:23 include
dr-xr-xr-x. 27 root root  4096 819 03:24 lib
dr-xr-xr-x. 37 root root 20480 819 03:24 lib64
drwxr-xr-x. 21 root root  4096 819 03:24 libexec
drwxr-xr-x. 12 root root   131 819 03:23 local
dr-xr-xr-x.  2 root root 12288 819 03:24 sbin
drwxr-xr-x. 76 root root  4096 819 03:24 share
drwxr-xr-x.  4 root root    34 819 03:23 src
lrwxrwxrwx.  1 root root    10 819 03:23 tmp -> ../var/tmp

# 简化查看/usr详细内容
[root@localhost usr] ll
总用量 100
dr-xr-xr-x.  2 root root 20480 819 03:24 bin
drwxr-xr-x.  2 root root     6 411 2018 etc
drwxr-xr-x.  2 root root     6 411 2018 games
drwxr-xr-x.  3 root root    23 819 03:23 include
dr-xr-xr-x. 27 root root  4096 819 03:24 lib
dr-xr-xr-x. 37 root root 20480 819 03:24 lib64
drwxr-xr-x. 21 root root  4096 819 03:24 libexec
drwxr-xr-x. 12 root root   131 819 03:23 local
dr-xr-xr-x.  2 root root 12288 819 03:24 sbin
drwxr-xr-x. 76 root root  4096 819 03:24 share
drwxr-xr-x.  4 root root    34 819 03:23 src
lrwxrwxrwx.  1 root root    10 819 03:23 tmp -> ../var/tmp
# 易懂简化查看/usr详细内容
[root@localhost usr] ll -h
总用量 100K
dr-xr-xr-x.  2 root root  20K 819 03:24 bin
drwxr-xr-x.  2 root root    6 411 2018 etc
drwxr-xr-x.  2 root root    6 411 2018 games
drwxr-xr-x.  3 root root   23 819 03:23 include
dr-xr-xr-x. 27 root root 4.0K 819 03:24 lib
dr-xr-xr-x. 37 root root  20K 819 03:24 lib64
drwxr-xr-x. 21 root root 4.0K 819 03:24 libexec
drwxr-xr-x. 12 root root  131 819 03:23 local
dr-xr-xr-x.  2 root root  12K 819 03:24 sbin
drwxr-xr-x. 76 root root 4.0K 819 03:24 share
drwxr-xr-x.  4 root root   34 819 03:23 src
lrwxrwxrwx.  1 root root   10 819 03:23 tmp -> ../var/tmp


其中,ls -a查看mu录中的所有内容时,.代表当前目录、..代表上一层目录。

(5)创建目录和删除目录

通过 mkdir 命令 创建目录,语法格式为:

mkdir [-p] 要创建的目录

其中, 选项-p为parent,表示可以递归创建目录。

使用如下:

[root@localhost ~]# mkdir aaa
[root@localhost ~]# mkdir -p bbb/ccc
[root@localhost ~]# ls -R
.:
aaa  anaconda-ks.cfg  bbb

./aaa:

./bbb:
ccc

./bbb/ccc:

rmdir 删除一个空的目录。

如果该目录中存在文件或其他目录是该命令是不能删除的。

使用如下:

[root@localhost ~]# rmdir aaa/
[root@localhost ~]# rmdir bbb/
rmdir: 删除 "bbb/" 失败: 目录非空
[root@localhost ~]# cd bbb/
[root@localhost bbb]# rmdir ccc/
[root@localhost bbb]# cd ..
[root@localhost ~]# rmdir bbb/
[root@localhost ~]# ls
anaconda-ks.cfg

(6)创建文件和删除文件

创建文件的命令为:

touch 文件名称

touch 文件名1 文件2 ... 可以创建多个文件。

删除命令语法为:

rm [-参数] 文件/目录

rm对应的英文是remove,含义为删除。

常见的选项如下:

选项英文含义
-fforce(强制)强制删除,忽略不存在的文件,无需提示
-rrecursive(递归)递归地删除目录下的内容,删除文件夹时必须加此参数

Linux ouch rm

常见用法如下:

文件操作命令
touch
文件名:
创建文件
rm:
删除文件或目录
rm
文件名:删除一个文件
rm
-f文件名:不经确认就删除文件
rm
-r
目录:
递归删除一个目录及目录中的内容
n
-rf目录:递归删除一个目录,并且不经确认
rm
-rf
*
:清空当前文件夹
rm
n
-rf/*
自杀行为,
不要尝试

(7)复制与剪切命令

通过 cp 实现复制将指定的 文件 或 目录 复制到 两一个 文件 或 目录中

基本语法如下:

(1)cp source dest (功能描述:复制source文件到dest)
(2)cp -r sourceFolder targetFolder (功能描述:递归复制整个文件夹)

命令 英文 作用
-r recursive (递归) 递归复制目标目录的内容

使用如下:

[root@localhost lxDemo]# touch abc.txt
[root@localhost lxDemo]# cp abc.txt cba.txt
[root@localhost lxDemo]# ls
abc.txt  cba.txt
[root@localhost lxDemo]# cp abc.txt ..
[root@localhost lxDemo]# cp abc.txt ../cba.txt
[root@localhost lxDemo]# ls ..
abc.txt  anaconda-ks.cfg  cba.txt  lxDemo
[root@localhost lxDemo]# mkdir -p aaa/bbb
[root@localhost lxDemo]# cp -r aaa/bbb/ ..
[root@localhost lxDemo]# ls ..
abc.txt  anaconda-ks.cfg  bbb  cba.txt  lxDemo
[root@localhost lxDemo]# mkdir -p aaa/bbb/ccc
[root@localhost lxDemo]# cp -r aaa ..

通过 mv 命令可以用来 移动 文件 或 目录, 也可以给 文件或目录重命名。

基本语法:

(1)mv oldNameFile newNameFile (功能描述:重命名)
(2)mv /temp/movefile /targetFolder (功能描述:递归移动文件)

使用如下:

[root@localhost lxDemo]# mv abc.txt aaabbcc.txt
[root@localhost lxDemo]# ls
aaa  aaabbcc.txt  cba.txt
[root@localhost lxDemo]# mkdir -p 111/222/333
[root@localhost lxDemo]# mv 111 ..
[root@localhost lxDemo]# ls ..
111  aaa  abc.txt  anaconda-ks.cfg  bbb  cba.txt  lxDemo

(8)cat查看文件命令

查看文件内容,从第一行开始显示。
1)基本语法

cat [选项] 要查看的文件

选项:

-b :列出行号,仅针对非空白行做行号显示,空白行不标行号!
-E :将结尾的断行行字节 $ 显示出来;
-n :列出行号,连同空白行也会有行号,与 -b 的选项不不同;
-T :将 [tab] 按键以 ^I 显示出来;
-v :列出一些看不出来的特殊字符
-A :相当于 -vET 的整合选项,可列列出一些特殊字符而不不是空白而已;

使用如下:

[root@instance-6m0ylrf0 lxDemo]# cat abc.txt 
123
                fefsfs

ad
aa



fadfsfv
666 fsd 

奥运

😊 ('◡')

o(* ̄▽ ̄*)ブ

csfdfsfrse
[root@instance-6m0ylrf0 lxDemo]# cat -b abc.txt 
     1  123
     2                  fefsfs

     3  ad
     4  aa



     5  fadfsfv
     6  666 fsd 

     7  奥运

     8  😊 ('◡')

     9  o(* ̄▽ ̄*)10  csfdfsfrse
[root@instance-6m0ylrf0 lxDemo]# cat -E abc.txt 
123$
                fefsfs$
$
ad$
aa$
$
$
$
fadfsfv$
666 fsd $
$
奥运$
$
😊 ('◡')$
$
o(* ̄▽ ̄*)ブ$
$
csfdfsfrse$
[root@instance-6m0ylrf0 lxDemo]# cat -n abc.txt 
     1  123
     2                  fefsfs
     3  
     4  ad
     5  aa
     6  
     7  
     8  
     9  fadfsfv
    10  666 fsd 
    11  
    12  奥运
    13  
    14  😊 ('◡')
    15  
    16  o(* ̄▽ ̄*)17  
    18  csfdfsfrse
[root@instance-6m0ylrf0 lxDemo]# cat -T abc.txt 
123
^I^Ifefsfs

ad
aa



fadfsfv
666 fsd 

奥运

😊 ('◡')

o(* ̄▽ ̄*)ブ

csfdfsfrse
[root@instance-6m0ylrf0 lxDemo]# cat -v abc.txt 
123
                fefsfs

ad
aa



fadfsfv
666 fsd 

M-eM-%M-%M-hM-?M-^P

M-pM-^_M-^XM-^J (M-bM-^WM-^O'M-bM-^WM-!'M-bM-^WM-^O)

o(*M-oM-?M-#M-bM-^VM-=M-oM-?M-#*)M-cM-^CM-^V

csfdfsfrse
[root@instance-6m0ylrf0 lxDemo]# cat -A abc.txt 
123$
^I^Ifefsfs$
$
ad$
aa$
$
$
$
fadfsfv$
666 fsd $
$
M-eM-%M-%M-hM-?M-^P$
$
M-pM-^_M-^XM-^J (M-bM-^WM-^O'M-bM-^WM-!'M-bM-^WM-^O)$
$
o(*M-oM-?M-#M-bM-^VM-=M-oM-?M-#*)M-cM-^CM-^V$
$
csfdfsfrse$

(9)more和less命令查看文件

cat命令一般用于查看小文件,查看大文件需要用到more和less命令。

more命令用于查看文件内容,一页一页的显示文件内容。

基本语法如下:

more 要查看的文件

使用如下:

操作键含义
空格键(space)向下翻一页
Enter向下翻一行
q代表立刻离开 more ,不再显示该文件内容
Ctrl+F向下滚动一屏
Ctrl+B返回上一屏
=输出当前行的行号

如下:

more use

less 的作用与 more 十分相似,都可以用来浏览文字档案的内容,不同的是 less 允许使用[pageup]、[pagedown]往回滚动。

语法格式如下:

less 要查看的文件

操作说明:

操作键含义
空格键向下翻动一页
pagedown向下翻动一页
pageup向上翻动一页
/字符串向下搜寻字符串;
n向下查找,N向上查找
q退出 less 程序

使用如下:

less use

(10)head命令和tail命令查看文件

head命令用于查看文件内容,只看头几行,优点:对于大文件不必都加载,只显示头几行行即可。

参数如下:

head 文件名 :查看前10行
head -n 3 文件名 :查看文件的前3行
head -c 3 文件名 :查看文件的前3个字符

使用如下:

[root@localhost lxDemo]# head more.txt 
MORE(1)                                             User Commands                                             MORE(1)



NAME
       more - file perusal filter for crt viewing

SYNOPSIS
       more [options] file [...]

[root@localhost lxDemo]# head -n 3 more.txt 
MORE(1)                                             User Commands                                             MORE(1)


[root@localhost lxDemo]# head -c 3 more.txt 
MOR

tail命令用于查看文件内容,只看尾巴几行行,优点:可以查看⽂文件实时追加的内容。

参数如下:

(1)tail -n 10 文件 (功能描述:查看文件头(从末尾开始数)10行行内容,10可以是任意行行数)
(2)tail -f 文件 (功能描述:实时追踪该文档的所有更更新)

使用如下:

[root@localhost lxDemo]# tail more.txt 
       currently in use in the Linux community.  Documentation was produced using several other versions of  the  man
       page, and extensive inspection of the source code.

AVAILABILITY
       The  more  command  is  part  of  the  util-linux  package  and is available from Linux Kernel Archive ⟨ftp://
       ftp.kernel.org/pub/linux/utils/util-linux/⟩.



util-linux                                          September 2011                                            MORE(1)
[root@localhost lxDemo]# tail -n 5 more.txt 
       ftp.kernel.org/pub/linux/utils/util-linux/⟩.



util-linux                                          September 2011                                            MORE(1)
[root@localhost lxDemo]# tail -f more.txt 
       currently in use in the Linux community.  Documentation was produced using several other versions of  the  man
       page, and extensive inspection of the source code.

AVAILABILITY
       The  more  command  is  part  of  the  util-linux  package  and is available from Linux Kernel Archive ⟨ftp://
       ftp.kernel.org/pub/linux/utils/util-linux/⟩.



util-linux                                          September 2011                                            MORE(1)

^Z
[1]+  已停止               tail -f more.txt
[root@localhost lxDemo]# tail -5f more.txt 
       ftp.kernel.org/pub/linux/utils/util-linux/⟩.



util-linux                                          September 2011                                            MORE(1)
^Z
[2]+  已停止               tail -5f more.txt

tail -f 文件一般可以用于监控日志的改变。

(11)重定向输出符号

Linux中的命令组合后,可以实现神奇的功能:

  • append追加

  • replace 替换,覆盖

> 重定向输出

>> 又追加功能;示例:
cat /etc/passwd a.txt将输出定向到a.txt中

cat letc/passwd >> a.txt 输出并且追加

使用如下:

[root@localhost ~]# cd /usr/tmp/
[root@localhost tmp]# rm -rf *
[root@localhost tmp]# touch 1.txt 2.txt
[root@localhost tmp]# vim 1.txt
[root@localhost tmp]# cat 1.txt 
text 1
[root@localhost tmp]# vim 2.txt 
[root@localhost tmp]# cat 2.txt 
text 2
[root@localhost tmp]# ll -ht
总用量 8.0K
-rw-r--r--. 1 root root 7 819 21:38 2.txt
-rw-r--r--. 1 root root 7 819 21:38 1.txt
[root@localhost tmp]# touch 3.txt
[root@localhost tmp]# ll -ht
总用量 8.0K
-rw-r--r--. 1 root root 0 819 21:39 3.txt
-rw-r--r--. 1 root root 7 819 21:38 2.txt
-rw-r--r--. 1 root root 7 819 21:38 1.txt
[root@localhost tmp]# cat 2.txt > 3.txt 
[root@localhost tmp]# cat 3.txt 
text 2
[root@localhost tmp]# cat 1.txt > 3.txt 
[root@localhost tmp]# cat 3.txt 
text 1
[root@localhost tmp]# cat 2.txt >> 3.txt 
[root@localhost tmp]# cat 3.txt 
text 1
text 2
[root@localhost tmp]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.31.155  netmask 255.255.255.0  broadcast 192.168.31.255
        inet6 fe80::25eb:a0e1:77f0:e5ba  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:8a:e8:31  txqueuelen 1000  (Ethernet)
        RX packets 289660  bytes 60183298 (57.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 556992  bytes 500904383 (477.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 36  bytes 3132 (3.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 36  bytes 3132 (3.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost tmp]# ifconfig >> 3.txt 
[root@localhost tmp]# tail 3.txt 

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 36  bytes 3132 (3.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 36  bytes 3132 (3.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


(12)管道符及逻辑控制&&

管道符号| 的作用是: 将一个命令的输出作为另一个命令的输入.

配合使用的命令:

ps(Process Status) 进程状态 ps -ef
grep(Global Regular Expression Print) 全局正则表达式版本(搜索)

管道是Linux命令中重要的一个概念, 其作用是将一个命令的输出用作另一个命令的输入。
示例
Is–help|more分页查询帮助信息
ps-ef|grep java查询名称中包含java的进程

使用如下:

[root@localhost tmp]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 15:46 ?        00:00:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root          2      0  0 15:46 ?        00:00:00 [kthreadd]
root          3      2  0 15:46 ?        00:00:04 [ksoftirqd/0]
root          5      2  0 15:46 ?        00:00:00 [kworker/0:0H]
...
root     111652 111605  0 21:41 ?        00:00:00 /usr/libexec/openssh/sftp-server
root     111659 111605  0 21:41 ?        00:00:00 /usr/libexec/openssh/sftp-server
root     111767 111607  0 21:41 pts/3    00:00:01 top
root     119246      2  0 21:44 ?        00:00:00 [kworker/0:0]
root     126080      2  0 21:47 ?        00:00:00 [kworker/0:2]
[root@localhost tmp]# ps -ef | grep java
root       7827  93428  0 21:53 pts/0    00:00:00 grep --color=auto java
[root@localhost tmp]# ls --help | more
用法:ls [选项]... [文件]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all                     不隐藏任何以. 开始的项目
  -A, --almost-all              列出除. 及.. 以外的任何项目
      --author                  与-l 同时使用时列出每个文件的作者
  -b, --escape                  以八进制溢出序列表示不可打印的字符
      --block-size=SIZE      scale sizes by SIZE before printing them; e.g.,
                               '--block-size=M' prints sizes in units of
                               1,048,576 bytes; see SIZE format below
  -B, --ignore-backups       do not list implied entries ending with ~
  -c                         with -lt: sort by, and show, ctime (time of last
                               modification of file status information);
                               with -l: show ctime and sort by name;
                               otherwise: sort by ctime, newest first
  -C                         list entries by columns
      --color[=WHEN]         colorize the output; WHEN can be 'never', 'auto',
                               or 'always' (the default); more info below
  -d, --directory            list directories themselves, not their contents
...
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 is an integer and optional unit (example: 10M is 10*1024*1024).  Units
are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).

使用色彩来区分文件类型的功能已被禁用,默认设置和 --color=never 同时禁用了它。
使用 --color=auto 选项,ls 只在标准输出被连至终端时才生成颜色代码。
LS_COLORS 环境变量可改变此设置,可使用 dircolors 命令来设置。


退出状态:
 0  正常
 1  一般问题 (例如:无法访问子文件夹)
 2  严重问题 (例如:无法使用命令行参数)

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告ls 的翻译错误
要获取完整文档,请运行:info coreutils 'ls invocation'
[root@localhost tmp]# ls --help | grep '递归'
  -R, --recursive               递归显示子目录

命令间的逻辑控制:
命令之间使用&&连接,实现类似逻辑与的功能:
只有在&&左边的命令运行成功时,&&右边的命令才会被执行;
只要左边命令运行失败,后面的命令就不会被执行。
例如:
cp 1.txt 2.txt && cat 2.txt

使用如下:

[root@localhost tmp]# cat 1.txt && touch 4.txt
text 1
[root@localhost tmp]# ll
总用量 12
-rw-r--r--. 1 root root   7 819 21:38 1.txt
-rw-r--r--. 1 root root   7 819 21:38 2.txt
-rw-r--r--. 1 root root 910 819 21:44 3.txt
-rw-r--r--. 1 root root   0 819 22:06 4.txt
[root@localhost tmp]# cat 100.txt && touch 5.txt
cat: 100.txt: 没有那个文件或目录
[root@localhost tmp]# ll
总用量 12
-rw-r--r--. 1 root root   7 819 21:38 1.txt
-rw-r--r--. 1 root root   7 819 21:38 2.txt
-rw-r--r--. 1 root root 910 819 21:44 3.txt
-rw-r--r--. 1 root root   0 819 22:06 4.txt

逻辑控制&&的应用场景:

用在需要把一些命令组合使用的场景下,例如启动服务并查看日志。

因为启动软件通常不会打印启动的日志信息,所以需要再打开对应的日志信息查看。

例如启动tomcat服务器./startup.sh和查看日志tail -100f catalina.out一般不能在一个窗口中同时执行,要是需要在启动服务的同时就查看日志,就可以用&&进行关联,即./startup.sh && tail -50f ../logs/catalina.out,就可以实现在启动tomcat后,再用tail命令查看日志,如果启动失败,则不查看。

(13)history查看历史命令

使用如下:

[root@localhost tmp]# history | tail
'
  209  ls --help | grep '递归'
  210  clear 
  211  cat 1.txt && touch 4.txt
  212  ll
  213  cat 100.txt && touch 5.txt
  214  ll
  215  history | wc -l
  216  clear 
  217  history | tail
[root@localhost tmp]# history > 4.txt 
[root@localhost tmp]# head 4.txt 
    1  ll
    2  ping www.baidu.com
    3  vi /etc/sysconfig/network-scripts/ifcfg-ens33 
    4  systemctl restart network
    5  ping www.baidu.com
    6  ip addr
    7  service status sshd
    8  systemctl status sshd
    9  ping 192.168.43.79
   10  cat /etc/sysconfig/network-scripts/ifcfg-ens33

2.打包和压缩

(1)打tar包和解tar包

打包是将一个文件(或目录)打包为一个文件,打包之后的文件需要以.tar结尾。

tar选项如下:

选项英文含义
ccreate创建打包文件
vverbosely(啰嗦地)报告打包的完整进度
ffile指定创建的文件名称,f后面一定是.tar文件, 所以必须放到最后

示意如下:

将一系列文件打包成 一个大文件

tar -cvf 打包名.tar 被打包的目录 用于打包一个目录

tar -cvf 打包名.tar 被打包的文件1 被打包的文件2 被打包的文件3 用于打包多个文件

使用如下:

[root@localhost lxDemo]$ ls
aaa  aaabbcc.txt  cba.txt  ls.txt  more.txt
[root@localhost lxDemo]$ tar -cvf test.tar more.txt 
more.txt
[root@localhost lxDemo]$ ll -ht
总用量 40K
-rw-r--r--. 1 root root  10K 819 23:49 test.tar
-rw-r--r--. 1 root root 5.3K 819 19:07 more.txt
-rw-r--r--. 1 root root  18K 819 19:07 ls.txt
drwxr-xr-x. 3 root root   17 819 18:29 aaa
-rw-r--r--. 1 root root    0 819 18:27 cba.txt
-rw-r--r--. 1 root root    0 819 18:27 aaabbcc.txt
[root@localhost lxDemo]$ tar -cvf abc.tar more.txt ls.txt cba.txt 
more.txt
ls.txt
cba.txt
[root@localhost lxDemo]$ ll -ht
总用量 72K
-rw-r--r--. 1 root root  30K 819 23:51 abc.tar
-rw-r--r--. 1 root root  10K 819 23:49 test.tar
-rw-r--r--. 1 root root 5.3K 819 19:07 more.txt
-rw-r--r--. 1 root root  18K 819 19:07 ls.txt
drwxr-xr-x. 3 root root   17 819 18:29 aaa
-rw-r--r--. 1 root root    0 819 18:27 cba.txt
-rw-r--r--. 1 root root    0 819 18:27 aaabbcc.txt

可以看到,打包后的文件更大。

解包是将打包后的文件进行逆向操作、分解成一系列小文件。

选项如下:

选项英文含义
xextract(提取)解包
C(大写)directory(目录)默认保存到当前目录,通过-C更改解压目录,需要保证目录存在

示意如下:

将一个打包后的 分解成 一系列小文件, 分解位置为 当前目录

tar -xvf 打包名.tar

将一个打包后的 分解成 一系列小文件, 分解位置为 指定目录

tar -xvf 打包名.tar -C 解包路径位置

使用如下:

[root@localhost lxDemo]$ rm -rf *.txt
[root@localhost lxDemo]$ ls
aaa  abc.tar  test.tar
[root@localhost lxDemo]$ tar -xvf test.tar 
more.txt
[root@localhost lxDemo]$ ls
aaa  abc.tar  more.txt  test.tar
[root@localhost lxDemo]$ tar -xvf abc.tar -C aaa/
more.txt
ls.txt
cba.txt
[root@localhost lxDemo]$ ls aaa/
bbb  cba.txt  ls.txt  more.txt

小结如下:

打包: tar -cvf 打包之后的文件名.tar 被打包的目录或文件名
解包: tar -xvf 打包之后的文件名.tar [ -C 指定解包位置 ]

(2)压缩与解压缩

打包 和 压缩 是不同的:
打包只是单纯地将多个文件生成一个文件存储;

压缩操作不但要生成一个文件,还要通过压缩工具对文件进行压缩,以减小文件占用存储空间大小。

在 Linux 中, 最常用的压缩文件格式是 xxx.tar.gz
在 tar 命令中有一个选项 -z 可以调用 gzip , 从而可以方便的实现压缩和解压缩的功能

其中,选项如下:

命令英文含义
zgzip使用gzip压缩和解压缩
jbzip2使用bzip2压缩和解压缩

命令格式如下:

压缩文件

tar -zcvf 打包压缩文件名.tar.gz 被压缩的文件/目录

解压缩文件

tar -zxvf 打包文件.tar.gz

解压缩到指定路径

tar -zxvf 打包文件.tar.gz -C 目录路径

使用如下:

[root@localhost lxDemo]$ ll -ht
总用量 52K
drwxr-xr-x. 3 root root   62 819 23:57 aaa
-rw-r--r--. 1 root root  30K 819 23:51 abc.tar
-rw-r--r--. 1 root root  10K 819 23:49 test.tar
-rw-r--r--. 1 root root 5.3K 819 19:07 more.txt
[root@localhost lxDemo]$ tar -zcvf more.tar.gz more.txt 
more.txt
[root@localhost lxDemo]$ ll -ht
总用量 56K
-rw-r--r--. 1 root root 2.2K 820 00:08 more.tar.gz
drwxr-xr-x. 3 root root   62 819 23:57 aaa
-rw-r--r--. 1 root root  30K 819 23:51 abc.tar
-rw-r--r--. 1 root root  10K 819 23:49 test.tar
-rw-r--r--. 1 root root 5.3K 819 19:07 more.txt
[root@localhost lxDemo]$ rm -f more.txt 
[root@localhost lxDemo]$ ll -ht
总用量 48K
-rw-r--r--. 1 root root 2.2K 820 00:08 more.tar.gz
drwxr-xr-x. 3 root root   62 819 23:57 aaa
-rw-r--r--. 1 root root  30K 819 23:51 abc.tar
-rw-r--r--. 1 root root  10K 819 23:49 test.tar
[root@localhost lxDemo]$ tar -xzvf more.tar.gz 
more.txt
[root@localhost lxDemo]$ ll -ht
总用量 56K
-rw-r--r--. 1 root root 2.2K 820 00:08 more.tar.gz
drwxr-xr-x. 3 root root   62 819 23:57 aaa
-rw-r--r--. 1 root root  30K 819 23:51 abc.tar
-rw-r--r--. 1 root root  10K 819 23:49 test.tar
-rw-r--r--. 1 root root 5.3K 819 19:07 more.txt
[root@localhost lxDemo]$ tar -xzvf more.tar.gz -C aaa/
more.txt
[root@localhost lxDemo]$ ll -ht aaa/
总用量 28K
-rw-r--r--. 1 root root 5.3K 819 19:07 more.txt
-rw-r--r--. 1 root root  18K 819 19:07 ls.txt
drwxr-xr-x. 3 root root   17 819 18:29 bbb
-rw-r--r--. 1 root root    0 819 18:27 cba.txt

可以看到,经过压缩得到的压缩包比原文件更小。

bzip 是压缩的第二种方式
在 Linux 中, bzip2 压缩文件格式是 xxx.tar.bz2
在 tar 命令中有一个选项 -j 可以调用 bzip2 , 从而可以方便的实现压缩和解压缩的功能

命令格式如下:

压缩文件

tar -jcvf 打包压缩文件名.tar.bz2 被压缩的文件/目录

解压缩文件 (绩效潍坊)

tar -jxvf 打包文件.tar.bz2

解压缩到指定路径

tar -jxvf 打包文件.tar.bz2 -C 目录路径

注意事项: 如果报错tar (child): bzip2:无法 exec: 没有那个文件或目录,说明系统中还没有安装bzip2的包,执行yum install -y bzip2即可安装。

使用如下:

[root@localhost lxDemo]$ ll -ht
总用量 56K
drwxr-xr-x. 3 root root   62 820 00:11 aaa
-rw-r--r--. 1 root root 2.2K 820 00:08 more.tar.gz
-rw-r--r--. 1 root root  30K 819 23:51 abc.tar
-rw-r--r--. 1 root root  10K 819 23:49 test.tar
-rw-r--r--. 1 root root 5.3K 819 19:07 more.txt
[root@localhost lxDemo]$ tar -cjvf more.tar.bz2 more.txt 
more.txt
[root@localhost lxDemo]$ ll -ht
总用量 60K
-rw-r--r--. 1 root root 2.3K 820 00:23 more.tar.bz2
drwxr-xr-x. 3 root root   62 820 00:11 aaa
-rw-r--r--. 1 root root 2.2K 820 00:08 more.tar.gz
-rw-r--r--. 1 root root  30K 819 23:51 abc.tar
-rw-r--r--. 1 root root  10K 819 23:49 test.tar
-rw-r--r--. 1 root root 5.3K 819 19:07 more.txt
[root@localhost lxDemo]$ rm -f more.txt 
[root@localhost lxDemo]$ ll -ht
总用量 52K
-rw-r--r--. 1 root root 2.3K 820 00:23 more.tar.bz2
drwxr-xr-x. 3 root root   62 820 00:11 aaa
-rw-r--r--. 1 root root 2.2K 820 00:08 more.tar.gz
-rw-r--r--. 1 root root  30K 819 23:51 abc.tar
-rw-r--r--. 1 root root  10K 819 23:49 test.tar
[root@localhost lxDemo]$ tar -xjvf more.tar.bz2 
more.txt
[root@localhost lxDemo]$ ll -ht
总用量 60K
-rw-r--r--. 1 root root 2.3K 820 00:23 more.tar.bz2
drwxr-xr-x. 3 root root   62 820 00:11 aaa
-rw-r--r--. 1 root root 2.2K 820 00:08 more.tar.gz
-rw-r--r--. 1 root root  30K 819 23:51 abc.tar
-rw-r--r--. 1 root root  10K 819 23:49 test.tar
-rw-r--r--. 1 root root 5.3K 819 19:07 more.txt

小结如下:

打包压缩: tar -jcvf 打包之后的文件名.tar.bz2 被打包压缩的目录或文件名
解包解压缩: tar -jxvf 打包之后的文件名.tar.bz2 [ -C 指定解包位置 ]

3.时间日期

date命令用于显示时间。

显示当前时间常用命令如下:

(1)date (功能描述:显示当前时间)
(2)date +%Y (功能描述:显示当前年年份)
(3)date +%m (功能描述:显示当前月份)
(4)date +%d (功能描述:显示当前是哪一天)
(5)date +%Y%m%d … (功能描述:显示当前年年月日各种格式 )
(6)date “+%Y-%m-%d %H:%M:%S” 或者单引号也可以 (功能描述:显示年年⽉月⽇日时分秒)

显示非当前时间常用命令如下:

(1)date -d ‘1 days ago’ (功能描述:显示前一天日期)
(2)date -d yesterday +"%Y-%m-%d"(同上)
(3)date -d next-day +"%Y-%m-%d" (功能描述:显示明天日期)
(4)date -d ‘next monday’ (功能描述:显示下周一时间)

date设置系统时间的基本语法如下:

date -s 字符串时间

使用如下:

[root@localhost lxDemo]$ date
2021年 08月 20日 星期五 00:26:19 CST
[root@localhost lxDemo]$ date +%Y
2021
[root@localhost lxDemo]$ date +%m
08
[root@localhost lxDemo]$ date +%d
20
[root@localhost lxDemo]$ date "+%Y-%m=%d %H:%M:%S"
2021-08=20 00:27:52
[root@localhost lxDemo]$ date "+%Y-%m-%d %H:%M:%S"
2021-08-20 00:27:59
[root@localhost lxDemo]$ date -d '1 days ago'
2021年 08月 19日 星期四 00:28:29 CST
[root@localhost lxDemo]$ date -d yesterday +%Y-%m-%d
2021-08-19
[root@localhost lxDemo]$ date -d next-day +%Y-%m-%d
2021-08-21
[root@localhost lxDemo]$ date -d 'next monday'
2021年 08月 23日 星期一 00:00:00 CST
[root@localhost lxDemo]$ date -s '2021-08-19 22:02:30'
2021年 08月 19日 星期四 22:02:30 CST
[root@localhost lxDemo]$ date
2021年 08月 19日 星期四 22:02:36 CST

上面显示的都是字符串描述的时间,不是当前时间。

cal命令用于查看日历。

基本语法如下:

cal [选项] (功能描述:不加选项,显示本月日历)

常见选项如下:

-3 ,显示系统前一个月,当前月,下一个月的日历
具体某一年年,显示这一年年的日历。

使用如下:

[root@localhost lxDemo]$ cal
      八月 2021     
日 一 二 三 四 五 六
 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

[root@localhost lxDemo]$ cal -3
      七月 2021             八月 2021             九月 2021     
日 一 二 三 四 五 六  日 一 二 三 四 五 六  日 一 二 三 四 五 六
             1  2  3   1  2  3  4  5  6  7            1  2  3  4
 4  5  6  7  8  9 10   8  9 10 11 12 13 14   5  6  7  8  9 10 11
11 12 13 14 15 16 17  15 16 17 18 19 20 21  12 13 14 15 16 17 18
18 19 20 21 22 23 24  22 23 24 25 26 27 28  19 20 21 22 23 24 25
25 26 27 28 29 30 31  29 30 31              26 27 28 29 30      
                                                                
[root@localhost lxDemo]$ cal 2021
                               2021                               

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

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


4.搜索查找

(1)find查找命令

find命令用来查找文件或者目录,是根据文件的属性进行查找,如文件名、文件大小、所有者、所属组、是否为空、访问时间、修改时间等。

语法格式如下:

 find path [options]

常见命令如下:

(1)按照文件名查找

(1)find /etc -name yum.conf #在/etc目录下文件yum.conf
(2)find /etc -name ‘yum’ #使用通配符*(0或者任意多个)。表示在/etc目录下查找文件名中含有
字符串‘yum’的文件

(3)find . -name ‘yum*’ #表示当前目录下查找文件名开头是字符串‘yum’的文件

使用如下:

[root@localhost lxDemo]$ find /etc/ -name yum.conf
/etc/yum.conf
[root@localhost lxDemo]$ find /etc/ -name 'yum'
/etc/yum
/etc/logrotate.d/yum
[root@localhost lxDemo]$ find /etc/ -name yum*
/etc/yum.repos.d
/etc/yum
/etc/logrotate.d/yum
/etc/yum.conf

(2)按照文件特征查找

(1)find / -atime -2 # 查找在系统中最后48小时访问的文件 (Access Time,文件读取访问时
间)
(2)find / -empty # 查找在系统中为空的文件或者文件夹
(3)find / -group corley # 查找在系统中属于group为corley的文件
(4)find / -mtime -1 #查找在系统中最后24小时里修改过的文件 (modify time)
(5)find / -user corley #查找在系统中属于corley这个用户的文件
(6)find / -size +10000c #查找出大于10000字节的文件(c:字节,w:双字,k:KB,M:MB,G:GB)
(7)find / -size -1000k #查找出小于1000KB的文件

使用如下:

[root@localhost lxDemo]$ find / -atime -2 | head
/
/boot
/boot/efi
/boot/efi/EFI
/boot/efi/EFI/centos
/boot/grub2
/boot/grub2/device.map
/boot/grub2/i386-pc
/boot/grub2/i386-pc/gcry_rmd160.mod
/boot/grub2/i386-pc/acpi.mod
[root@localhost lxDemo]$ find / -empty | tail
find: ‘/proc/79712/task/79712/fd/6’: 没有那个文件或目录
find: ‘/proc/79712/task/79712/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/79712/fd/5’: 没有那个文件或目录
find: ‘/proc/79712/fdinfo/5’: 没有那个文件或目录
/usr/local/share/man/man9x
/usr/local/share/man/mann
/usr/local/src
/usr/src/debug
/usr/src/kernels
/home
/media
/mnt
/opt
/srv
[root@localhost lxDemo]$ find / -group corley
find: ‘corley’ 不是已存在用户组的名称
[root@localhost lxDemo]$ find / -mtime -1 | head
/
/boot
/boot/efi
/boot/efi/EFI
/boot/grub2
/boot/grub2/device.map
/boot/grub2/i386-pc
/boot/grub2/i386-pc/gcry_rmd160.mod
/boot/grub2/i386-pc/acpi.mod
/boot/grub2/i386-pc/gcry_rsa.mod
[root@localhost lxDemo]$ find / -size +10000c | tail
find: ‘/proc/80196/task/80196/fd/6’: 没有那个文件或目录
find: ‘/proc/80196/task/80196/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/80196/fd/5’: 没有那个文件或目录
find: ‘/proc/80196/fdinfo/5’: 没有那个文件或目录
/usr/libexec/postfix/nqmgr
/usr/libexec/postfix/qmgr
/usr/libexec/microcode_ctl/check_caveats
/usr/libexec/man-db/globbing
/usr/libexec/man-db/manconv
/usr/libexec/sudo/group_file.so
/usr/libexec/sudo/libsudo_util.so.0.0.0
/usr/libexec/sudo/sesh
/usr/libexec/sudo/sudo_noexec.so
/usr/libexec/sudo/sudoers.so
[root@localhost lxDemo]$ find / -size -1000k | head
/
/boot
/boot/efi
/boot/efi/EFI
/boot/efi/EFI/centos
/boot/grub2
/boot/grub2/device.map
/boot/grub2/i386-pc
/boot/grub2/i386-pc/gcry_rmd160.mod
/boot/grub2/i386-pc/acpi.mod

(3)使用混合查找方式查找文件

参数有: !,-and(-),-or(-o)。

常见命令如下:

(1)find /tmp -size +10c -and -mtime +2 #在/tmp目录下查找大于10字节并在2天前修改的文

(2)find / -user root -or -user corley #在/目录下查找用户是root或者corley的文件文件
(3)find /tmp ! -user corley #在/tmp目录中查找所有不属于corley用户的文件

使用如下:

[root@localhost lxDemo]$ find /usr/ -size +10c -and -mtime +2 | head
/usr/bin/cp
/usr/bin/gzip
/usr/bin/alias
/usr/bin/csplit
/usr/bin/bash
/usr/bin/cut
/usr/bin/zcat
/usr/bin/fmt
/usr/bin/zcmp
/usr/bin/bashbug-64

(2)grep过滤查找

grep是根据文件的内容进行查找,会对文件的每一行按照给定的模式(patter)进行匹配查找。

语法格式如下:

grep [options] 范围

主要选项如下:

选项含义
-c只输出匹配行的计数
-i不区分大小写
-n显示匹配行及行号
-w显示整个单词
-r递归查询

如下:

1)在workspace.xml文件中查找project
2)查找project统计行号
3)统计value的个数
4)查找component并忽略大小写
5)查找java单词
6)递归查询/usr目录下 含有project字段的文件

使用如下:

[root@localhost lxDemo]$ grep project workspace.xml 
<project version="4">
    <property name="project.structure.last.edited" value="Modules" />
    <property name="project.structure.proportion" value="0.15" />
    <property name="project.structure.side.proportion" value="0.2" />
</project>
[root@localhost lxDemo]$ grep -n project workspace.xml 
2:<project version="4">
55:    <property name="project.structure.last.edited" value="Modules" />
56:    <property name="project.structure.proportion" value="0.15" />
57:    <property name="project.structure.side.proportion" value="0.2" />
214:</project>
[root@localhost lxDemo]$ grep -c workspace.xml 
^Z
[2]+  已停止               grep --color=auto -c workspace.xml
[root@localhost lxDemo]$ grep -c value workspace.xml 
64
[root@localhost lxDemo]$ grep -i component workspace.xml 
  <component name="AnalysisUIOptions">
  </component>
  <component name="AutoImportSettings">
  </component>
  <component name="ChangeListManager">
  </component>
  <component name="CompilerWorkspaceConfiguration">
  </component>
  <component name="FileTemplateManagerImpl">
  </component>
  <component name="GitSEFilterConfiguration">
  </component>
  <component name="ProjectId" id="1v7SYRLnEwwh6D3nuxbRekj7XeX" />
  <component name="ProjectViewState">
  </component>
  <component name="PropertiesComponent">
  </component>
  <component name="RecentsManager">
  </component>
  <component name="RunManager" selected="Application.JDBCPreparedStatementTest">
  </component>
  <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
  <component name="TaskManager">
  </component>
  <component name="TypeScriptGeneratedFilesManager">
  </component>
  <component name="XDebuggerManager">
  </component>
[root@localhost lxDemo]$ grep -i java workspace.xml 
    <property name="Repository.Attach.JavaDocs" value="false" />
    <property name="settings.editor.selected.configurable" value="preferences.sourceCode.Java" />
      <recent name="C:\Users\LENOVO\Desktop\Study\BigData_Lagou\Exercise\javase\resources" />
      <recent name="C:\Users\LENOVO\Desktop\Study\BigData_Lagou\Exercise\javase\src\com\stage1\module3" />
      <module name="javase" />
      <module name="javase" />
      <module name="javase" />
      <module name="javase" />
        <breakpoint enabled="true" type="java-exception">
          <properties class="java.lang.NullPointerException" package="java.lang" />
        <line-breakpoint enabled="true" type="java-line">
          <url>file://$PROJECT_DIR$/src/com/stage1/module2/ShapeRectTest.java</url>
        <line-breakpoint enabled="true" type="java-line">
          <url>file://$PROJECT_DIR$/src/com/stage1/module2/ShapeRectTest.java</url>
        <line-breakpoint enabled="true" type="java-line">
          <url>file://$PROJECT_DIR$/src/com/stage1/module2/Shape.java</url>
[root@localhost lxDemo]$ grep -iw java workspace.xml 
    <property name="settings.editor.selected.configurable" value="preferences.sourceCode.Java" />
        <breakpoint enabled="true" type="java-exception">
          <properties class="java.lang.NullPointerException" package="java.lang" />
        <line-breakpoint enabled="true" type="java-line">
          <url>file://$PROJECT_DIR$/src/com/stage1/module2/ShapeRectTest.java</url>
        <line-breakpoint enabled="true" type="java-line">
          <url>file://$PROJECT_DIR$/src/com/stage1/module2/ShapeRectTest.java</url>
        <line-breakpoint enabled="true" type="java-line">
          <url>file://$PROJECT_DIR$/src/com/stage1/module2/Shape.java</url>
[root@localhost lxDemo]$ grep -r project /usr/ | head
匹配到二进制文件 /usr/bin/cp
匹配到二进制文件 /usr/bin/csplit
匹配到二进制文件 /usr/bin/cut
匹配到二进制文件 /usr/bin/fmt
匹配到二进制文件 /usr/bin/test
匹配到二进制文件 /usr/bin/timeout
匹配到二进制文件 /usr/bin/fold
匹配到二进制文件 /usr/bin/touch
匹配到二进制文件 /usr/bin/tr
匹配到二进制文件 /usr/bin/groups

5.vi编辑器

(1)vi编辑器的使用

vi编辑器在Linux中具有至关重要的地位。

在Linux下一般使用vi编辑器来编辑文件。
vi既可以查看文件也可以编辑文件。
而vim是vi的升级版本,具备更多的功能。
vi如果目标文件不存在,会创建新的文件。但是如果新文件没做编辑,退出后还会消失。

vi three mode

三种模式(状态):编辑、底行、命令模式
切换到编辑模式:按i、o、a键;
切换到底行模式:按:(冒号);
切换到命令行模式:按Esc键;
更多详细用法,可以查询文档。

三种模式之间的切换如下:

vi change mode

编辑模式(插入模式):对文本进行输入和修改
底行模式:退出vim或者查找、替换功能
命令模式(一般模式):通过快捷命令操作数据,打开vi默认就是命令模式

如果vim命令不能使用,需要执行yum -y install vim-enhanced安装。

命令模式按下:i、o、a进入编辑模式:
i:光标不动
o:另起一行
a:光标到下一个字符
按ESC退出编辑模式,进入命令模式

使用示意如下:
vi edit

命令模式下,按‘:’或者‘/’进入底行模式,可以输入命令
1)退出vim:(重点重点重点)
:q 未编辑时退出vim
:q! 编辑后,退出并且不保存
:wq 编辑后,退出且保存
:x 编译后保存

撤销上次操作(扩展—一般模式下):

u 撤销上一次操作(ctrl + z windows操作)
ctrl + r 恢复上一次被撤销的操作 (ctrl + y windows操作)

设置行号(了解) 底行模式
:set nu 显示行号
:set nonu 不显示行号

示意如下:

vim command

4)替换文本(了解)
😒/old/new/ 用new替换old,替换当前行的第一个匹配
😒/old/new/g 用new替换old,替换当前行的所有匹配
:%s/old/new/ 用new替换old,替换所有行的第一个匹配
:%s/old/new/g 用new替换old,替换整个文件的所有匹配
5)查找 (一般模式)
/文本 搜索指定文本,高亮显示,按n显示下一个,按N显示前一个
:整数 快捷跳转到指定行

示意如下:

vim replace search

(2)vi编辑器复制与剪切

vi编辑器复制与剪切的命令如下:

p(pause) 将之前dd或yy的数据粘贴到光标位置
yy 复制光标所在行
5yy 复制光标及下面共5行
dd 剪切当前行
5dd 剪切光标及下面共5行

示意如下:

vim cut paste

总结

Linux中是通过命令来进行相关操作的,与Windows等系统可以直接点选等方式不一样,因此要想使用好Linux系统,就必须熟悉操作的命令和常用参数。

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东哥说AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值