服务器大本营

 找回密码
 我要入营

QQ登录

只需一步,快速开始

查看: 21|回复: 0

Linux系统文件与目录管理

[复制链接]

7万

敬重

838

主题

207

精华

管理员

积分
9106

启航之章进阶之印里程之碑突破之证飞跃之星蜕变之勋卓越之路龙年行大运

QQ
发表于 昨天 15:30 | 显示全部楼层 |阅读模式

我要入营,结交更多好友,开启更多功能,轻松玩转服务器大本营!

您需要 登录 才可以下载或查看,没有账号?我要入营

x
一、Linux系统目录结构

Linux系统文件与目录管理

Linux系统文件与目录管理


  1. #进入根目录
  2. [root@bogon ~]# cd ..

  3. #查看根目录中的文件夹
  4. [root@bogon /]# ls
  5. bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
  6. 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:打印工作目录命令
  1. [root@bogon /]# pwd
  2. /
  3. #进入bin文件夹
  4. [root@bogon /]# cd bin
  5. #打印当前工作目录
  6. [root@bogon bin]# pwd
  7. /bin
复制代码

2、ls:目录内容查看命令
-a, --all :显示所有文件(包括以 . 开头的隐藏文件)
  1. [root@bogon ~]# ls -a
  2. .                .bash_profile  .esd_auth             公共  音乐
  3. ..               .bashrc        .ICEauthority         模板  桌面
  4. aa               .cache         initial-setup-ks.cfg  视频
  5. anaconda-ks.cfg  .config        .local                图片
  6. .bash_history    .cshrc         .tcshrc               文档
  7. .bash_logout     .dbus          .viminfo              下载
复制代码

-l :使用详细列表格式显示文件或目录的详细信息
  1. [root@bogon ~]# ls -l
  2. 总用量 8
  3. drwxr-xr-x. 2 root root    6 5月   9 15:20 aa
  4. -rw-------. 1 root root 1698 5月   8 02:29 anaconda-ks.cfg
  5. -rw-r--r--. 1 root root 1746 5月   8 02:32 initial-setup-ks.cfg
  6. drwxr-xr-x. 2 root root    6 5月   8 02:34 公共
  7. drwxr-xr-x. 2 root root    6 5月   8 02:34 模板
  8. drwxr-xr-x. 2 root root    6 5月   8 02:34 视频
  9. drwxr-xr-x. 2 root root    6 5月   8 02:34 图片
  10. drwxr-xr-x. 2 root root    6 5月   8 02:34 文档
  11. drwxr-xr-x. 2 root root    6 5月   8 02:34 下载
  12. drwxr-xr-x. 2 root root    6 5月   8 02:34 音乐
  13. drwxr-xr-x. 2 root root    6 5月   8 02:34 桌面
复制代码

-h, --human-readable :使用人类可读的格式显示文件大小(例如 1K、2M)#注:一般和 ”l“ 一起使用
  1. [root@bogon ~]# ls -lh
  2. 总用量 8.0K
  3. drwxr-xr-x. 2 root root    6 5月   9 15:20 aa
  4. -rw-------. 1 root root 1.7K 5月   8 02:29 anaconda-ks.cfg
  5. -rw-r--r--. 1 root root 1.8K 5月   8 02:32 initial-setup-ks.cfg
  6. drwxr-xr-x. 2 root root    6 5月   8 02:34 公共
  7. drwxr-xr-x. 2 root root    6 5月   8 02:34 模板
  8. drwxr-xr-x. 2 root root    6 5月   8 02:34 视频
  9. drwxr-xr-x. 2 root root    6 5月   8 02:34 图片
  10. drwxr-xr-x. 2 root root    6 5月   8 02:34 文档
  11. drwxr-xr-x. 2 root root    6 5月   8 02:34 下载
  12. drwxr-xr-x. 2 root root    6 5月   8 02:34 音乐
  13. drwxr-xr-x. 2 root root    6 5月   8 02:34 桌面
复制代码

-r, --reverse :以相反的顺序列出文件
  1. [root@bogon ~]# ls -r
  2. 桌面  下载  图片  模板  initial-setup-ks.cfg  aa
  3. 音乐  文档  视频  公共  anaconda-ks.cfg
复制代码

-t :按照修改时间(最新的文件在前)排序
  1. [root@bogon ~]# ls -t
  2. aa    模板  图片  下载  桌面                  anaconda-ks.cfg
  3. 公共  视频  文档  音乐  initial-setup-ks.cfg
复制代码

-S :按照文件大小排序 #注一般和“lh”一起使用
  1. [root@bogon ~]# ls -lhS
  2. 总用量 8.0K
  3. -rw-r--r--. 1 root root 1.8K 5月   8 02:32 initial-setup-ks.cfg
  4. -rw-------. 1 root root 1.7K 5月   8 02:29 anaconda-ks.cfg
  5. drwxr-xr-x. 2 root root    6 5月   9 15:20 aa
  6. drwxr-xr-x. 2 root root    6 5月   8 02:34 公共
  7. drwxr-xr-x. 2 root root    6 5月   8 02:34 模板
  8. drwxr-xr-x. 2 root root    6 5月   8 02:34 视频
  9. drwxr-xr-x. 2 root root    6 5月   8 02:34 图片
  10. drwxr-xr-x. 2 root root    6 5月   8 02:34 文档
  11. drwxr-xr-x. 2 root root    6 5月   8 02:34 下载
  12. drwxr-xr-x. 2 root root    6 5月   8 02:34 音乐
  13. drwxr-xr-x. 2 root root    6 5月   8 02:34 桌面
复制代码

-d, --directory :显示目录本身,而非目录下的文件列表
  1. [root@bogon ~]# ls -d
  2. .
复制代码

-i, --inode :显示每个文件的 inode 号
  1. [root@bogon ~]# ls -i
  2. 68335309 aa                         3516 模板  100663418 下载
  3. 100663362 anaconda-ks.cfg        33713629 视频  100663419 音乐
  4. 100663363 initial-setup-ks.cfg       3517 图片   68802040 桌面
  5. 33713628 公共                   68802041 文档
复制代码

–color=auto :自动根据文件类型显示不同的颜色
  1. [root@bogon ~]# ls --color=auto
  2. aa               initial-setup-ks.cfg  模板  图片  下载  桌面
  3. anaconda-ks.cfg  公共                  视频  文档  音乐
复制代码

-R, --recursive :递归列出目录及其子目录中的文件
  1. [root@bogon ~]# ls -R
  2. .:
  3. aa               initial-setup-ks.cfg  模板  图片  下载  桌面
  4. anaconda-ks.cfg  公共                  视频  文档  音乐

  5. ./aa:

  6. ./公共:

  7. ./模板:

  8. ./视频:

  9. ./图片:

  10. ./文档:

  11. ./下载:

  12. ./音乐:

  13. ./桌面:
复制代码

3、cat:这个命令常用来显示文件内容
一次显示整个文件:cat filename
从键盘创建一个文件:cat > filename 只能创建新文件,不能编辑已有文件.
将几个文件合并为一个文件:cat file1 file2 > file3
追加内容到指定文件
  1. [root@bogon ~]# cat <<ad >> 1.txt
  2. > 123
  3. > 345
  4. > 456
  5. > 789
  6. > 0asd
  7. > qad
  8. > ad
  9. [root@bogon ~]# ls
  10. 1.txt  aa  anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
复制代码
  1. [root@bogon ~]# cat <<c >> 2.txt
  2. > 1
  3. > 2
  4. > 3
  5. > c
  6. [root@bogon ~]# cat <<a >> 3.txt
  7. > 13456768
  8. > 09876565
  9. > a
复制代码
  1. [root@bogon ~]# cat 3.txt
  2. 123
  3. 345
  4. 456
  5. 789
  6. 0asd
  7. qad
  8. 1
  9. 2
  10. 3
复制代码

4、more :用于逐页查看文本文件内容的命令
  1. [root@localhost aa]# more text.txt
  2. This document contains the following sections:

  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
  11. 8.  Installing the INF Files After OS Installation
  12. 9.  Verifying Installation of the Software and
  13.      Identifying the Software Version Number
  14. 10. Troubleshooting

  15. * 5.  INSTALLING THE SOFTWARE IN INTERACTIVE MODE
  16. ************************************************************
  17. 1.  Verify that all system requirements have been met as
  18.      described in Section 2 above.

  19. 2.  Run the setup program:
  20.      SetupChipset.exe

  21. 3.  You will be prompted to agree to the license agreement.  
  22.      If you do not agree, the installation program will exit.

  23. 4.  Upon successful installation you will see a screen listing
  24. --More--(27%)
复制代码

less :看文本文件内容的命令,可以让用户在文件中进行搜索、前后翻页、跳转到指定行等操作。
  1. This document contains the following sections:
  2. 1.  Overview
  3. 2.  System Requirements
  4. 3.  Contents of the Distribution Package
  5.     3A. Public and NDA Configurations
  6. 4.  List of Available Command Line Switch Options
  7. 5.  Installing the Software in Interactive Mode
  8. 6.  Installing the Software in Silent Mode
  9. 7.  Installing the INF Files Prior to OS Installation
  10. 8.  Installing the INF Files After OS Installation
  11. 9.  Verifying Installation of the Software and
  12.      Identifying the Software Version Number
  13. 10. Troubleshooting

  14. * 5.  INSTALLING THE SOFTWARE IN INTERACTIVE MODE
  15. ************************************************************
  16. 1.  Verify that all system requirements have been met as
  17.      described in Section 2 above.

  18. 2.  Run the setup program:
  19.      SetupChipset.exe

  20. 3.  You will be prompted to agree to the license agreement.  
  21.      If you do not agree, the installation program will exit.

  22. 4.  Upon successful installation you will see a screen listing
  23.      Intel(R) Chipset Device Software as installed.
  24. :
复制代码

5、head:显示文件的开头部分,默认情况下显示文件的前 10 行内容
head 文件名
  1. [root@localhost aa]# head text.txt
  2. This document contains the following sections:

  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
复制代码

或“-行数”可以查看多少行
head -行数 文件名
  1. [root@localhost aa]# cat -n text.txt | head -10
  2.      1  This document contains the following sections:
  3.      2
  4.      3  1.  Overview
  5.      4  2.  System Requirements
  6.      5  3.  Contents of the Distribution Package
  7.      6      3A. Public and NDA Configurations
  8.      7  4.  List of Available Command Line Switch Options
  9.      8  5.  Installing the Software in Interactive Mode
  10.      9  6.  Installing the Software in Silent Mode
  11.     10  7.  Installing the INF Files Prior to OS Installation
复制代码

6、tail:显示文件的结尾部分,默认情况下显示文件的后 10 行内容
tail 文件名
  1. [root@localhost aa]# cat -n text.txt | tail -10
  2.     83  * 9.    IDENTIFYING THE SOFTWARE VERSION NUMBER
  3.     84  ************************************************************
  4.     85  The version numbers displayed by Device Manager for a given
  5.     86  device may not be the same as the Intel(R) Chipset Device
  6.     87  Software version.
  7.     88   
  8.     89  The correct version number is indicated at the registry
  9.     90  entry specified in section 6.
  10.     91
  11.     92
复制代码
注意tail -f 文件名 可以监控文件,对文件进行实时监测。

7、echo:表示输出
-n 在输出内容后不换行。
-e 启用转义字符的解析。“"转义符号;\n:换行;\r:回车;\t:水平制表符(tab)
  1. [root@localhost aa]# echo 123
  2. 123
复制代码

8、mkdir:用来创建指定的名称的目录
-p 递归创建目录,如果父目录不存在也会创建。
  1. [root@localhost ~]# ls
  2. 2.txt  3.txt  aa  anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
  3. [root@localhost ~]# mkdir text
  4. [root@localhost ~]# ls
  5. 2.txt  3.txt  aa  anaconda-ks.cfg  initial-setup-ks.cfg  text  公共  模板  视频  图片  文档  下载  音乐  桌面
  6. [root@localhost ~]#
复制代码

9、cp:用来复制文件和目录的命令
-r或-R(递归) 用于复制目录以及目录内所有子目录和文件。
-p(保留属性) 复制文件的同时保留源文件的属性如权限和时间戳。
  1. [root@localhost ~]# ls
  2. 2.txt  3.txt  aa  anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
  3. [root@localhost ~]# cp -r aa/ bb
  4. [root@localhost ~]# ls
  5. 2.txt  3.txt  aa  anaconda-ks.cfg  bb  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
  6. [root@localhost ~]# cd bb
  7. [root@localhost bb]# ls
  8. text.txt
复制代码

10、stat:用于显示关于文件的状态信息
  1. [root@localhost ~]# stat aa
  2.   文件:"aa"
  3.   大小:22              块:0          IO 块:4096   目录
  4. 设备:fd00h/64768d      Inode:68335309    硬链接:2
  5. 权限:(0755/drwxr-xr-x)  Uid:(    0/    root)   Gid:(    0/    root)
  6. 环境:unconfined_u:object_r:admin_home_t:s0
  7. 最近访问:2025-05-12 14:43:26.375768756 +0800
  8. 最近更改:2025-05-12 14:43:20.883702095 +0800
  9. 最近改动:2025-05-12 14:43:20.883702095 +0800
  10. 创建时间:-
复制代码

tree:是一个用于以树状结构显示目录及其内容的实用程序,常用于显示文件系统的层次结构。
  1. [root@localhost ~]# mkdir -p text1/tex11/text33
  2. [root@localhost ~]# ls
  3. 2.txt  3.txt  aa  anaconda-ks.cfg  bb  initial-setup-ks.cfg  text1  公共  模板  视频  图片  文档  下载  音乐  桌面
  4. [root@localhost ~]# tree text1
  5. text1
  6. └── tex11
  7.     └── text33

  8. 2 directories, 0 files
复制代码

11、dd:用于复制文件和转换数据。它用于将源文件复制到目标文件,或者用于在目标文件上进行数据转换
if=filename 指定输入文件。
of=filename 指定输出文件。
bs=size 指定块大小。
count=num 指定要复制的块数。
  1. [root@localhost ~]# ls
  2. 2.txt  3.txt  aa  anaconda-ks.cfg  bb  initial-setup-ks.cfg  text1  公共  模板  视频  图片  文档  下载  音乐  桌面
  3. [root@localhost ~]# dd if=./2.txt of=./aa/text2.txt
  4. 记录了0+1 的读入
  5. 记录了0+1 的写出
  6. 6字节(6 B)已复制,0.000159546 秒,37.6 kB/秒
  7. [root@localhost ~]# tree aa
  8. aa
  9. ├── text2.txt
  10. └── text.txt

  11. 0 directories, 2 files
复制代码

12、ln:用于在文件系统中创建链接(links)。链接分为两种类型:硬链接(hard link)和符号链接(symboliclink)
硬链接(hard link):(目录不允许创建硬链接)
硬链接是文件系统中的一个目录条目,它指向另一个文件的数据块。
硬链接和原始文件共享同一个 inode,因此对文件的修改会影响到所有硬链接。
硬链接不能跨越文件系统边界。
符号(软)链接(symbolic link):
符号链接是一个特殊的文件,其内容是指向另一个文件的路径。
dd [选项] [输出选项] [输入选项]
符号链接和原始文件不共享 inode,因此删除原始文件后,符号链接仍然存在,但无法访问。
符号链接可以跨越文件系统边界。


注意:001.txt 和2.txt是有背景颜色的,证明是硬连接
  1. [root@localhost ~]# ls
  2. 2.txt  3.txt  aa  anaconda-ks.cfg  bb  initial-setup-ks.cfg  text1  公共  模板  视频  图片  文档  下载  音乐  桌面
  3. [root@localhost ~]# ln 2.txt 001.txt
  4. [root@localhost ~]# ls -l
  5. 总用量 20
  6. -rw-r--r--. 2 root root    6 5月   9 16:57 001.txt
  7. -rw-r--r--. 2 root root    6 5月   9 16:57 2.txt
  8. -rw-r--r--. 1 root root   31 5月   9 16:59 3.txt
  9. drwxr-xr-x. 2 root root   39 5月  12 18:34 aa
  10. -rw-------. 1 root root 1698 5月   8 02:29 anaconda-ks.cfg
  11. drwxr-xr-x. 2 root root   22 5月  12 16:17 bb
  12. -rw-r--r--. 1 root root 1746 5月   8 02:32 initial-setup-ks.cfg
  13. drwxr-xr-x. 3 root root   19 5月  12 16:48 text1
  14. drwxr-xr-x. 2 root root    6 5月   8 02:34 公共
  15. drwxr-xr-x. 2 root root    6 5月   8 02:34 模板
  16. drwxr-xr-x. 2 root root    6 5月   8 02:34 视频
  17. drwxr-xr-x. 2 root root    6 5月   8 02:34 图片
  18. drwxr-xr-x. 2 root root    6 5月   8 02:34 文档
  19. drwxr-xr-x. 2 root root    6 5月   8 02:34 下载
  20. drwxr-xr-x. 2 root root    6 5月   8 02:34 音乐
  21. drwxr-xr-x. 2 root root    6 5月   8 02:34 桌面
复制代码

三、Linux系统文件与目录内容检索
1、grep筛选
在文本中查找指定的字符串所在的行。
  1. [root@bogon bb]# cat test.txt
  2. shirt
  3. short
  4. gOOd
  5. food
  6. wood
  7. wooooooood
  8. gooood
  9. adcxyzxyzxyz
  10. abcABC
  11. best
  12. besssst

  13. ofion

  14. ofson

  15. ofison
  16. AxyzxyzC
  17. #test
  18. #tast
  19. #hoo
  20. #boo
  21. #joo

  22. ferd
复制代码

命令
-i 忽略大小写。
-v 反转匹配,只显示不匹配的行。
-c 计数,只输出匹配行的数量。
-n 显示匹配行及其行号。
-l(小写L) 只输出包含匹配字符串的文件名。
-r 或 --recursive 递归搜索目录中的文件。

案例
1)在 目录中查找所有包含 oo 的行(忽略大小写)
  1. [root@bogon bb]# grep -n -i "OO" test.txt
  2. 3:gOOd
  3. 4:food
  4. 5:wood
  5. 6:wooooooood
  6. 7:gooood
  7. 21:#hoo
  8. 22:#boo
  9. 23:#joo
复制代码

2)统计目录中 oo 出现的次数
  1. [root@bogon bb]# grep -n -i "OO" test.txt | wc -l
  2. 8
复制代码

3)列出 /etc 目录下所有包含 root 的配置文件名称
  1. [root@bogon bb]# grep -rl "root" /etc
  2. /etc/fstab
  3. /etc/grub.d/00_header
  4. /etc/grub.d/01_users
  5. /etc/grub.d/10_linux
  6. /etc/grub.d/20_linux_xen
  7. /etc/grub.d/30_os-prober
  8. /etc/pki/ca-trust/ca-legacy.conf
  9. /etc/pki/ca-trust/extracted/README
  10. /etc/pki/ca-trust/extracted/java/README
  11. /etc/pki/ca-trust/extracted/java/cacerts
复制代码

4)从 /etc/passwd 中提取所有以 /sbin/nologin 结尾的行。
  1. [root@bogon bb]# grep -rl "/sbin/nologin" /etc/passwd
  2. /etc/passwd
复制代码

5)查找 /etc/ssh/sshd_config 中所有非注释行(排除以 # 开头的行)。
  1. [root@bogon bb]# grep -v "^#" /etc/ssh/sshd_config
复制代码

6)递归搜索 /etc/yum.repos.d/ 目录下所有包含 baseurl 的文件。
  1. [root@bogon bb]# grep -rl "baseurl" /etc/yum.repos.d/
  2. /etc/yum.repos.d/CentOS-Base.repo
  3. /etc/yum.repos.d/epel-testing.repo
  4. /etc/yum.repos.d/epel.repo
复制代码

7)查找 /etc/fstab 中所有空行并显示行号。
  1. [root@bogon bb]# find /var/log -name "*.log"  -size -10M
  2. /var/log/audit/audit.log
  3. /var/log/gdm/:0.log
  4. /var/log/gdm/:0-greeter.log
  5. /var/log/tuned/tuned.log
  6. /var/log/anaconda/anaconda.log
  7. /var/log/anaconda/X.log
  8. /var/log/anaconda/program.log
  9. /var/log/anaconda/packaging.log
  10. /var/log/anaconda/storage.log
  11. /var/log/anaconda/ifcfg.log
复制代码

2、find查找
find - 递归地在层次目录中处理文件

-name 根据关键字查找,支持通配符*
-type根据文件类型查找
-size根据文件大小查找
-maxdepth 根据查找时的路径深度
-exec将find命令查找到的内容交给-exec后面的命令再次处理
-user根据文件的属主进行查找
-perm根据文件的权限进行查找

案例:
1)查找 /var/log/ 下所有小于 10MB 的日志文件。
  1. [root@bogon etc]#find /var/log -name "*.log"  -size -10M
复制代码

2)列出 /etc/ 下所有属主为 root 的 .conf 文件。
  1. [root@bogon etc]# find /etc/ -user "root" -name "*.conf"
  2. /etc/resolv.conf
  3. /etc/fonts/conf.d/65-0-khmeros-base.conf
  4. /etc/fonts/conf.d/61-urw-fallback-backwards.conf
  5. /etc/fonts/conf.d/66-ucs-miscfixed.conf
  6. /etc/fonts/conf.d/31-cantarell.conf
  7. /etc/fonts/conf.d/60-overpass.conf
  8. /etc/fonts/conf.d/59-liberation-sans.conf
复制代码

3)查找 /home/ 下所有空文件。
  1. [root@bogon etc]# find /home/ -size 0
复制代码

4)查找 /usr/lib/ 下所有符号链接文件。
  1. [root@bogon etc]# find /usr/lib/ -type l
  2. /usr/lib/debug/usr/.dwz
  3. /usr/lib/debug/bin
  4. /usr/lib/debug/lib
  5. /usr/lib/debug/lib64
  6. /usr/lib/debug/sbin
复制代码

5)统计 /var/log/ 目录下每个子目录中.log文件的数量。
  1. [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去重的案例:

1)对 /etc/passwd 按用户名(第一列)排序并去重。
  1. cut -d: -f1 /etc/passwd | sort | uniq
复制代码

2)将 /var/log/yum.log 按日期(第一列)排序。
  1. cut -d: -f1 /var/log/yum.log | sort
复制代码

3)合并 /var/log/messages 和 /var/log/secure,去重后保存到 merged_logs.txt。
  1. cat /var/log/messages /var/log/secure | sort | uniq > merged_logs.txt
复制代码

4)对 /etc/group 按组 ID(第三列)数值排序。
  1. cat /etc/passwd | cut -d" " -f3  | sort
复制代码

5)从 /var/log/maillog 中提取所有唯一的时间戳(格式 HH:MM:SS)。
  1. cat /var/log/maillog | tr -s " "| cut -d" " -f3 | uniq
复制代码

5、tr转换
tr 命令是 Unix 和类 Unix 系统中的一个用于字符转换或删除的实用程序。tr 命令通常用于处理文本数据,例如删除特定字符、替换字符、转换大小写等操作。

选项作用
-d删除所有属于字符集1(-d后面的参数)的字符
-s 将重复出现的字符串压缩成一个字符串;用字符集2 替换 字符集1

案例:
将文件 test.txt 中所有字母转换为大写,并删除所有数字。
  1. [root@bogon ~]# cat test.txt
  2. 123
  3. 456
  4. 789
  5. ewrtyu
  6. fenusfnus
  7. esfiesig
  8. qewrtyu
  9. oiuyt
  10. 123
  11. 456
  12. 789
  13. szxfg
  14. rgfb
  15. qazwsxedc
复制代码
  1. cat test.txt | tr [a-z] [A-Z] | tr -d [0-9]



  2. EWRTYU
  3. FENUSFNUS
  4. ESFIESIG
  5. QEWRTYU
  6. OIUYT



  7. SZXFG
  8. RGFB
  9. QAZWSXEDC
复制代码

6、cut 切割
cut 是 Unix 和类 Unix 系统中的命令,用于根据指定的字符分隔符从输入中提取字段。

选项作用
-f通过指定哪一个字段进行提取。cut命令使用“TAB”作为默认的字段分隔符
-d“TAB”是默认的分隔符,使用此选项可更改为其他的分隔符

案例:
从 /etc/passwd 文件中提取所有用户的用户名(第一列)和默认 Shell(第七列),以冒号分隔。
  1. [root@bogon ~]# cut -d: -f1,7 /etc/passwd | sort | uniq
  2. abrt:/sbin/nologin
  3. adm:/sbin/nologin
  4. avahi:/sbin/nologin
  5. bin:/sbin/nologin
  6. chrony:/sbin/nologin
  7. colord:/sbin/nologin
  8. daemon:/sbin/nologin
  9. dbus:/sbin/nologin
  10. ftp:/sbin/nologin
复制代码

7、which命令
通常用于确认命令是否存在于系统 PATH 中,或者用于在脚本中获取命令的确切路径。

案例:
查找 passwd可执行文件的路径及其帮助文档位置。
  1. [root@bogon ~]# which passwd
  2. /usr/bin/passwd
复制代码

8、whereis命令
whereis 命令在 Unix 和类 Unix 系统中用于查找二进制文件、源代码文件和手册页的位置。它搜索默认的文件系统路径来确定指定命令的相关文件。

案例:
查找passwd 可执行文件的路径及其帮助文档位置。
  1. [root@bogon ~]# whereis passwd
  2. 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 的差异,并生成简洁报告。
  1. [root@bogon ~]# cat config_old.txt
  2. 123456
  3. 456789
  4. qwe
  5. asd
  6. zxc
  7. [root@bogon ~]# cat config_new.txt
  8. 123456
  9. 4567890
  10. qwe
  11. asd
  12. zxc
  13. mnbvc
复制代码
  1. [root@bogon ~]# diff config_old.txt  config_new.txt
  2. 2c2
  3. < 456789
  4. ---
  5. > 4567890
  6. 5a6
  7. > mnbvc
复制代码

四、文件与目录归档压缩命令
1、tar命令
-c:创建归档文件。
-z:使用 gzip 压缩。
-v:显示详细过程。


案例:
将 /root/aaa下test11.txt文件打包为 test11.tar.gz,并解压到 aaa目录下。
  1. [root@bogon aaa]# tar -czvf test11.tar.gz ./test11.txt
  2. ./test11.txt
  3. [root@bogon aaa]# ls
  4. test11.tar.gz  test11.txt
复制代码

解压到 /root/bbb目录下。
  1. [root@bogon aaa]# tar -xvf test11.tar.gz -C /root/bbb
  2. ./test11.txt
  3. [root@bogon aaa]# cd /root/bbb
  4. [root@bogon bbb]# ls
  5. test11.txt
复制代码

2、zip/unzip命令
案例:
将aaa文件下的test11.txt进行zip压缩成t200.zip,之后解压到bbb
  1. [root@bogon aaa]# zip t200.zip test11.txt
  2.   adding: test11.txt (deflated 100%)
  3. [root@bogon aaa]# ls
  4. t200.zip  test11.txt
  5. [root@bogon aaa]# ls -l
  6. 总用量 102500
  7. -rw-r--r--. 1 root root    101943 5月  14 19:16 t200.zip
  8. -rw-r--r--. 1 root root 104857600 5月  14 18:16 test11.txt
  9. [root@bogon aaa]# unzip t200.zip -d /root/bbb
  10. Archive:  t200.zip
  11.   inflating: /root/bbb/test11.txt   
  12. [root@bogon aaa]# cd /root/bbb
  13. [root@bogon bbb]# ls
  14. 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、正确重定向
覆盖重定向 >
追加重定向 >>
混合重定向 &>

感谢您的阅读,服务器大本营-技术文章内容集合站,助您成为更专业的服务器管理员!

一入IDC深似海,从此你我是良人!
您需要登录后才可以回帖 登录 | 我要入营

本版积分规则

点击直接加入[服务器大本营QQ频道]
滴!摸鱼时间到~
Loading...

QQ|Archiver|手机版|网站地图|服务器大本营 ( 赣ICP备2021009089号 )

GMT+8, 2025-7-26 18:07 , Processed in 0.064831 second(s), 27 queries , Gzip On.

Powered by 服务器大本营

© 2021-2023 联系飞飞

快速回复 返回顶部 返回列表