龙空技术网

linux 文件管理

斩获必有所得 102

前言:

今天同学们对“xci安装软件”大体比较珍视,大家都想要分析一些“xci安装软件”的相关知识。那么小编在网络上网罗了一些关于“xci安装软件””的相关资讯,希望小伙伴们能喜欢,姐妹们一起来了解一下吧!

FHS 介绍FHS:让使用者可以了解到一安装软件存放位置,让所有的开发者,遵从这个标准FHS 重点在于规范每个特定的目录下应该要放置什么样的数据,它有四种形态1、static:不变的 有些数据补偿变动,例如函数库,说明文档等2、variable 可变动的 服务程序有关的socket 是经常变动的。3、shareable 可分享的 nfs 网络文件等4、unshareable 不可分享的 /etc/ 系统的二进制文件等FHS 要求必须存在的目录/bin:二进制可执行文件,root和一般用户都可以使用/boot:系统引导程序存放目录/dev:接口设备以文件形式存放在这个目录中/etc:系统主要配置文件存放露露/lib:系统函数库存放目录/media:存放可以移除的设备 CD/DVD USB/mnt: 临时挂载额外的设备目录/opt:存放第三方软件目录/run:放置系统开启启动后的pid文件/var/run,新版本/run 可以用内存仿真,效率高/sbin:存放root 执行的二进制可执行文件/srv:service 服务启动后需要的数据存放目录/tmp:程序运行临时生成的数据存放目录/usr:普通用户的数据目录 /usr/bin:普通用户执行的命令 /usr/lib:函数库文件 /usr/local:源码安装的目录 /usr/sbin:系统指令,服务执行的指令 /usr/share:帮助文档和共享文档 /usr/games:游戏 /usr/include:header 文件源码安装需要的一些文件数据 /usr/libexec:可执行的脚本 /usr/lib64:64位函数库 /usr/src:源码存放的目录/var:针对一般变动的文件,包括cache,log runfile、lockfile /var/cache:程序本身运行过程中产生的一些临时文件 /var/lib:程序本身执行所需要的文件 /var/lock:程序运行时产生的锁定文件 /var/log:系统日志目录 /var/mail:用户邮箱目录 /var/run:程序运行后产生pid文件 /var/spool 存放队列数据等待其它程序使用 /var/spool/cron /var/spool/mail

注意:在这里我们所介绍的目录是在linux系统中常见的目录,不同厂商的linux有所差异,有的版本中会出现有额外作用的目录,这个就需要具体问题具体对待了。

FHS 可选目录/home: 默认系统用户家目录/root:管理员家目录/local+found:ext系列用来存放系统错误时生成的数据,xfs 文件系统不存在这个目录/proc:虚拟文件系统内存映射目录/sys :虚拟文件系统,用来记录内核与系统信息相关的所有的目录都起源于:/ 根目录每个目录中都有两个隐藏目录:. : 代表着当前目录..: 代表着上一层目录绝对路径与相对路径absolute 绝对路径:由根/目录开始写起的文件或目录 例如: /etc/passwdrelative 相对路径:从当前路径开始写的 例如:./passwd目录和设备节点

我们已经知道目录是用来存放文件的,不同的目录存放的文件作用不同,那么我们来看下设备节点是什么? 一个设备节点其实就是一个文件,Linux中称为设备文件。有一点必要说明的是,在Linux中,所有的设备访问都是通过文件的方式,一般的数据文件程序普通文件,设备节点称为设备文件,而这些设备节点,或者说是设备文件都统一存放在/dev目录下,简单来说,设备节点是一种特殊的文件,只不过这个文件存放的不是一般的数据,而是和你计算机中的设备相关联的。

隐藏的当前路径和上层路径:. or ..在linux系统中,使用ls -a命令时,你会发现每一个目录下都包含两个目录,这两个目录就是.和…,其中".“表示是的是当前目录,也就是你使用pwd所查看到的路径,而”…"表示的上一级目录,也就是父目录。绝对路径和相对路径

linux的路径书写方式有两种:

绝对路径:路径的写法一定是由根目录 / 写起的,以根目录为起点;相对路径:路径的写法不是由根目录 / 写起的,不以根目录为起点。

例如,你知道你的朋友在四楼,而你在五楼,如果你的朋友询问你的位置,你就有两种回答方式:

绝对路径的方式就是你告诉对方你在XX小区的xx号楼的五楼,相对路径的方式就是你告诉对方你在对方五楼。

至于优缺点方面没有绝对的答案,不同情况使用不同的路径罢了,两种路径的出现只是因为参照物不同而已。

ls 查看目录内容

参数

说明

-a, --all

不隐藏任何以. 开始的项目

-A, --almost-all

列出除. 及.. 以外的任何项目

-b, --escape

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

-B, --ignore-backups

不显示备份文件

-c

按时间排序类似-lt

-C

按列列出条目

-d, --directory

列出目录本身,而不是其内容

-f

不排序,启用-aU,禁用-ls--颜色

-g

只显示数组与-l一起使用否则看不到效果

-G, --no-group

不显示数组与-l一起使用否则看不到效果

-h, --human-readable

显示每个文件或目录的大小(以更友好的方式显示)

-H, --dereference-command-line

遵循命令行上列出的符号链接

-i, --inode

打印每个文件的inode 节点号

-I, --ignore=PATTERN

不列出与shell模式匹配的隐含条目(忽略指定的文件或目录)

-k, --kibibytes

磁盘使用默认为1024字节与-s一起使用

-l

使用较长的格式列出信息

-L, --dereference

当显示符号链接的文件信息时,显示符号链接所指示的对象而并非符号链接本身的信息

-m

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

-n, --numeric-uid-gid

以数字形式列出属主、属组信息与-l参数类似

-N, --literal

不带引号的文字打印条目名称

-o

类似-l,但不列出组信息

-r, --reverse

逆序排列

-R, --recursive

递归显示子目录

-s, --size

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

-S

按文件大小排序,最大优先

--sort=WORD

按文件后缀名的首字母进行排序

--time=WORD

-u、atime、use ;-c、ctime、status;time 按时间排序

-x

按行而不是按列列出条目

-X

按条目扩展名按字母顺序排序

-Z, --context

打印每个文件的任何安全上下文

-1

每行列出一个文件。用-q或-b避免“\n”

-t

按时间最新排序

# -A 不列出.. and .[root@ym test]# ls -a.   123       2test.sh  abc.zip  passwd  test1    y..  1test.sh  abc       Bigfile  test    test.sh[root@ym test]# ls -A123  1test.sh  2test.sh  abc  abc.zip  Bigfile  passwd  test  test1  test.sh # -B 不显示备份文件[root@ym test]# ls123       2test.sh  ab~  abc.zip  passwd  test1    y1test.sh  a_b       abc  Bigfile  test    test.sh[root@ym test]# ls -B123       2test.sh  abc      Bigfile  test   test.sh1test.sh  a_b       abc.zip  passwd   test1  y# -C 按文件目录的时间书序显示[root@ym test]# ls -cab~  abc  test1  test     abc.zip  passwd    1test.sha_b  123  y      Bigfile  test.sh  2test.sh# -C 按列输出[root@ym test]# ls -C123       2test.sh  ab~  abc.zip  passwd  test1    y1test.sh  a_b       abc  Bigfile  test    test.sh# -d 只列出目录本身[root@ym test]# ls testa  b  c[root@ym test]# ls -d testtest# -g 只显示属组[root@ym test]# ls -l abc-rw-r--r-- 1 bin bin 0 7月   9 18:39 abc[root@ym test]# ls -g总用量 10272lrwxrwxrwx 1 root        3 7月   9 18:47 123 -> abc-rw-r--r-- 1 root      122 7月   4 00:11 1test.sh-rw-r--r-- 1 root      203 7月   4 00:17 2test.sh-rw-r--r-- 1 root        0 7月   9 19:03 a_b-rw-r--r-- 1 root        0 7月   9 19:03 ab~-rw-r--r-- 1 bin         0 7月   9 18:39 abc-rw-r--r-- 1 root    14806 7月   8 21:09 abc.zip-rw-r--r-- 1 root 10485760 7月   8 19:40 Bigfile-rw-r--r-- 1 root     2660 7月   8 18:55 passwddrwxr-xr-x 5 root       33 7月   9 10:30 testdrwxr-xr-x 2 root       95 7月   9 18:22 test1-rw-r--r-- 1 root       37 7月   8 18:59 test.shdrwxr-xr-x 2 root        6 7月   9 10:33 y# -G 不显示属组[root@ym test]# ls -l abc-rw-r--r-- 1 bin bin 0 7月   9 18:39 abc[root@ym test]# ls -g总用量 10272lrwxrwxrwx 1 root        3 7月   9 18:47 123 -> abc-rw-r--r-- 1 root      122 7月   4 00:11 1test.sh-rw-r--r-- 1 root      203 7月   4 00:17 2test.sh-rw-r--r-- 1 root        0 7月   9 19:03 a_b-rw-r--r-- 1 root        0 7月   9 19:03 ab~-rw-r--r-- 1 bin         0 7月   9 18:39 abc-rw-r--r-- 1 root    14806 7月   8 21:09 abc.zip-rw-r--r-- 1 root 10485760 7月   8 19:40 Bigfile-rw-r--r-- 1 root     2660 7月   8 18:55 passwddrwxr-xr-x 5 root       33 7月   9 10:30 testdrwxr-xr-x 2 root       95 7月   9 18:22 test1-rw-r--r-- 1 root       37 7月   8 18:59 test.shdrwxr-xr-x 2 root        6 7月   9 10:33 y# -h 显示文件目录的大小[root@ym test]# ls -lh总用量 11Mlrwxrwxrwx 1 root root    3 7月   9 18:47 123 -> abc-rw-r--r-- 1 root root  122 7月   4 00:11 1test.sh-rw-r--r-- 1 root root  203 7月   4 00:17 2test.sh-rw-r--r-- 1 root root    0 7月   9 19:03 a_b-rw-r--r-- 1 root root    0 7月   9 19:03 ab~-rw-r--r-- 1 root bin     0 7月   9 18:39 abc-rw-r--r-- 1 root root  15K 7月   8 21:09 abc.zip-rw-r--r-- 1 root root  10M 7月   8 19:40 Bigfile-rw-r--r-- 1 root root 2.6K 7月   8 18:55 passwddrwxr-xr-x 5 root root   33 7月   9 10:30 testdrwxr-xr-x 2 root root   95 7月   9 18:22 test1-rw-r--r-- 1 root root   37 7月   8 18:59 test.shdrwxr-xr-x 2 root root    6 7月   9 10:33 y# -i 显示inode 节点号[root@ym test]# ls -i 1503681 123        2386357 a_b   1577010 abc.zip   1503687 test      2386333 y 1576996 1test.sh   2386356 ab~   1576994 Bigfile  18837279 test1 1576981 2test.sh   2386353 abc   1576995 passwd    1576985 test.sh# -I 忽略指定的文件或目录[root@ym test]# ls -I test123       2test.sh  ab~  abc.zip  passwd  test.sh1test.sh  a_b       abc  Bigfile  test1   y# -m 所有项目以逗号分隔,并填满整行行宽[root@ym test]# ls -m 123, 1test.sh, 2test.sh, a_b, ab~, abc, abc.zip, Bigfile, passwd, test, test1,test.sh, y# -k 以字节的方式显示大小[root@ym test]# ls -lk总用量 10272lrwxrwxrwx 1 root root        3 7月   9 18:47 123 -> abc-rw-r--r-- 1 root root      122 7月   4 00:11 1test.sh-rw-r--r-- 1 root root      203 7月   4 00:17 2test.sh-rw-r--r-- 1 root root        0 7月   9 19:03 a_b-rw-r--r-- 1 root root        0 7月   9 19:03 ab~-rw-r--r-- 1 root bin         0 7月   9 18:39 abc-rw-r--r-- 1 root root    14806 7月   8 21:09 abc.zip-rw-r--r-- 1 root root 10485760 7月   8 19:40 Bigfile-rw-r--r-- 1 root root     2660 7月   8 18:55 passwddrwxr-xr-x 5 root root       33 7月   9 10:30 testdrwxr-xr-x 2 root root       95 7月   9 18:22 test1-rw-r--r-- 1 root root       37 7月   8 18:59 test.shdrwxr-xr-x 2 root root        6 7月   9 10:33 y# -n 以数字方式显示属组属主[root@ym test]# ls -n总用量 10272lrwxrwxrwx 1 0 0        3 7月   9 18:47 123 -> abc-rw-r--r-- 1 0 0      122 7月   4 00:11 1test.sh-rw-r--r-- 1 0 0      203 7月   4 00:17 2test.sh-rw-r--r-- 1 0 0        0 7月   9 19:03 a_b-rw-r--r-- 1 0 0        0 7月   9 19:03 ab~-rw-r--r-- 1 0 1        0 7月   9 18:39 abc-rw-r--r-- 1 0 0    14806 7月   8 21:09 abc.zip-rw-r--r-- 1 0 0 10485760 7月   8 19:40 Bigfile-rw-r--r-- 1 0 0     2660 7月   8 18:55 passwddrwxr-xr-x 5 0 0       33 7月   9 10:30 testdrwxr-xr-x 2 0 0       95 7月   9 18:22 test1-rw-r--r-- 1 0 0       37 7月   8 18:59 test.shdrwxr-xr-x 2 0 0        6 7月   9 10:33 y# -o 只显示属主[root@ym test]# ls -o总用量 10272lrwxrwxrwx 1 root        3 7月   9 18:47 123 -> abc-rw-r--r-- 1 root      122 7月   4 00:11 1test.sh-rw-r--r-- 1 root      203 7月   4 00:17 2test.sh-rw-r--r-- 1 root        0 7月   9 19:03 a_b-rw-r--r-- 1 root        0 7月   9 19:03 ab~-rw-r--r-- 1 bin         0 7月   9 18:39 abc-rw-r--r-- 1 root    14806 7月   8 21:09 abc.zip-rw-r--r-- 1 root 10485760 7月   8 19:40 Bigfile-rw-r--r-- 1 root     2660 7月   8 18:55 passwddrwxr-xr-x 5 root       33 7月   9 10:30 testdrwxr-xr-x 2 root       95 7月   9 18:22 test1-rw-r--r-- 1 root       37 7月   8 18:59 test.shdrwxr-xr-x 2 root        6 7月   9 10:33 y# -r 逆向排序以字母[root@ym test]# ls -ry        test1  passwd   abc.zip  ab~  2test.sh  123test.sh  test   Bigfile  abc      a_b  1test.sh# -R 递归显示子目录[root@ym test]# ls -R.:123       2test.sh  ab~  abc.zip  passwd  test1    y1test.sh  a_b       abc  Bigfile  test    test.sh./test:a  b  c./test/a:x  y./test/a/x:./test/a/y:./test/b:x  y./test/b/x:./test/b/y:./test/c:x  y./test/c/x:./test/c/y:./test1:file.a  file.b  file.bz2  file.c  file.txt  file.za./y:# --time=WORD 以时间作为排序[root@ym test]# ls --time=atimey    ab~  abc    test     passwd   Bigfile   2test.sha_b  123  test1  abc.zip  test.sh  1test.sh# -s,--size 以块数形式显示每个文件分配的尺寸[root@ym test]# ls -s总用量 10272    0 123           0 a_b     16 abc.zip      0 test         0 y    4 1test.sh      0 ab~  10240 Bigfile      0 test1    4 2test.sh      0 abc      4 passwd       4 test.sh# -S 按文件大小排序,最大优先[root@ym test]# ls -SBigfile  passwd    1test.sh  test.sh  y    a_b  abcabc.zip  2test.sh  test1     test     123  ab~[root@ym test]# ls -lS总用量 10280-rw-r--r-- 1 root root 10485760 7月   8 19:40 Bigfile-rwr--r-- 1 root root    14806 7月   8 21:09 abc.zip-rw-r--r-- 1 root root     2660 7月   9 09:16 file.txt-rw-r--r-- 1 root root     2660 7月   8 18:55 passwd-rw-r--r-- 1 root root     1070 7月   8 19:30 file.bz2-rw-r--r-- 1 root root      203 7月   4 00:17 2test.sh-rw-r--r-- 1 root root      122 7月   4 00:11 1test.sh-rw-r--r-- 1 root root       37 7月   8 18:59 test.shdrwxr-xr-x 5 root root       33 7月   9 10:30 testdrwxr-xr-x 2 root root        6 7月   9 10:33 y# --sort=WORD 按文件后缀名的首字母进行排序[root@ym test1]# ls --sort=extensionfile.a  file.b  file.bz2  file.c  file.txt  file.za# -x -X 按行显示 按目录拓展名输出[root@ym test]# ls -x123      1test.sh  2test.sh  a_b  ab~  abc  abc.zip  Bigfile  passwd  test  test1test.sh  y[root@ym test]# ls -X123  ab~  Bigfile  test   y         2test.sh  abc.zipa_b  abc  passwd   test1  1test.sh  test.sh# -1 每行显示一个文件或目录[root@ym test]# ls -11231test.sh2test.sha_bab~abcabc.zipBigfilepasswdtesttest1test.shy# -t 按时间最新排序[root@ym test]# ls -ta_b  123  test1  test     Bigfile  passwd    1test.shab~  abc  y      abc.zip  test.sh  2test.sh
cd 切换目录

参数

说明

~

代表回到家目录,cd ~ 直接回到家目录

-

代表回到上次所在的工作目录,cd -

..

代表回到当前目录的上一层目录

/path/

代表切换到指定目录: cd /usr/local

# cd 命令用来切换工作目录[root@ym ~]# cd /etc[root@ym etc]## cd - 回到前一个工作目录[root@ym etc]# cd -/root# cd ~ 回到用户家目录[root@ym ~]# cd /tmp/ym[root@ym ym]# cd ~ # ~符号代表家目录[root@ym ~]## .. 回到上层目录[root@ym ym]# cd ..[root@ym tmp]# # cd ./ 当前目录[root@ym tmp]# cd .[root@ym tmp]# # 注意:.. 和 . 为什么可以回到上层或当前,原因是在一个目录中,默认存在这个量个 符号,分别代表 .. 父目录 . 代表 当前的自己目录[root@ym ym]# ls -a # 可以在任意目录中查看这两个符号.  ..  test
cp 复制

参数

说明

-r

-R 递归复制目录下内容到指定位置

-i

复制的文件已经存在是,提示用户是否覆盖

-p

保留文件元数据信息

-f

强制覆盖已经存在的目录或文件

-a

保留原文属性(-p 功能强)

-t

将所有参数指定的源文件/目录复制至目标目录

-n

-u

只在源文件比目标文件新,或目标文件不存在时才进行复制

-T

将目标目录视作普通文件

mkdir 创建目录

参数

说明

-m

创建目录时设置权限

-p

创建多级目录

-v

显示创建过程

# -m 创建目录并赋予指定权限[root@ym test]# mkdir -m a=rw x[root@ym test]# ls -ld xdrw-rw-rw- 2 root root 6 7月   9 10:31 x# -p 一次创建多级目录[root@ym test]# mkdir -p ./test/{a,b,c}/{x,y}[root@ym test]# tree testtest├── a│   ├── x│   └── y├── b│   ├── x│   └── y└── c    ├── x    └── y# -v 显示创建过程信息[root@ym test]# mkdir -v  ymkdir: 已创建目录 'y'
mv 移动重命名

参数

说明

-b

当目标文件或目录存在时,在执行覆盖前,会为其创建一个备份

-f

如果指定移动的源目录或文件与目标的目录或文件同名,不会询问,直接覆盖旧文件

-i

如果指定移动的源目录或文件与目标的目录或文件同名,则会先询问是否覆盖旧文件,输入 y 表示直接覆盖,输入 n 表示取消该操作

-n

不要覆盖任何已存在的文件或目录

-u

当源文件比目标文件新或者目标文件不存在时,才执行移动操作

# 移动文件目录[root@ym test]# mv file.txt /tmp/ym[root@ym test]# ls /tmp/yum/ls: 无法访问'/tmp/yum/': 没有那个文件或目录[root@ym test]# ls /tmp/ym/file.txt  test# 如果移动的文件或目录同名是,就修改名字(重命名)[root@ym test]# mv file file.txt[root@ym test]# ls filels: 无法访问'file': 没有那个文件或目录[root@ym test]# ls file.txtfile.txt# -b 移动时创建备份文件[root@ym test]# mv -b file.txt /tmp/ymmv:是否覆盖'/tmp/ym/file.txt'? y[root@ym test]# ls /tmp/ym/file.txt  file.txt~  test #备份文件件:file.txt~# -f 文件存在就覆盖,不提示[root@ym test]# ls /tmp/ymfile.txt  test[root@ym test]# mv -f file.txt /tmp/ym/[root@ym test]# ls /tmp/ymfile.txt  test# -i 文件存在就提示是否覆盖,默认mv命令使用的识别名:alias mv='mv -i'[root@ym test]# mv file.txt /tmp/ymmv:是否覆盖'/tmp/ym/file.txt'? y# -n 文件存在就不移动[root@ym test]# mv -n file.txt /tmp/ym[root@ym test]# ls1test.sh  2test.sh  abc.zip  Bigfile  file.bz2  file.txt  passwd  test.sh# -u 移动文件比已存在的文件新时提示覆盖[root@ym test]# mv -u file.txt /tmp/ymmv:是否覆盖'/tmp/ym/file.txt'?
pwd 显示当前工作目录

参数

说明

-L

打印 $PWD 变量的值,如果它包含了当前的工作目录(默认参数)

-P

打印当前的物理路径,不带有任何的符号链接

# 显示当前工作路径 [root@ym ym]# pwd/tmp/ym# -P 显示物理路径,不显示符号链接目录[root@ym system]# cd /etc/init.d/[root@ym init.d]# pwd -P/etc/rc.d/init.d
rename 批量修名称
rename [选项] <表达式> <替换> <文件>...[root@ym test]# ls123       a_b  abc.zip  file   paa  pad  pag  paj  pam  pap     test     y1test.sh  ab~  acd      file1  pab  pae  pah  pak  pan  passwd  test12test.sh  abc  Bigfile  file2  pac  paf  pai  pal  pao  pswd    test.sh[root@ym test]# rename pa pa00 pa?[root@ym test]# ls123       ab~      Bigfile  pa00a  pa00e  pa00i  pa00m  passwd  test.sh1test.sh  abc      file     pa00b  pa00f  pa00j  pa00n  pswd    y2test.sh  abc.zip  file1    pa00c  pa00g  pa00k  pa00o  testa_b       acd      file2    pa00d  pa00h  pa00l  pa00p  test1
rm 删除

参数

说明

-r

递归删除目录项的内容并删除指定目录

-i

删除时提醒

-f

强制删除,不提示

-d

删除空目录文件

# -r 删除指定目录所有数据,不提示(有提示原因是有别名alias)[root@ym test]# rm -r /tmp/ym# -f 删除文件不提示[root@ym test]# rm f /tmp/ym/file.txt# -rf 递归删除所有数据不提示[root@ym test]# rm -rf /tmp/ym# -d 删除空目录[root@ym test]# rm -d /tmp/ym # 无法删除因为ym目录有数据
rmdir 删除空目录
[root@ym test]# rmdir aa # aa 一定要是空目录才能删掉
touch 创建文件

参数

说明

atime

文件访问时间(使用cat 命令查看文件只有atime 时间改变)

mtime

文件的内容修改时间(使用echo 追加内容只有mtime、ctime 时间会改变)

ctime

文件元数据(权限、大小:修改权限只有ctime 时间变)

-a

只修改文件的 atime(文件访问)、ctime (元数据)

-c

如果文件存在就修改时间,不存在就忽略(atime、mtime、ctime都会改变)

-m

修改文件时间戳,(只有ctime、mtime 时间戳会修改)

-r

将指定文件修改为参考文件(atime、mtime、ctime都修改)

-t

把指定文件的时间戳,修改为指定时间

touch -t 2022791010.30 test#touch:关键命令#-t:选项#2017:世纪和年份#10:月份#16:天#10:小时#10:分钟#.30:秒数#test 文件
tree 查看目录树

参数

说明

-L

指定显示目录层级

-d

只显示目录

# -L 显示指定数量的目录层级[root@ym test]# tree -L 3 /usr/local/usr/local├── bin├── etc├── games├── include├── lib├── lib64│   └── bpf├── libexec├── sbin├── share│   ├── applications# -d 只显示目录[root@ym test]# tree -d /usr/local/usr/local├── bin├── etc├── games├── include├── lib├── lib64│   └── bpf├── libexec├── sbin├── share│   ├── applications
basename 提取文件名

参数

说明

-a

可以同时出入多个参数

-s

删除指定的后缀

-z

不换行输出

# 不加参数是打印文件名字[root@ym test]# basename /usr/bin/sortsort# [root@ym test]# basename include/stdio.h .hstdio# -s 删除尾随后缀[root@ym test]# basename -s .h include/stdio.hstdio# -a 指定多个参数args[root@ym test]# basename -a any/str1 any/str2str1str2
dirname 提取目录
# 只获取目录名[root@ym test]# dirname /usr/bin//usr[root@ym test]# dirname dir1/str dir2/strdir1dir2[root@ym test]# dirname stdio.h.# -z 不换行输出root@ym test]# dirname -z  dir1/str dir2/strdir1dir2[root@ym test]#
lsattr 查看特殊权限

参数

说明

-R

递归列出目录及其内容的属性

-V

显示程序版本

-a

列出目录中的所有文件,包括以开头的文件

-d

与其他文件一样列出目录,而不是列出其它目录

-l

使用长名称而不是单个字符打印选项

-p

列出文件的项目编号

-v

列出文件的版本号/代号

# -R 递归列出目录及其内容的属性[root@ym test]# lsattr -R test-------------------- test/atest/a:-------------------- test/a/x# -V显示程序版本[root@ym test]# lsattr -V lsattr 1.45.6 (20-Mar-2020)-------------------- ./1test.sh# -a列出目录中的所有文件,包括以开头的文件[root@ym test]# lsattr -a test-------------------- test/.-------------------- test/..-------------------- test/a-------------------- test/b-------------------- test/c# -d与其他文件一样列出目录,而不是列出其它目录[root@ym test]# lsattr -d  test-------------------- test# -l使用长名称而不是单个字符打印选项[root@ym test]# lsattr -l testtest/a                       ---test/b                       ---test/c                       ---# -p列出文件的项目编号[root@ym test]# lsattr -p     0 -------------------- ./1test.sh    0 -------------------- ./2test.sh    0 -------------------- ./passwd    # -v列出文件的版本号/代号[root@ym test]# lsattr -v 523250067  -------------------- ./1test.sh3621861802 -------------------- ./2test.sh2593114332 -------------------- ./passwd
file 查看键类型

参数

说明

-b

列出辨识结果时,不显示文件名称

-c

详细显示指令执行过程,便于排错或分析程序执行的情形

-L

直接显示符号连接所指向的文件的类别

-z

尝试去解读压缩文件的内容

[root@ym test]# file passwdpasswd: ASCII text# -b列出辨识结果时,不显示文件名称[root@ym test]# file -b passwdASCII text# -c详细显示指令执行过程,便于排错或分析程序执行的情形[root@ym test]# file -c passwdcont	offset	type	opcode	mask	value	desc# -L直接显示符号连接所指向的文件的类别[root@ym test]# file -L passwdpasswd: ASCII text# -z尝试去解读压缩文件的内容[root@ym test]# file -z abc.zipabc.zip: gzip ERROR: Stdin has more than one entry--rest ignored (Zip archive data, at least v1.0 to extract)
stat 查看文件状态
[root@ym test]# stat abc  文件:abc  大小:0         	块:0          IO 块:4096   普通空文件设备:fd00h/64768d	Inode:2386353     硬链接:1权限:(0644/-rw-r--r--)  Uid:(    1/     bin)   Gid:(    1/     bin)最近访问:2022-07-10 00:17:40.037711138 +0800最近更改:2022-07-09 18:39:16.254660981 +0800最近改动:2022-07-09 23:43:58.418804957 +0800创建时间:2022-07-09 18:39:16.254660981 +0800
type 查看命令是内部、外部、别名

参数

说明

-t

输出“file”、“alias”或者“builtin”,分别表示给定的指令为“外部指令”、“命令别名”或者“内部指令”

-p

如果给出的指令为外部指令,则显示其绝对路径

-a

在环境变量“PATH”指定的路径中,显示给定指令的信息,包括命令别名

[root@localhost ~]# type -a grepgrep is /bin/grep[root@localhost ~]# type lsls is aliased to `ls --color=tty'[root@ym test]# type -t cdbuiltin[root@ym test]# type cdcd 是 shell 内建[root@ym test]# type -p mkdir/usr/bin/mkdir
cat 打印文件内容

参数

说明

-A, --show-all

查看文件所有

-b, --number-nonblank

非空输出行数,覆盖-n

-e

等级与 -vE显示除tab键的其他符号

-E, --show-ends

显示每行的结尾符$

-n, --number

显示文件每行

-s, --squeeze-blank

显示空行行号

-t

-vT 显示特殊符号

-T, --show-tabs

将tab键字符显示为^I

-u

取消了

-v, --show-nonprinting

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

# 不加任何参数显示文件所用内容[root@ym test]# cat passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin.......# -A 显示文件中所有内容包括隐藏的符号[root@ym test]# cat -A passwdroot:x:0:0:root:/root:/bin/bash$bin:x:1:1:bin:/bin:/sbin/nologin$# -b 空行没有行号[root@ym test]# cat -b passwd     1  root:x:0:0:root:/root:/bin/bash     2  bin:x:1:1:bin:/bin:/sbin/nologin     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin# -e 显示tab键的符号[root@ym test]# vim passwd[root@ym test]# cat -e passwdroot:x:0:0:root :/root  :/bin/bash  $bin:x:1:1:bin:/bin:   /sbin/nologin$# -E 显示每行的结尾符[root@ym test]# cat -E passwdroot:x:0:0:root :/root  :/bin/bash  $bin:x:1:1:bin:/bin:   /sbin/nologin$# -n 空行也显示行号[root@ym test]# cat -n passwd     1  root:x:0:0:root :/root  :/bin/bash       2       3  bin:x:1:1:bin:/bin:   /sbin/nologin     # -n# -t 显示tab键为 ^I[root@ym test]# cat -t passwdroot:x:0:0:root^I:/root^I:/bin/bash^Ibin:x:1:1:bin:/bin:   /sbin/nologi# -T 将tab键字符显示为^I[root@ym test]# cat -T passwd | head -5root:x:0:0:root^I:/root^I:/bin/bash^Ibin:x:1:1:bin:/bin:   /sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin# -v 不显示特殊符号[root@ym test]# cat -v passwd root:x:0:0:root :/root  :/bin/bash  bin:x:1:1:bin:/bin:   /sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin
tac 反向显示文件内容

参数

说明

-b, --before

在行前而非行尾添加分隔标志

-r, --regex

将分隔标志视作正则表达式来解析

-s, --separator=STRING

指定字符串代替换行符作为行分隔标志

[root@ym test]# tac filefedcba# -s 指定字符串代替换行符作为行分隔标志b[root@ym test]# tac -s "c" filedefabc[root@ym test]# # a b c d e f  # a b d e f# -b 在行前而非行尾添加分隔标志[root@ym test]# seq 1 3 | tac -b321# -r 将分隔标志视作正则表达式来解析[root@ym test]# tac -r -s "[^x]" filefcbax  ohtypxx dollexxh cxxxohtyp bllehsX a[root@ym test]# 
less 阅读过滤文件

参数

说明

/

向下搜索“字符串”的功能

?

向上搜索“字符串”的功能

n

重复前一个搜索(与 / 或 ? 有关)

N

反向重复前一个搜索(与 / 或 ? 有关)

b

向后翻一页

d

向后翻半页

h

显示帮助界面

q

退出less 命令

u

向前滚动半页

y

向前滚动一行

space 空格键

滚动一行

enter 回车键

滚动一页

more 阅读过滤文件

参数

说明

-d

显示帮助提示

-f

计算逻辑行数,而非屏幕行数

-l

屏蔽换页(form feed)后的暂停

-c

从顶部清屏,然后显示

-p

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

-s

将多行空行压缩为一行

-u

屏蔽下划线

-n

每屏的行数

+n

从指定行开始显示文件

+/pattern

从匹配搜索字符串的位置开始显示文件

# -d 显示帮助提示[root@ym test]# more -d /var/log/messages--更多--(8%)[按空格键继续,“q”键退出。]# -f 计算逻辑行数,而非屏幕行数[root@ym test]# more -f /var/log/messages--更多--(14%)# -n 显示指定的每屏的行数[root@ym test]# more -3 pswd     1  root:x:0:0:root :/root  :/bin/bash       2       3  bin:x:1:1:bin:/bin:   /sbin/nologin--更多--(3%)# +n 从指定行开始显示文件[root@ym test]# more +45 pswd    45  tcpdump:x:72:72::/:/sbin/nologin    46  sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin    47  centos:x:1000:1000:centos:/home/centos:/bin/bash    48  mysql:x:27:27:MySQL Server:/var/lib/mysql:/sbin/nologin    49  mongod:x:974:974:mongod:/var/lib/mongo:/bin/false# +/pattern 从匹配搜索字符串的位置开始显示文件[root@ym test]# more +/49 pswd...跳过    47  centos:x:1000:1000:centos:/home/centos:/bin/bash    48  mysql:x:27:27:MySQL Server:/var/lib/mysql:/sbin/nologin    49  mongod:x:974:974:mongod:/var/lib/mongo:/bin/false
head 默认显示前十行内容

参数

说明

-c, --bytes=[-]NUM

以字节的形式显示指定个数的字符

-n, --lines=[-]NUM

显示指定的行

-q, --quiet, --silent

显示多个文件的行不显示文件名字

-v, --verbose

总是显示包含给定文件名的文件头

-z, --zero-terminated

以 NUL 字符而非换行符作为行尾分隔符

# -c 以字节的形式显示指定个数的字符[root@ym test]# head -c 5 passwdroot:# -n 显示指定的行[root@ym test]# head -c 5 passwdroot:[root@ym test]# head -n  3 passwdroot:x:0:0:root :/root  :/bin/bash  bin:x:1:1:bin:/bin:   /sbin/nologin# -q 显示多个文件的行不显示文件的名字[root@ym test]# head -q file passwd        python shella Xshellb pythoxxxc xxhellodxx pytho x abcfroot:x:0:0:root :/root  :/bin/bash  bin:x:1:1:bin:/bin:   /sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/sync# -v 显示文件的名字[root@ym test]# head -v passwd==> passwd <==root:x:0:0:root :/root  :/bin/bash
tail 默认显示后10行内容

参数

说明

-c, --bytes=[+]NUM

显示末行的指定字符数

-f, --follow[={namedescriptor}]

显示文件最新追加的内容

-n, --lines=[+]NUM

输出文件的尾部N(N位数字)行内容

-q, --quiet, --silent

多个文件作为参数从不输出给出文件名的标题

-s, --sleep-interval=N

与“-f”选项连用,指定监视文件变化时间隔的秒数

-z, --zero-terminated

以 NUL 字符而非换行符作为行尾分隔符

# -c[root@ym test]# tail -c 10 passwdbin/false# -n 显示倒数的指定行数[root@ym test]# tail -n 10 passwdflatpak:x:977:977:User for flatpak system helper:/:/sbin/nologincolord:x:976:976:User for colord:/var/lib/colord:/sbin/nologinrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologingdm:x:42:42::/var/lib/gdm:/sbin/nologingnome-initial-setup:x:975:975::/run/gnome-initial-setup/:/sbin/nologintcpdump:x:72:72::/:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologincentos:x:1000:1000:centos:/home/centos:/bin/bashmysql:x:27:27:MySQL Server:/var/lib/mysql:/sbin/nologinmongod:x:974:974:mongod:/var/lib/mongo:/bin/false# -q 再多个文件作为参数是不显示文件名[root@ym test]# tail -q file passwda Xshellb pythoxxxc xxhellodxx pytho x abcfflatpak:x:977:977:User for flatpak system helper:/:/sbin/nologincolord:x:976:976:User for colord:/var/lib/colord:/sbin/nologinrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin# -s 与“-f”选项连用,指定监视文件变化时间隔的秒数[root@ym test]# tail -f -s 10 /var/log/messagesJul 10 23:54:33 ym dnf[53790]: Rocky Linux 8 - Extras                           24 kB/s | 3.5 kB     00:00Jul 10 23:54:33 ym dnf[53790]: MongoDB Repository                               14 kB/s | 2.5 kB     00:00Jul 10 23:54:34 ym dnf[53790]: 元数据缓存已建立。Jul 10 23:54:34 ym systemd[1]: dnf-makecache.service: Succeeded.Jul 10 23:54:34 ym systemd[1]: Started dnf makecache.Jul 11 00:00:32 ym systemd[1]: Starting update of the root trust anchor for DNSSEC validation in unbound...Jul 11 00:00:32 ym systemd[1]: Started Update a database for mlocate.Jul 11 00:00:32 ym systemd[1]: mlocate-updatedb.service: Succeeded.Jul 11 00:01:01 ym systemd[1]: unbound-anchor.service: Succeeded.Jul 11 00:01:01 ym systemd[1]: Started update of the root trust anchor for DNSSEC validation in unbound.# -n[root@ym test]# tail -n 3 passwdcentos:x:1000:1000:centos:/home/centos:/bin/bashmysql:x:27:27:MySQL Server:/var/lib/mysql:/sbin/nologinmongod:x:974:974:mongod:/var/lib/mongo:/bin/false
cut 切割文件

参数

说明

-b

以字节为单位显示指定位置的字节 注意:中文才有区别

-c

以字符为单位显示

-d

自定义分隔符,默认为制表符

-f

与-d一起使用,指定显示哪个区域

-n

取消分割多字节字符,与 -b 一起使用 按字符处理

-s

与-d一起使用,如果加上-s表示只显示有分隔符的行

N

显示第n个字符

N-

显示从第n个字符到最后

N-M

显示从n到m的字符

-M

显示从开头到m字符

# -b 以字节为单位显示指定位置的字节[root@ym test]# echo "hello python" | cut -b 1h[root@ym test]# echo "hello python" | cut -b 1-5[root@ym test]# echo "你好 python" |cut -b 1 # 结果为空# -c 以字符为单位显示[root@ym test]# echo "hello python" | cut -c 1h[root@ym test]# echo "hello python" | cut -c 1-5hello# -d 自定义分隔符,默认为制表符 与 -f 一起使用显示指定区域echo "hello python" | cut -d ' ' -f 2python[root@ym test]# # -n 取消分割多字节字符,与 -b 一起使用 按字符处理[root@ym test]# echo "你好 python" |cut -nb 1 你#  -sd 一起使用只显示有分割行[root@ym test]# cat pswdcentos x 1000 1000 centos /home/centos /bin/bashmysql x 27 27 MySQL Server /var/lib/mysql /sbin/nologinmongod:x:974:974:mongod:/var/lib/mongo:/bin/false:0[root@ym test]# cut -sd ':' -f 2-3 pswdx:974# N N- -N N-M 用法[root@ym test]# cut -sd ':' -f 2-3 pswdx:974[root@ym test]# cut -d ' ' -f 7- pswd/bin/bash/var/lib/mysql /sbin/nologinmongod:x:974:974:mongod:/var/lib/mongo:/bin/false:0[root@ym test]# cut -d ' ' -f 5-6 pswdcentos /home/centosMySQL Servermongod:x:974:974:mongod:/var/lib/mongo:/bin/false:0[root@ym test]# cut -d ' ' -f -5 pswdcentos x 1000 1000 centosmysql x 27 27 MySQLmongod:x:974:974:mongod:/var/lib/mongo:/bin/false:0
split 将文件拆分为多个部分

参数

说明

-<行数>

指定每多少行切成一个小文件

-b<字节>

指定每多少字节切成一个小文件

-C<字节>

与参数"-b"相似,但是在切 割时将尽量维持每行的完整性

[root@ym test]# split -2 passwd[root@ym test]# ls123       ab~      file    test1    xac  xag  xak  xao  xas  xaw1test.sh  abc      passwd  test.sh  xad  xah  xal  xap  xat  xax2test.sh  abc.zip  pswd    xaa      xae  xai  xam  xaq  xau  xaya_b       Bigfile  test    xab      xaf  xaj  xan  xar  xav  y[root@ym test]# split -b 10 passwd[root@ym test]# ls123       xae  xaw  xbo  xcg  xcy  xdq  xei  xfa  xfs  xgk  xhc  xhu  xim  xje  xjw1test.sh  xaf  xax  xbp  xch  xcz  xdr  xej  xfb  xft  xgl  xhd  xhv  xin  xjf  xjx2test.sh  xag  xay  xbq  xci  xda  xds  xek  xfc  xfu  xgm  xhe  xhw  xio  xjg  xjya_b       xah  xaz  xbr  xcj  xdb  xdt  xel  xfd  xfv  xgn  xhf  xhx  xip  xjh  xjzab~       xai  xba  xbs  xck  xdc  xdu[root@ym test]# split -C 100 passwd[root@ym test]# ls123       abc      pswd     xab  xag  xal  xaq  xav  xba  xbf  xbk1test.sh  abc.zip  test     xac  xah  xam  xar  xaw  xbb  xbg  xbl2test.sh  Bigfile  test1    xad  xai  xan  xas  xax  xbc  xbh  xbma_b       file     test.sh  xae  xaj  xao  xat  xay  xbd  xbi  yab~       passwd   xaa      xaf  xak  xap  xau  xaz  xbe  xbj[root@ym test]# split -C 200 passwd p # p指定切割的文件名字[root@ym test]# ls123       a_b  abc.zip  paa  pad  pag  paj  pam  pap     test     y1test.sh  ab~  Bigfile  pab  pae  pah  pak  pan  passwd  test12test.sh  abc  file     pac  paf  pai  pal  pao  pswd    test.sh
paste 合并文件行

参数

说明

-d

用指定的间隔字符取代跳格字符

-s或--serial

将列按行显示

[root@ym test]# paste file passwd        python shell    root:x:0:0:root :/root  :/bin/bash  a Xshell    b pythoxxx  bin:x:1:1:bin:/bin:   /sbin/nologinc xxhello   daemon:x:2:2:daemon:/sbin:/sbin/nologind   adm:x:3:4:adm:/var/adm:/sbin/nologinxx pytho x abc  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@ym test]# cat filehello pythonhello worldhello shellhello sqlhello nginx[root@ym test]# paste -s  filehello python    hello world hello shell hello sql   hello nginx

sort 对文本文件的行进行排序

参数

说明

-b

忽略每行前面开始出的空格字符

-c

检查文件是否已经按照顺序排序

-d

排序时,处理英文字母、数字及空格字符外,忽略其他的字符

-f

排序时,将小写字母视为大写字母

-n

依照数值的大小排序

-o<输出文件>

将排序后的结果存入指定的文件

-r

以相反的顺序来排序

-t<分隔字符>

指定排序时所用的栏位分隔字符

-k

指定排序的区域

uniq 忽略重复行

参数

说明

-c或--count

在每列旁边显示该行重复出现的次数

-d或--repeated

仅显示重复出现的行列

-f

比较时跳过前N 列

-i

比较前忽略大小写

-s<字符位置>或--skip-chars=<字符位置>

比较时跳过前N 个字符

-u或--unique

仅显示出一次的行列

-w<字符位置>或--check-chars=<字符位置>

对每行第N 个字符以后的内容不作对照

wc 打印每个文件的换行、字和字节计数

参数

说明

-c, --bytes

打印字节总数

-m, --chars

打印字符总数

-l, --lines

打印行总数

-w, --words

打印单词总数

[root@ym test]# wc file 6 12 76 file # 6 行 12 单词 76字节[root@ym test]# wc -m file66 file # 66 字符[root@ym test]# wc -l file6 file # 6 行
dos2unix 将windows 格式文件转换成unix格式
dos2unix file file2 # 将file文件内容格式转换为unix格式保存在file2中
diff 比较文件内容

注意:1,2 c 1,3 表示 左边1,2行 和 右边的1,3行不一样

"11,12d10"表示第一个文件比第二个文件多了第11和12行

"|"表示前后2个文件内容有不同

"<"表示后面文件比前面文件少了1行内容

">"表示后面文件比前面文件多了1行内容

[root@ym test]# diff /etc/passwd passwd1,2c1,3< root:x:0:0:root:/root:/bin/bash< bin:x:1:1:bin:/bin:/sbin/nologin---> root:x:0:0:root   :/root  :/bin/bash  > > bin:x:1:1:bin:/bin:   /sbin/nologin48c49< mongod:x:974:974:mongod:/var/lib/mongo:/bin/false---> mongod:x:974:974:mongod:/var/lib/mongo:/bin/false 0
rev 翻转每行
[root@ym test]# rev filenohtyp ollehdlrow ollehllehs ollehlqs ollehxnign olleh
grep or egrep 按条件过滤匹配

参数

说明

-i

忽略大小写

-v

根据匹配取反

-o

匹配一个完整的单词

-w

打印匹配到的行

-n

匹配m次后停止

-m

实现多个选项间的逻辑or 关系

-e

统计匹配到的行数

-c

统计匹配到的行数

-B

向前n行

-A

向后n行

-C

前后个n行

-E

使用扩展正则表达式

join 连接两个文件的行

root@ym test]# cat file1li 2008wang 2010liu 2020[root@ym test]# cat file2li 30wang 20liu 10[root@ym test]# join file1 file2li 2008 30wang 2010 20liu 2020 10
tr 替换删除字符
tr [OPTION]…SET1[SET2] 

参数

说明

-c, --complement

字符补集替换:用SET2替换SET1中没有包含的字符

-d, --delete

删除指定字符:删除SET1中指定的所有字符

-s, --squeeze-repeats

压缩重复字符:用SET1指定的字符来替换对应的重复字符

-t, --truncate-set1

替换字符:将SET1中字符用SET2对应位置的字符进行替换(缺省就是-t选项)

# -s 将重复的字符显示成一个[root@ym test]# echo $PATH | tr -s ":" "\n"/usr/local/sbin/usr/local/bin/usr/sbin/usr/bin/root/bin# -d 删除指令字符[root@ym test]# echo $PATH | tr -d ":"/usr/local/sbin/usr/local/bin/usr/sbin/usr/bin/root/bin# -c [root@ym test]# echo $PATH | tr -s ":" "\n"|tr -c 'bin' "\n"
vim 编辑文件

vim 三种工作模式

命令行模式

vim 默认处于命令行模式,此模式下可以使用上下左右或 k j h i 移动光标的位置,可以对文件内容进行复制、粘贴、替换、删除等操作

输入模式

在输入模式下,vim可以对文件进行写操作,可以按一下按键进入输入模式:

i、I、a、A、o、O 退出可以按 esc 回退到命令行模式

按键

说明

i

在当前光标除插入

I

在光标所在的行首插入

o

在光标的下一行插入空白行

O

在光标的上一行插入空白行

a

在光标的后一个字符插入

A

在光标的所在行的行位插入

末行模式

末行模式用于对文件进行保存,查找、替换、退出等操作

在命令行模式下按 :键进入末行模式,回退到命令行模式可以 按 esc

搜索替换

搜索替换

说明

/word

向光标之下寻找一个名称为 word 的字符串。例如要在档案内搜寻 vbird 这个字符串,就是输入 /vbird 即可! (常用)

?word

向光标之上寻找一个字符串名称为 word 的字符串。

n

这个 n 是英文按键。代表重复前一个搜寻的动作。举例来说, 如果刚刚我们执行 /vbird 去向下搜寻 vbird 这个字符串,则按下 n 后面,会向下继续搜寻下一个名称为 vbird 的字符串。如果是执行 ?vbird 的话,那么按下 n 则会向上继续搜寻名称为 vbird 的字符串!

N

这个 N 是英文按键。与 n 刚好相反,为『反向』进行前一个搜寻动作。 例如 /vbird 后,按下 N 他们表示『向上』搜寻 vbird 。

:n1,n2s/word1/word2/g

n1 与 n2 为数字。在第 n1 与 n2 行之间寻找 word1 这个字符串,并将该字符串取代为 word2 !举例来说,在 100 到 200 行之间搜寻 vbird 并取代为 VBIRD 则: 『:100,200s/vbird/VBIRD/g』。(常用)

:1,$s/word1/word2/g 或 :%s/word1/word2/g

从第一行到最后一行寻找 word1 字符串,并将该字符串取代为 word2 !(常用)

:1,$s/word1/word2/gc 或 :%s/word1/word2/gc

从第一行到最后一行寻找 word1 字符串,并将该字符串取代为 word2 !且在取代前显示提示字符给用户确认 (confirm) 是否需要取代!(常用)

删除、复制、粘贴

x, X

在每一行字当中,x 为向后删除一个字符 (相当于 [del] 按键), X 为向前删除一个字符(相当于 [backspace] 亦即是退格键) (常用)

nx

n 为数字,连续向后删除 n 个字符。举例来说,我要连续删除 10 个字符, 『10x』。

dd

剪切游标所在的那一整行(常用),用 p/P 可以粘贴。

ndd

n 为数字。剪切光标所在的方向向下 n 行,例如 20dd 则是剪切 20 行(常用),用 p/P 可以粘贴。

d1G

删除光标所在地到第一行的所有数据

dG

删除光标所在地到最后一行的所有数据

d$

删除游标所在处,到该行的最后一个字符

d0

那个是数字的 0 ,删除游标所在处,到该行的最前面的一个字符

yy

复制游标所在的那一行(常用)

nyy

n 为数字。复制光标所在的向下 n 行,例如 20yy 则是复制 20 行(常用)

y1G

复制游标所在行到第一行的所有数据

yG

复制游标所在行到最后一行的所有数据

y0

复制光标所在的那个字符到该行行首的所有数据

y$

复制光标所在的那个字符到该行行尾的所有数据

p, P

p 为将已复制的数据在光标下一行贴上,P 则为贴在游标上一行! 举例来说,我目前光标在第 20 行,且已经复制了 10 行数据。则按下 p 后, 那 10 数据会贴在原本的 20 行之后,亦即由 21 行开始贴。但如果是按下 P 呢? 那么原本的第 20 行会被推到变成 30 行。 (常用)

J

将光标所在行与下一行的数据结合成同一行

c

重复删除多个数据,例如向下删除 10 行,[ 10cj ]

u

复原前一个动作。(常用)

存储退出

:w

将编辑的数据写入硬盘档案中(常用)

:w!

若文件属性为『只读』时,强制写入该档案。不过,到底能不能写入, 还是跟你对该档案的档案权限有关啊!

:q

离开 vi (常用)

:q!

若曾修改过档案,又不想储存,使用 ! 为强制离开不储存档案。

注意一下啊,那个惊叹号 (!) 在 vi 当中,常常具有『强制』的意思~

:wq

储存后离开,若为 :wq! 则为强制储存后离开 (常用)

ZZ

这是大写的 Z 喔!如果修改过,保存当前文件,然后退出!效果等同于(保存并退出)

ZQ

不保存,强制退出。效果等同于 :q!

:w [filename]

将编辑的数据储存成另一个档案(类似另存新档)

:r [filename]

在编辑的数据中,读入另一个档案的数据。亦即将 『filename』 这个档案内容加到游标所在行后面

:n1,n2 w [filename]

将 n1 到 n2 的内容储存成 filename 这个档案。

:! command

暂时离开 vi 到指令行模式下执行 command 的显示结果!例如 『:! ls /home』即可在 vi 当中察看 /home 底下以 ls 输出的档案信息!

vim 环境的变更

:set nu

显示行号,设定之后,会在每一行的前缀显示该行的行号

:set nonu

与 set nu 相反,为取消行号!

文件搜索命令which 查找安命令

# 查找文件[root@ym test]# which cd/usr/bin/cd
find 搜索文件
-mount, -xdev : 只检查和指定目录在同一个文件系统下的文件,避免列出其它文件系统中的文件-amin n : 在过去 n 分钟内被读取过-anewer file : 比文件 file 更晚被读取过的文件-atime n : 在过去 n 天内被读取过的文件-cmin n : 在过去 n 分钟内被修改过-cnewer file :比文件 file 更新的文件-ctime n : 在过去 n 天内创建的文件-mtime n : 在过去 n 天内修改过的文件-empty : 空的文件-ipath p, -path p : 路径名称符合 p 的文件,ipath 会忽略大小写-name name,  : 文件名称符合 name 的文件。iname 会忽略大小写-size n : 文件大小 是 n 单位,b 代表 512 位元组的区块,c 表示字元数,k 表示 kilo bytes,w 是二个位元组。-perm -mode: 查找符合指定权限的文件-nogroup: 没有属组的文件-nouser:没有属主的文件-type c : 文件类型是 c 的文件。-print :将结果输出到标准输出上-exec :表示对文件在当前shell执行命令-ok:作用与-exec相同d: 目录c: 字型装置文件b: 区块装置文件p: 具名贮列f: 一般文件l: 符号连结s: socket-pid n : process id 是 n 的文件
# 按文件类型查找[root@ym test]# find ./ -user bin -type f./abc[root@ym test]# find ./ -user bin -type f -print./abc# 查抄属主属组被删除的文件[root@ym ~]# find /home -nouser -type f/home/qq/.bash_logout/home/qq/.bash_profile/home/qq/.bashrc/home/qq/qq_file/home/qq/.bash_history[root@ym test]# find ./ -group bin -type f ./abc# atime ctime 访问 修改时间查找文件[root@ym test]# find ./ -atime 1 -type f./file1./file2./file[root@ym test]# find ./ -atime +2 -type f./1test.sh./2test.sh./test.sh./Bigfile./test1/file.bz2# -perm[root@ym test]# find ./ -type f -perm -645./file# -size 按文件大小查找文件[root@ym test]# find . -size +5M ./Bigfile# -cnewer 比指定文件新[root@ym test]# find . -cnewer qq_file../qq_file1
whereis 搜索二进制文件
-b:只查找二进制文件-B:只在指定目录找二进制文件-f:不显示文件名前的路径名-m:只找说明文件-s:只找原始代码文件-S:只在指定目录找原始代码文件-u:查找不包含指定类型的文件
root@ym test]# whereis  -b bashbash: /usr/bin/bash[root@ym test]# whereis  -m bashbash: /usr/share/man/man1/bash.1.gz /usr/share/info/bash.info.gz[root@ym test]# whereis -B /usr/share/man -f bashbash: /usr/share/man/man1/bash.1.gz /usr/share/info/bash.info.gz[root@ym test]# whereis  -mf bashbash: /usr/share/man/man1/bash.1.gz /usr/share/info/bash.info.gz
locate 基于数据库搜索(速度快)
# locate 快速查找文件或目录# 可以使用 updatedb 更新数据库-r:递归查找包括子目录-i:或略大小写[root@ym ~]# locate bigfile/root/test/bigfile/root/test2/test/bigfile[root@ym ~]# locate -i bigfile/root/test/bigfile/root/test2/test/bigfile

标签: #xci安装软件