解析CentOS 7中系统文件与目录管理

Linux目录结构

Linux目录结构是树形的目录结构

根目录

  • 所有分区、目录、文件等的位置起点

  • 整个树形目录结构中,使用独立的一个“/”表示

常见的子目录

目录目录名称目录目录名称
/root管理员家目录/bin所有用户可执行命令文件目录
/boot系统内核、启动文件目录/dev设备文件
/etc配置文件/home用户家文件目录
/var变量文件(日志文件)/usr用户应用程序文件目录
/sbin管理员可执行的管理命令/proc硬件信息存放目录

查看及检索文件命令

cat命令

  • cat命令:显示并连接文件内容

  • 格式

​ cat [选项] 文件名 …

[root@localhost ~]# cat /mnt/tast02.txt          //输入命令,查看文件内容
this is tast02                                   //显示文件内容
[root@localhost ~]#

more 和 less 命令

  • more命令:全屏方式分页显示文件内容(当阅读完时自动退出阅读模式,不可直接回看)

  • 格式

​ more [选项] 文件名 ...

  • 交互操作方法

    按Enter键向下逐行滚动

    按空格键向下翻一屏

    按b键向上翻一屏

    按q键退出

[root@localhost ~]# more /etc/httpd/conf/httpd.conf 
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see 
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure
# consult the online docs. You have been warned.  
#
# Configuration and logfile names: If the filenames you specify for many
# of the server's control files beg...//以下省略内容...
  • less命令:与more命令相同,但扩展功能更过(阅读结束后可向上翻页,继续阅读)

  • 格式

    less [选项] 文件名 ...

  • 交互操作方法

    page up 向上翻页

    page down 向下翻页

    “/”键查找内容

    “n”下一个内容

    “N”上一个内容

    其他功能与more命令基本类似

[root@localhost ~]# less /etc/httpd/conf/httpd.conf 
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see 
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure
# consult the online ...//以下省略内容...

head和tail命令

  • head命令:查看文件开头部分内容(默认10行)

  • 格式

    head [选项] 文件名 ...

    命令字选项作用
    head查看文件开头一部分内容(默认10行)
    head-n(数字)查看头n行内容
    [root@localhost ~]# head /etc/passwd            //查看目录文件头10行内容
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin   //查看目录文件头3行内容
    [root@localhost ~]# head -3 /etc/passwd       
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    
  • tail命令:查看文件结尾的少部分内容(默认为10行)

  • 格式

    tail [选项] 文件名 ...

    命令字选项作用
    tail查看文件结尾的少部分内容(默认为10行)
    tail-n查看结尾n行内容
    tail-f跟踪文件尾部内容的动态更新(在公共日志等文件中用)
[root@localhost ~]# tail /etc/passwd                //查看目录文件结尾10行内容
setroubleshoot:x:993:988::/var/lib/setroubleshoot:/sbin/nologin
sssd:x:992:987:User for sssd:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:991:986::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
sun:x:1000:1000:sun:/home/sun:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@localhost ~]# tail -3 /etc/passwd            //查看目录文件结尾3行内容
tcpdump:x:72:72::/:/sbin/nologin
sun:x:1000:1000:sun:/home/sun:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@localhost ~]# 

统计文件内容命令

  • wc命令:统计文件中的单词数量等信息

  • 格式

    wc [选项]... 目标文件 ...

    命令字选项作用
    wc默认统计行数、单词数、字节数
    wc-l统计行数
    wc-w统计单词个数
    wc-c统计字节数
[root@localhost ~]# wc /etc/httpd/conf/httpd.conf       //统计文件行数、单词数、字节数
  353  1801 11753 /etc/httpd/conf/httpd.conf            //显示行数、单词数、字节数
[root@localhost ~]# wc -l /etc/httpd/conf/httpd.conf    //只统计文件行数
353 /etc/httpd/conf/httpd.conf                          //显示行数
[root@localhost ~]# wc -w /etc/httpd/conf/httpd.conf    //只统计文件单词数
1801 /etc/httpd/conf/httpd.conf                         //显示单词数
[root@localhost ~]# wc -c /etc/httpd/conf/httpd.conf    //只统计文件字节数
11753 /etc/httpd/conf/httpd.conf                        //显示字节数
[root@localhost ~]# 

检索和过滤文件内容命令

  • grep命令:在文件中查找并显示包含指定字符串的行

  • 格式

    grep [选项] 查找条件 目标文件

    命令字选项作用
    grep在目录中查找文件(正向查找,查找我们需要的信息)
    grep-i查找时忽略大小写
    grep-v反转查找,输出与查找条件不相符的行
  • 查找条件设置

    要查找的字符串以双引号括起来

    “^……“表示以……开头,”……$‘表示以……结尾

    “^$”表示空行

[root@localhost ~]# grep "ftp" /etc/passwd        //在passwd中查找“ftp”文件
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin       //查找出的文件信息
[root@localhost ~]# cd /etc/httpd/conf            //进入到conf文件目录
[root@localhost conf]# ls                         //查看目录内配置文件
httpd.conf  magic                                 //显示内容
[root@localhost conf]# grep -v "#" httpd.conf     //查找httpd.conf文件中不带#的文件
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache                                      //显示查找的内容
ServerAdmin root@localhost
//以下省略...
[root@localhost conf]# grep "bash$" /etc/passwd  //在passwd文件中查找以bash为结尾
root:x:0:0:root:/root:/bin/bash                    的文件
sun:x:1000:1000:sun:/home/sun:/bin/bash          //显示以bash为结尾的文件
[root@localhost conf]# grep "^n" /etc/passwd     //在passwd文件中查找以n为开头的文件
nobody:x:99:99:Nobody:/:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin              //显示以n为开头的文件内容

使用压缩和解压缩工具

  • gzip命令与bzip2命令

    gzip 制作的压缩文件默认的扩展名为“.gz”

    bzip2 制作的压缩文件默认的扩展名为“.bz2“

    (gzip格式 bzip2格式 创建压缩包或者解开压缩包原文件会消失)

  • 格式

    命令字选项作用
    gzip/bzip2-9压缩文件(命令字后面不输入选项时默认为 ”-9”)
    gzip/bzip2-d解压文件
[root@localhost conf]# cd /mnt                    //进入mnt文件目录
[root@localhost mnt]# ls                          //查看目录文件
demo02.txt  demo.jpg  tast01.txt  tast02.txt      //显示目录文件
[root@localhost mnt]# gzip -9 demo02.txt          //以gzip格式压缩demo02文件
[root@localhost mnt]# ls                          //查看
demo02.txt.gz  demo.jpg  tast01.txt  tast02.txt   //demo02为gz格式压缩文件
[root@localhost mnt]# gzip -d demo02.txt.gz       //解压demo02
[root@localhost mnt]# ls                          //查看
demo02.txt  demo.jpg  tast01.txt  tast02.txt      //demo02解压
[root@localhost mnt]# bzip2 -9 demo02.txt         //以bzip2格式压缩demo02文件
[root@localhost mnt]# ls                          //查看
demo02.txt.bz2  demo.jpg  tast01.txt  tast02.txt  //demo02为bz2格式压缩文件
[root@localhost mnt]# bzip2 -d demo02.txt.bz2     //解压demo02
[root@localhost mnt]# ls                          //查看
demo02.txt  demo.jpg  tast01.txt  tast02.txt      //demo02解压

归档命令

  • tar命令:制作归档文件、释放归档文件

  • 格式

    tar [选项] .... 归档文件名 源文件或目录

    tar [选项] ... 归档文件名 [-C 目标目录]

    命令字-选项作用
    tar-c创建压缩包
    tar-x解压
    tar-v显示详细信息
    tar-f执行
    tar-p保留原有权限
    tar-t查看压缩包内容
    tar-C解压目标路径
    tar-zgzip格式
    tar-jbzip2格式
[root@localhost mnt]# tar czvf demo.tar.gz demo02.txt tast01.txt 
demo02.txt                    //将demo02、tast02文件压缩为gz格式的压缩文件命令为demo
tast01.txt
[root@localhost mnt]# ls      //查看
demo02.txt  demo.jpg  demo.tar.gz  tast01.txt  tast02.txt  //显示demo压缩文件
[root@localhost mnt]# mkdir /data       //创建新文件夹data
[root@localhost mnt]# tar xzvf demo.tar.gz -C /data/
demo02.txt                    //将demo解压到新建的data文件夹
tast01.txt
[root@localhost mnt]# cd /data      //进入data文件就夹
[root@localhost data]# ls           //查看
demo02.txt  tast01.txt              //显示解压内容

补充知识点

  • ">":重定向符号的应用(输出重定向到一个文件或设备 覆盖原来的文件)
  • “>>”:输出重定向到一个文件或设备 追加原来的文件

  • “|”:管道符号(格式:命令A|命令B,即命令1的正确输出作为命令B的操作对象)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值