文章目录
- 一、Linux系统目录结构
- 二、文件与目录操作命令
- 1.pwd:打印工作目录命令
- 2.ls:目录内容查看命令
- 3.cat:这个命令常用来显示文件内容
- 4.more :用于逐页查看文本文件内容的命令。
- 5.less :看文本文件内容的命令,可以让用户在文件中进行搜索、前后翻页、跳转到指定行等操作。
- 6.head:显示文件的开头部分,默认情况下显示文件的前 10 行内容。
- 7.tail:显示文件的结尾部分,默认情况下显示文件的后 10 行内容。
- 8.echo:表示输出
- 9.mkdir:用来创建指定的名称的目录
- 10.cp:用来复制文件和目录的命令
- 11.stat:用于显示关于文件的状态信息。
- 12.tree:是一个用于以树状结构显示目录及其内容的实用程序,常用于显示文件系统的层次结构。
- 13.dd:用于复制文件和转换数据。它用于将源文件复制到目标文件,或者用于在目标文件上进行数据转换。
- 14.ln:用于在文件系统中创建链接(links)。链接分为两种类型:硬链接(hard link)和符号链接(symboliclink)。
- 三、Linux系统文件与目录内容检索
- 四、文件与目录归档压缩命令
- 五、统计命令
- 六、总结
一、Linux系统目录结构
#进入根目录
[root@bogon ~]# cd ..
#查看根目录中的文件夹
[root@bogon /]# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
根目录下各个文件夹的名称和用途
bin :命令,二进制文件的存放目录
boot:系统引导程序及系统内核文件存储目录
dev :设备、光驱、硬盘目录
etc :存放系统或服务的配置文件目录
home :普通用户的家目录
lib :库文件存放目录
lib64 :库文件存放目录(64位)
lost+found :磁盘、文件系统损坏、断电、存放临时文件目录
mnt :临时的挂载点目录
opt :三方软件安装目录
proc :虚拟目录,显示硬件的信息及内核参数配置文件
root :超级用户的家目录
sbin :超级命令,二进制文件的存放目录,只有root用户能够使用的命令
/etc/selinux :selinux及其配置文件存放目录,安全上下文
sys :虚拟目录,内核配置信息
tmp :临时文件的存放目录
usr :存放用户程序目录
var :log–系统日志目录;spool/mail–邮件文件
二、文件与目录操作命令
1.pwd:打印工作目录命令
[root@bogon /]# pwd
/
#进入bin文件夹
[root@bogon /]# cd bin
#打印当前工作目录
[root@bogon bin]# pwd
/bin
2.ls:目录内容查看命令
-a, --all :显示所有文件(包括以 . 开头的隐藏文件)
[root@bogon ~]# ls -a
. .bash_profile .esd_auth 公共 音乐
.. .bashrc .ICEauthority 模板 桌面
aa .cache initial-setup-ks.cfg 视频
anaconda-ks.cfg .config .local 图片
.bash_history .cshrc .tcshrc 文档
.bash_logout .dbus .viminfo 下载
-l :使用详细列表格式显示文件或目录的详细信息
[root@bogon ~]# ls -l
总用量 8
drwxr-xr-x. 2 root root 6 5月 9 15:20 aa
-rw-------. 1 root root 1698 5月 8 02:29 anaconda-ks.cfg
-rw-r--r--. 1 root root 1746 5月 8 02:32 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 5月 8 02:34 公共
drwxr-xr-x. 2 root root 6 5月 8 02:34 模板
drwxr-xr-x. 2 root root 6 5月 8 02:34 视频
drwxr-xr-x. 2 root root 6 5月 8 02:34 图片
drwxr-xr-x. 2 root root 6 5月 8 02:34 文档
drwxr-xr-x. 2 root root 6 5月 8 02:34 下载
drwxr-xr-x. 2 root root 6 5月 8 02:34 音乐
drwxr-xr-x. 2 root root 6 5月 8 02:34 桌面
-h, --human-readable :使用人类可读的格式显示文件大小(例如 1K、2M)#注:一般和 ”l“ 一起使用
[root@bogon ~]# ls -lh
总用量 8.0K
drwxr-xr-x. 2 root root 6 5月 9 15:20 aa
-rw-------. 1 root root 1.7K 5月 8 02:29 anaconda-ks.cfg
-rw-r--r--. 1 root root 1.8K 5月 8 02:32 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 5月 8 02:34 公共
drwxr-xr-x. 2 root root 6 5月 8 02:34 模板
drwxr-xr-x. 2 root root 6 5月 8 02:34 视频
drwxr-xr-x. 2 root root 6 5月 8 02:34 图片
drwxr-xr-x. 2 root root 6 5月 8 02:34 文档
drwxr-xr-x. 2 root root 6 5月 8 02:34 下载
drwxr-xr-x. 2 root root 6 5月 8 02:34 音乐
drwxr-xr-x. 2 root root 6 5月 8 02:34 桌面
-r, --reverse :以相反的顺序列出文件
[root@bogon ~]# ls -r
桌面 下载 图片 模板 initial-setup-ks.cfg aa
音乐 文档 视频 公共 anaconda-ks.cfg
-t :按照修改时间(最新的文件在前)排序
[root@bogon ~]# ls -t
aa 模板 图片 下载 桌面 anaconda-ks.cfg
公共 视频 文档 音乐 initial-setup-ks.cfg
-S :按照文件大小排序 #注一般和“lh”一起使用
[root@bogon ~]# ls -lhS
总用量 8.0K
-rw-r--r--. 1 root root 1.8K 5月 8 02:32 initial-setup-ks.cfg
-rw-------. 1 root root 1.7K 5月 8 02:29 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 5月 9 15:20 aa
drwxr-xr-x. 2 root root 6 5月 8 02:34 公共
drwxr-xr-x. 2 root root 6 5月 8 02:34 模板
drwxr-xr-x. 2 root root 6 5月 8 02:34 视频
drwxr-xr-x. 2 root root 6 5月 8 02:34 图片
drwxr-xr-x. 2 root root 6 5月 8 02:34 文档
drwxr-xr-x. 2 root root 6 5月 8 02:34 下载
drwxr-xr-x. 2 root root 6 5月 8 02:34 音乐
drwxr-xr-x. 2 root root 6 5月 8 02:34 桌面
-d, --directory :显示目录本身,而非目录下的文件列表
[root@bogon ~]# ls -d
.
-i, --inode :显示每个文件的 inode 号
[root@bogon ~]# ls -i
68335309 aa 3516 模板 100663418 下载
100663362 anaconda-ks.cfg 33713629 视频 100663419 音乐
100663363 initial-setup-ks.cfg 3517 图片 68802040 桌面
33713628 公共 68802041 文档
–color=auto :自动根据文件类型显示不同的颜色
[root@bogon ~]# ls --color=auto
aa initial-setup-ks.cfg 模板 图片 下载 桌面
anaconda-ks.cfg 公共 视频 文档 音乐
-R, --recursive :递归列出目录及其子目录中的文件
[root@bogon ~]# ls -R
.:
aa initial-setup-ks.cfg 模板 图片 下载 桌面
anaconda-ks.cfg 公共 视频 文档 音乐
./aa:
./公共:
./模板:
./视频:
./图片:
./文档:
./下载:
./音乐:
./桌面:
3.cat:这个命令常用来显示文件内容
一次显示整个文件:cat filename
从键盘创建一个文件:cat > filename 只能创建新文件,不能编辑已有文件.
将几个文件合并为一个文件:cat file1 file2 > file3
追加内容到指定文件
[root@bogon ~]# cat <<ad >> 1.txt
> 123
> 345
> 456
> 789
> 0asd
> qad
> ad
[root@bogon ~]# ls
1.txt aa anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@bogon ~]# cat <<c >> 2.txt
> 1
> 2
> 3
> c
[root@bogon ~]# cat <<a >> 3.txt
> 13456768
> 09876565
> a
[root@bogon ~]# cat 3.txt
123
345
456
789
0asd
qad
1
2
3
4.more :用于逐页查看文本文件内容的命令。
[root@localhost aa]# more text.txt
This document contains the following sections:
1. Overview
2. System Requirements
3. Contents of the Distribution Package
3A. Public and NDA Configurations
4. List of Available Command Line Switch Options
5. Installing the Software in Interactive Mode
6. Installing the Software in Silent Mode
7. Installing the INF Files Prior to OS Installation
8. Installing the INF Files After OS Installation
9. Verifying Installation of the Software and
Identifying the Software Version Number
10. Troubleshooting
* 5. INSTALLING THE SOFTWARE IN INTERACTIVE MODE
************************************************************
1. Verify that all system requirements have been met as
described in Section 2 above.
2. Run the setup program:
SetupChipset.exe
3. You will be prompted to agree to the license agreement.
If you do not agree, the installation program will exit.
4. Upon successful installation you will see a screen listing
--More--(27%)
5.less :看文本文件内容的命令,可以让用户在文件中进行搜索、前后翻页、跳转到指定行等操作。
This document contains the following sections:
1. Overview
2. System Requirements
3. Contents of the Distribution Package
3A. Public and NDA Configurations
4. List of Available Command Line Switch Options
5. Installing the Software in Interactive Mode
6. Installing the Software in Silent Mode
7. Installing the INF Files Prior to OS Installation
8. Installing the INF Files After OS Installation
9. Verifying Installation of the Software and
Identifying the Software Version Number
10. Troubleshooting
* 5. INSTALLING THE SOFTWARE IN INTERACTIVE MODE
************************************************************
1. Verify that all system requirements have been met as
described in Section 2 above.
2. Run the setup program:
SetupChipset.exe
3. You will be prompted to agree to the license agreement.
If you do not agree, the installation program will exit.
4. Upon successful installation you will see a screen listing
Intel(R) Chipset Device Software as installed.
:
6.head:显示文件的开头部分,默认情况下显示文件的前 10 行内容。
head 文件名
[root@localhost aa]# head text.txt
This document contains the following sections:
1. Overview
2. System Requirements
3. Contents of the Distribution Package
3A. Public and NDA Configurations
4. List of Available Command Line Switch Options
5. Installing the Software in Interactive Mode
6. Installing the Software in Silent Mode
7. Installing the INF Files Prior to OS Installation
或“-行数”可以查看多少行
head -行数 文件名
[root@localhost aa]# cat -n text.txt | head -10
1 This document contains the following sections:
2
3 1. Overview
4 2. System Requirements
5 3. Contents of the Distribution Package
6 3A. Public and NDA Configurations
7 4. List of Available Command Line Switch Options
8 5. Installing the Software in Interactive Mode
9 6. Installing the Software in Silent Mode
10 7. Installing the INF Files Prior to OS Installation
7.tail:显示文件的结尾部分,默认情况下显示文件的后 10 行内容。
tail 文件名
[root@localhost aa]# cat -n text.txt | tail -10
83 * 9. IDENTIFYING THE SOFTWARE VERSION NUMBER
84 ************************************************************
85 The version numbers displayed by Device Manager for a given
86 device may not be the same as the Intel(R) Chipset Device
87 Software version.
88
89 The correct version number is indicated at the registry
90 entry specified in section 6.
91
92
注意tail -f 文件名 可以监控文件,对文件进行实时监测。
8.echo:表示输出
-n 在输出内容后不换行。
-e 启用转义字符的解析。“"转义符号;\n:换行;\r:回车;\t:水平制表符(tab)
[root@localhost aa]# echo 123
123
9.mkdir:用来创建指定的名称的目录
-p 递归创建目录,如果父目录不存在也会创建。
[root@localhost ~]# ls
2.txt 3.txt aa anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@localhost ~]# mkdir text
[root@localhost ~]# ls
2.txt 3.txt aa anaconda-ks.cfg initial-setup-ks.cfg text 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@localhost ~]#
10.cp:用来复制文件和目录的命令
-r或-R(递归) 用于复制目录以及目录内所有子目录和文件。
-p(保留属性) 复制文件的同时保留源文件的属性如权限和时间戳。
[root@localhost ~]# ls
2.txt 3.txt aa anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@localhost ~]# cp -r aa/ bb
[root@localhost ~]# ls
2.txt 3.txt aa anaconda-ks.cfg bb initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@localhost ~]# cd bb
[root@localhost bb]# ls
text.txt
11.stat:用于显示关于文件的状态信息。
[root@localhost ~]# stat aa
文件:"aa"
大小:22 块:0 IO 块:4096 目录
设备:fd00h/64768d Inode:68335309 硬链接:2
权限:(0755/drwxr-xr-x) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2025-05-12 14:43:26.375768756 +0800
最近更改:2025-05-12 14:43:20.883702095 +0800
最近改动:2025-05-12 14:43:20.883702095 +0800
创建时间:-
12.tree:是一个用于以树状结构显示目录及其内容的实用程序,常用于显示文件系统的层次结构。
[root@localhost ~]# mkdir -p text1/tex11/text33
[root@localhost ~]# ls
2.txt 3.txt aa anaconda-ks.cfg bb initial-setup-ks.cfg text1 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@localhost ~]# tree text1
text1
└── tex11
└── text33
2 directories, 0 files
13.dd:用于复制文件和转换数据。它用于将源文件复制到目标文件,或者用于在目标文件上进行数据转换。
if=filename 指定输入文件。
of=filename 指定输出文件。
bs=size 指定块大小。
count=num 指定要复制的块数。
[root@localhost ~]# ls
2.txt 3.txt aa anaconda-ks.cfg bb initial-setup-ks.cfg text1 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@localhost ~]# dd if=./2.txt of=./aa/text2.txt
记录了0+1 的读入
记录了0+1 的写出
6字节(6 B)已复制,0.000159546 秒,37.6 kB/秒
[root@localhost ~]# tree aa
aa
├── text2.txt
└── text.txt
0 directories, 2 files
14.ln:用于在文件系统中创建链接(links)。链接分为两种类型:硬链接(hard link)和符号链接(symboliclink)。
硬链接(hard link):(目录不允许创建硬链接)
硬链接是文件系统中的一个目录条目,它指向另一个文件的数据块。
硬链接和原始文件共享同一个 inode,因此对文件的修改会影响到所有硬链接。
硬链接不能跨越文件系统边界。
符号(软)链接(symbolic link):
符号链接是一个特殊的文件,其内容是指向另一个文件的路径。
dd [选项] [输出选项] [输入选项]
符号链接和原始文件不共享 inode,因此删除原始文件后,符号链接仍然存在,但无法访问。
符号链接可以跨越文件系统边界。
注意:001.txt 和2.txt是有背景颜色的,证明是硬连接
[root@localhost ~]# ls
2.txt 3.txt aa anaconda-ks.cfg bb initial-setup-ks.cfg text1 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@localhost ~]# ln 2.txt 001.txt
[root@localhost ~]# ls -l
总用量 20
-rw-r--r--. 2 root root 6 5月 9 16:57 001.txt
-rw-r--r--. 2 root root 6 5月 9 16:57 2.txt
-rw-r--r--. 1 root root 31 5月 9 16:59 3.txt
drwxr-xr-x. 2 root root 39 5月 12 18:34 aa
-rw-------. 1 root root 1698 5月 8 02:29 anaconda-ks.cfg
drwxr-xr-x. 2 root root 22 5月 12 16:17 bb
-rw-r--r--. 1 root root 1746 5月 8 02:32 initial-setup-ks.cfg
drwxr-xr-x. 3 root root 19 5月 12 16:48 text1
drwxr-xr-x. 2 root root 6 5月 8 02:34 公共
drwxr-xr-x. 2 root root 6 5月 8 02:34 模板
drwxr-xr-x. 2 root root 6 5月 8 02:34 视频
drwxr-xr-x. 2 root root 6 5月 8 02:34 图片
drwxr-xr-x. 2 root root 6 5月 8 02:34 文档
drwxr-xr-x. 2 root root 6 5月 8 02:34 下载
drwxr-xr-x. 2 root root 6 5月 8 02:34 音乐
drwxr-xr-x. 2 root root 6 5月 8 02:34 桌面
三、Linux系统文件与目录内容检索
1、grep筛选
在文本中查找指定的字符串所在的行。
[root@bogon bb]# cat test.txt
shirt
short
gOOd
food
wood
wooooooood
gooood
adcxyzxyzxyz
abcABC
best
besssst
ofion
ofson
ofison
AxyzxyzC
#test
#tast
#hoo
#boo
#joo
ferd
命令
-i 忽略大小写。
-v 反转匹配,只显示不匹配的行。
-c 计数,只输出匹配行的数量。
-n 显示匹配行及其行号。
-l(小写L) 只输出包含匹配字符串的文件名。
-r 或 --recursive 递归搜索目录中的文件。
案例
1.在 目录中查找所有包含 oo
的行(忽略大小写)。
[root@bogon bb]# grep -n -i "OO" test.txt
3:gOOd
4:food
5:wood
6:wooooooood
7:gooood
21:#hoo
22:#boo
23:#joo
2.统计目录中 oo
出现的次数。
[root@bogon bb]# grep -n -i "OO" test.txt | wc -l
8
3.列出 /etc
目录下所有包含 root
的配置文件名称。
[root@bogon bb]# grep -rl "root" /etc
/etc/fstab
/etc/grub.d/00_header
/etc/grub.d/01_users
/etc/grub.d/10_linux
/etc/grub.d/20_linux_xen
/etc/grub.d/30_os-prober
/etc/pki/ca-trust/ca-legacy.conf
/etc/pki/ca-trust/extracted/README
/etc/pki/ca-trust/extracted/java/README
/etc/pki/ca-trust/extracted/java/cacerts
4.从 /etc/passwd
中提取所有以 /sbin/nologin
结尾的行。
[root@bogon bb]# grep -rl "/sbin/nologin" /etc/passwd
/etc/passwd
5.查找 /etc/ssh/sshd_config
中所有非注释行(排除以 #
开头的行)。
[root@bogon bb]# grep -v "^#" /etc/ssh/sshd_config
6.递归搜索 /etc/yum.repos.d/
目录下所有包含 baseurl
的文件。
[root@bogon bb]# grep -rl "baseurl" /etc/yum.repos.d/
/etc/yum.repos.d/CentOS-Base.repo
/etc/yum.repos.d/epel-testing.repo
/etc/yum.repos.d/epel.repo
7.查找 /etc/fstab
中所有空行并显示行号。
[root@bogon bb]# find /var/log -name "*.log" -size -10M
/var/log/audit/audit.log
/var/log/gdm/:0.log
/var/log/gdm/:0-greeter.log
/var/log/tuned/tuned.log
/var/log/anaconda/anaconda.log
/var/log/anaconda/X.log
/var/log/anaconda/program.log
/var/log/anaconda/packaging.log
/var/log/anaconda/storage.log
/var/log/anaconda/ifcfg.log
2、find查找
find - 递归地在层次目录中处理文件
-name | 根据关键字查找,支持通配符* |
---|---|
-type | 根据文件类型查找 |
-size | 根据文件大小查找 |
-maxdepth | 指定查找时的路径深度 |
-exec | 将find命令查找到的内容交给-exec后面的命令再次处理 |
-user | 根据文件的属主进行查找 |
-perm | 根据文件的权限进行查找 |
案例:
- 查找
/var/log/
下所有小于10MB
的日志文件。
[root@bogon etc]#find /var/log -name "*.log" -size -10M
- 列出
/etc/
下所有属主为root
的.conf
文件。
[root@bogon etc]# find /etc/ -user "root" -name "*.conf"
/etc/resolv.conf
/etc/fonts/conf.d/65-0-khmeros-base.conf
/etc/fonts/conf.d/61-urw-fallback-backwards.conf
/etc/fonts/conf.d/66-ucs-miscfixed.conf
/etc/fonts/conf.d/31-cantarell.conf
/etc/fonts/conf.d/60-overpass.conf
/etc/fonts/conf.d/59-liberation-sans.conf
- 查找
/home/
下所有空文件。
[root@bogon etc]# find /home/ -size 0
- 查找
/usr/lib/
下所有符号链接文件。
[root@bogon etc]# find /usr/lib/ -type l
/usr/lib/debug/usr/.dwz
/usr/lib/debug/bin
/usr/lib/debug/lib
/usr/lib/debug/lib64
/usr/lib/debug/sbin
- 统计
/var/log/
目录下每个子目录中.log文件的数量。
[root@bogon bb]# find /var/log/ -name "*.log" | xargs dirname | soft |uniq -c
3、sort排序
以行对文件进行排序
选项 | 作用 |
---|---|
-n | 按照数值进行排序 |
-r | 反向排序 |
-k | 指定排序字段 |
-o<输出文件> | 将排序后的结果转存至指定文件 |
4、uniq去重
uniq 是 Unix 和类 Unix 系统中的命令,用于从排序的文本数据中去除重复行,仅保留唯一的行。它通常与 sort 命令结合使用,因为 uniq 只能删除相邻的重复行。
-c 进行计数,并删除文件中重复出现的行
sort排序和uniq去重的案例:
- 对
/etc/passwd
按用户名(第一列)排序并去重。
cut -d: -f1 /etc/passwd | sort | uniq
- 将
/var/log/yum.log
按日期(第一列)排序。
cut -d: -f1 /var/log/yum.log | sort
- 合并
/var/log/messages
和/var/log/secure
,去重后保存到merged_logs.txt
。
cat /var/log/messages /var/log/secure | sort | uniq > merged_logs.txt
- 对
/etc/group
按组 ID(第三列)数值排序。
cat /etc/passwd | cut -d" " -f3 | sort
6 从 /var/log/maillog
中提取所有唯一的时间戳(格式 HH:MM:SS
)。
cat /var/log/maillog | tr -s " "| cut -d" " -f3 | uniq
5、tr转换
tr 命令是 Unix 和类 Unix 系统中的一个用于字符转换或删除的实用程序。tr 命令通常用于处理文本数据,例如删除特定字符、替换字符、转换大小写等操作。
选项 | 作用 |
---|---|
-d | 删除所有属于字符集1(-d 后面的参数)的字符 |
-s | 将重复出现的字符串压缩为一个字符串;用字符集2 替换 字符集1 |
案例:
1.将文件 test.txt 中所有字母转换为大写,并删除所有数字。
[root@bogon ~]# cat test.txt
123
456
789
ewrtyu
fenusfnus
esfiesig
qewrtyu
oiuyt
123
456
789
szxfg
rgfb
qazwsxedc
cat test.txt | tr [a-z] [A-Z] | tr -d [0-9]
EWRTYU
FENUSFNUS
ESFIESIG
QEWRTYU
OIUYT
SZXFG
RGFB
QAZWSXEDC
6、cut 切割
cut 是 Unix 和类 Unix 系统中的命令,用于根据指定的字符分隔符从输入中提取字段。
选项 | 作用 |
---|---|
-f | 通过指定哪一个字段进行提取。cut命令使用“TAB”作为默认的字段分割符 |
-d | “TAB”是默认的分隔符,使用此选项可更改为其他的分隔符 |
案例:
1.从 /etc/passwd 文件中提取所有用户的用户名(第一列)和默认 Shell(第七列),以冒号分隔。
[root@bogon ~]# cut -d: -f1,7 /etc/passwd | sort | uniq
abrt:/sbin/nologin
adm:/sbin/nologin
avahi:/sbin/nologin
bin:/sbin/nologin
chrony:/sbin/nologin
colord:/sbin/nologin
daemon:/sbin/nologin
dbus:/sbin/nologin
ftp:/sbin/nologin
7、which命令
通常用于确认命令是否存在于系统 PATH 中,或者用于在脚本中获取命令的确切路径。
案例:
查找 passwd可执行文件的路径及其帮助文档位置。
[root@bogon ~]# which passwd
/usr/bin/passwd
8、whereis命令
whereis 命令在 Unix 和类 Unix 系统中用于查找二进制文件、源代码文件和手册页的位置。它搜索默认的文件系统路径来确定指定命令的相关文件。
案例:
查找passwd 可执行文件的路径及其帮助文档位置。
[root@bogon ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz
9、diff命令
案例:
比较两个配置文件 config_old.txt 和 config_new.txt 的差异,并生成简洁报告。
[root@bogon ~]# cat config_old.txt
123456
456789
qwe
asd
zxc
[root@bogon ~]# cat config_new.txt
123456
4567890
qwe
asd
zxc
mnbvc
[root@bogon ~]# diff config_old.txt config_new.txt
2c2
< 456789
---
> 4567890
5a6
> mnbvc
四、文件与目录归档压缩命令
1、tar命令
-c:创建归档文件。
-z:使用 gzip 压缩。
-v:显示详细过程。
案例:
将 /root/aaa下test11.txt文件打包为 test11.tar.gz,并解压到 aaa目录下。
[root@bogon aaa]# tar -czvf test11.tar.gz ./test11.txt
./test11.txt
[root@bogon aaa]# ls
test11.tar.gz test11.txt
解压到 /root/bbb目录下。
[root@bogon aaa]# tar -xvf test11.tar.gz -C /root/bbb
./test11.txt
[root@bogon aaa]# cd /root/bbb
[root@bogon bbb]# ls
test11.txt
2、zip/unzip命令
案例:
将aaa文件下的test11.txt进行zip压缩成t200.zip,之后解压到bbb
[root@bogon aaa]# zip t200.zip test11.txt
adding: test11.txt (deflated 100%)
[root@bogon aaa]# ls
t200.zip test11.txt
[root@bogon aaa]# ls -l
总用量 102500
-rw-r--r--. 1 root root 101943 5月 14 19:16 t200.zip
-rw-r--r--. 1 root root 104857600 5月 14 18:16 test11.txt
[root@bogon aaa]# unzip t200.zip -d /root/bbb
Archive: t200.zip
inflating: /root/bbb/test11.txt
[root@bogon aaa]# cd /root/bbb
[root@bogon bbb]# ls
test11.txt
3、gzip / gunzip命令
注意:压缩后源文件消失
gzip 需要压缩的文件
gzip -k filename # 保留原文件
4、bzip2 / bunzip2命令
注意:压缩后源文件消失
bzip2 需要压缩的文件
bunzip2 压缩文件
5、xz / unxz命令
注意:压缩后源文件消失
压缩
xz filename
解压
unxz filename.xz
五、统计命令
1、wc命令
统计文件内容的行数、字符数、单词数
wc -l file.txt # 统计文件行数(常用日志分析)
wc -c file.txt # 统计字节数
wc -m file.txt # 统计字符数(与字节数区别在UTF-8环境)
wc -w file.txt # 统计单词数(以空格分隔)
wc -L file.txt # 统计最长行的长度
2、du命令
统计文件占用磁盘空间的容量
du -sh /path/to/dir # 统计目录总大小(-s汇总,-h人性化显示)
du -h --max-depth=1 /var # 显示/var下一级子目录大小
du -ah /path # 显示所有文件及子目录大小(含隐藏文件)
六、总结
重定向符号:
1、正确重定向
覆盖重定向 >
追加重定向 >>
混合重定向 &>
万事开头难,还得多练!!!