Linux目录结构

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

根目录

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

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

常见的子目录

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

查看及检索文件命令

cat命令

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

  • 格式

​ cat [选项] 文件名 …

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

more 和 less 命令

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

  • 格式

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

  • 交互操作方法

    按Enter键向下逐行滚动

    按空格键向下翻一屏

    按b键向上翻一屏

    按q键退出

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

  • 格式

    less [选项] 文件名 ...

  • 交互操作方法

    page up 向上翻页

    page down 向下翻页

    “/”键查找内容

    “n”下一个内容

    “N”上一个内容

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

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

head和tail命令

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

  • 格式

    head [选项] 文件名 ...

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

  • 格式

    tail [选项] 文件名 ...

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

统计文件内容命令

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

  • 格式

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

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

检索和过滤文件内容命令

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

  • 格式

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

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

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

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

    “^$”表示空行

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

使用压缩和解压缩工具

  • gzip命令与bzip2命令

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

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

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

  • 格式

    命令字 选项 作用
    gzip/bzip2 -9 压缩文件(命令字后面不输入选项时默认为 ”-9”)
    gzip/bzip2 -d 解压文件
  1. [root@localhost conf]# cd /mnt //进入mnt文件目录
  2. [root@localhost mnt]# ls //查看目录文件
  3. demo02.txt demo.jpg tast01.txt tast02.txt //显示目录文件
  4. [root@localhost mnt]# gzip -9 demo02.txt //以gzip格式压缩demo02文件
  5. [root@localhost mnt]# ls //查看
  6. demo02.txt.gz demo.jpg tast01.txt tast02.txt //demo02为gz格式压缩文件
  7. [root@localhost mnt]# gzip -d demo02.txt.gz //解压demo02
  8. [root@localhost mnt]# ls //查看
  9. demo02.txt demo.jpg tast01.txt tast02.txt //demo02解压
  10. [root@localhost mnt]# bzip2 -9 demo02.txt //以bzip2格式压缩demo02文件
  11. [root@localhost mnt]# ls //查看
  12. demo02.txt.bz2 demo.jpg tast01.txt tast02.txt //demo02为bz2格式压缩文件
  13. [root@localhost mnt]# bzip2 -d demo02.txt.bz2 //解压demo02
  14. [root@localhost mnt]# ls //查看
  15. 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 -z gzip格式
    tar -j bzip2格式
  1. [root@localhost mnt]# tar czvf demo.tar.gz demo02.txt tast01.txt
  2. demo02.txt //将demo02、tast02文件压缩为gz格式的压缩文件命令为demo
  3. tast01.txt
  4. [root@localhost mnt]# ls //查看
  5. demo02.txt demo.jpg demo.tar.gz tast01.txt tast02.txt //显示demo压缩文件
  6. [root@localhost mnt]# mkdir /data //创建新文件夹data
  7. [root@localhost mnt]# tar xzvf demo.tar.gz -C /data/
  8. demo02.txt //将demo解压到新建的data文件夹
  9. tast01.txt
  10. [root@localhost mnt]# cd /data //进入data文件就夹
  11. [root@localhost data]# ls //查看
  12. demo02.txt tast01.txt //显示解压内容

补充知识点

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

  • “>>”:输出重定向到一个文件或设备 追加原来的文件

  • “|”:管道符号(格式:命令A|命令B,即命令1的正确输出作为命令B的操作对象)

解析CentOS 7中系统文件与目录管理的更多相关文章

  1. Linux系统文件和目录管理

    Linux系统文件和目录管理 相关命令的解析 1.pwd:显示用户当前的工作目录 2.ls: -a:显示所有文件,包括隐藏文件 -l:显示文件的详细信息 3.设备文件统一存放在/dev 设备文件 块设 ...

  2. CentOS系统文件和目录管理相关的一些重要命令

    我们都知道,在Linux系统中,基本上任何我们需要做的事都可以通过输入命令来完成,所以在Linux系统中命令非常的多,我们不可能也没必要记住所有的这些命令,但是对于一些常用的命令我们还是必须要对其了如 ...

  3. Linux系统文件与目录管理(1)

    文件与目录的操作 在Linux系统的文件与目录的管理上,不外乎『显示属性』.『拷贝』.『删除文件』.『移动文件或目录』.『重命名』等常用操作,由于文件与目录的管理在 Linux当中是很重要的,尤其是每 ...

  4. Linux学习系列--如何在Linux中进行文件的管理

    文件 在常见的Linux的文件系统中,经常使用能了解到的文件管理系统是分为多个文件夹进行管理的. 如何查看文件路径 pwd ,在文件目录中,会有一个点(.)代表的是当前目录,两个点(..)代表的是当前 ...

  5. CentOS(十)--与Linux文件和目录管理相关的一些重要命令②

    在结束了第二期的广交会实习之后,又迎来了几天休闲的日子,继续学习Linux.在上一篇随笔 Linux学习之CentOS(十七)--与Linux文件和目录管理相关的一些重要命令① 中,详细记录了与Lin ...

  6. CentOS(九)--与Linux文件和目录管理相关的一些重要命令①

       接上一篇文章,实际生产过程中的目录管理一定要注意用户是root 还是其他用户. 一.目录与路径 1.相对路径与绝对路径 因为我们在Linux系统中,常常要涉及到目录的切换,所以我们必须要了解 & ...

  7. 【CentOS】文件与目录管理

    一.文件与目录管理 0.cd--change directory cd -  返回上次的目录 cd ~ 返回到家目录 --对于root用户来说是/root,对于普通用户来说是/home/用户名 cd ...

  8. CentOS学习笔记--基本命令--文件与目录管理

    Linux基本命令--文件与目录管理 本节节选自鸟哥的 Linux 私房菜 -- 基础学习篇目录  第七章.Linux 文件与目录管理  ls(文件与目录的检视) ls命令就是list的缩写,ls可以 ...

  9. 在 RedHat/CentOS 7.x 中使用 nmcli 命令管理网络

    在 RedHat/CentOS 7.x 中使用 nmcli 命令管理网络 学习了:https://linux.cn/article-5410-1.html#3_3613 http://www.linu ...

随机推荐

  1. jQuery根据style筛选元素

    <div style="display:block;"> <input/> </div> <div style="display ...

  2. ABAP DEMO so批量导入

    *&---------------------------------------------------------------------* *& Report YDEMO_015 ...

  3. Python - Django - 添加首页尾页上一页下一页

    添加首页和尾页: views.py: from django.shortcuts import render from app01 import models def book_list(reques ...

  4. 改进初学者的PID-介绍

    最近看到了Brett Beauregard发表的有关PID的系列文章,感觉对于理解PID算法很有帮助,于是将系列文章翻译过来!在自我提高的过程中,也希望对同道中人有所帮助.作者Brett Beaure ...

  5. Redis应用场景梳理

    缓存 作为Key-Value形态的内存数据库,Redis 最先会被想到的应用场景便是作为数据缓存.而使用 Redis 缓存数据非常简单,只需要通过string类型将序列化后的对象存起来即可,不过也有一 ...

  6. C#获取IP及MAC地址 方法

    C#获取IP及MAC地址 方法,比较齐全 using System.Net; using System; using System.Management; using System.Runtime.I ...

  7. canvas《砸肉蛋》

    计划今年要自己写几个游戏的demo,先从<砸地鼠>这种简单的入手. 游戏思路 卡通化前端组头像 两种状态(快乐&被砸) 游戏时间1分钟 微信接口,分享最终得分 游戏规则 前端组的肉 ...

  8. linux如何查看mysql是否启动

    linux下有很多服务,今天就写一下如何查看服务是否启动,以mysql为例子 使用命令 # service mysqld status 或者 # service mysql status 命令来查看m ...

  9. twemproxy配置

    redis多主从,多节点,读写分离架构. nutcracker.yml的twemproxy配置 #redis_main是twemproxy所控制redis主从集群逻辑名称 redis_main: #t ...

  10. C++语法笔记(上)

    客观事物中任何一个事物都可以看成一个对象,对象是由一组属性和一组行为构成的. c++中,每个对象都是由数据与函数这两部分构成,数据就是对象的属性,函数就是对象的行为. c++中对象的类型称为类,类是一 ...