文件管理基础

管理文件与目录

1、Linux的目录结构(单根结构

  整个系统的目录结构是一个树形结构,这种结构类似于一棵倒立的树,有一个根节点,然后向下延伸出多个子节点,每个子节点又可以继续延伸出更多的子节点。在Linux系统中,这个根节点用斜杠“/”表示。从这个根节点开始,系统中的所有文件和目录都被组织在这个树形结构中。

  某一个目录肯定要关联上磁盘空间才能往里面存东西,但不是说所有的目录都要关联磁盘空间。目录结构是目录结构,磁盘空间是磁盘空间,他们两者本身是没任何关联关系。目录结构是给系统的使用者用的,目录结构里放着文件,文件是操作硬盘的机制,那就意味着在往某个目录结构里放东西的时候其实是往硬盘里放东西。所以我们要建立好硬盘和目录结构他们之间的关联关系的。

  为什么要设计成这种树形结构呢?目的是非常清晰的帮你把你的文件给组织起来,不同的文件放在不同的文件夹里面。这种结构使得文件和目录的管理变得有序和高效。用户可以根据文件的类型、用途或项目等不同维度进行分类存放,从而方便地查找和管理文件。例如,系统相关的配置文件通常放在“/etc”目录下,而用户的数据文件则放在“/home”目录下。此外,树形结构还支持权限管理,可以通过设置不同目录和文件的权限来控制用户对文件和目录的访问。

  在Linux系统中,还有一些特殊的目录和文件,如“/dev”目录用于存放设备文件,“/proc”目录则是一个虚拟文件系统,用于提供运行系统的信息。这些目录和文件都是系统运行所必需的,它们在树形目录结构中各自占据着特定的位置,共同构成了Linux系统的完整目录结构。

  综上所述,Linux的树形目录结构不仅是一种组织文件的方式,更是系统高效运行和管理的基石。通过合理地利用这种结构,用户可以更好地管理自己的文件和数据,同时也能够更好地理解和维护整个Linux系统。

  在操作文件的时候,归根结底是要去操作硬盘空间的,这就需要你将目录结构与硬盘的某个分区建立好关联才行。这个过程实际上是在文件系统层面上进行的,它确保了你的数据能够被准确地存储在硬盘的特定区域。建立起这种关联关系之后,当你往某一个文件夹里存放东西时,这些数据才会被实际写入到某个硬盘的分区中。这种机制对于数据的组织和管理至关重要,它保证了文件操作的效率和可靠性。

  只要是文件,他一定是属于树的某一个节点。

Linux系统当中一切皆文件!

 

[root@localhost ~]# ls /
a    boot  et   home  lib64  mnt  proc  run   srv  test  usr
bin  dev   etc  lib   media  opt  root  sbin  sys  tmp   var
ls /
[root@localhost ~]# ls /etc
adjtime                  ethertypes    libaudit.conf             popt.d          ssl
aliases                  exports       libnl                     postfix         statetab
aliases.db               favicon.png   libuser.conf              ppp             statetab.d
alternatives             filesystems   locale.conf               prelink.conf.d  subgid
anacrontab               firewalld     localtime                 printcap        subgid-
asound.conf              fstab         login.defs                profile         subuid
audisp                   fuse.conf     logrotate.conf            profile.d       subuid-
audit                    gcrypt        logrotate.d               protocols       sudo.conf
bash_completion.d        gnupg         lvm                       python          sudoers
bashrc                   GREP_COLORS   machine-id                rc0.d           sudoers.d
binfmt.d                 groff         magic                     rc1.d           sudo-ldap.conf
centos-release           group         mailcap                   rc2.d           sysconfig
centos-release-upstream  group-        makedumpfile.conf.sample  rc3.d           sysctl.conf
chkconfig.d              grub2.cfg     man_db.conf               rc4.d           sysctl.d
chrony.conf              grub.d        mime.types                rc5.d           systemd
chrony.keys              gshadow       mke2fs.conf               rc6.d           system-release
cron.d                   gshadow-      modprobe.d                rc.d            system-release-cpe
cron.daily               gss           modules-load.d            rc.local        terminfo
cron.deny                host.conf     motd                      redhat-release  tmpfiles.d
cron.hourly              hostname      mtab                      resolv.conf     tuned
cron.monthly             hosts         my.cnf                    rpc             udev
crontab                  hosts.allow   my.cnf.d                  rpm             vconsole.conf
cron.weekly              hosts.deny    NetworkManager            rsyslog.conf    vimrc
crypttab                 httpd         networks                  rsyslog.d       virc
csh.cshrc                init.d        nsswitch.conf             rwtab           vmware-tools
csh.login                inittab       nsswitch.conf.bak         rwtab.d         wgetrc
dbus-1                   inputrc       openldap                  sasl2           wpa_supplicant
default                  iproute2      opt                       securetty       X11
depmod.d                 issue         os-release                security        xdg
dhcp                     issue.net     pam.d                     selinux         xinetd.d
DIR_COLORS               kdump.conf    passwd                    services        yum
DIR_COLORS.256color      kernel        passwd-                   sestatus.conf   yum.conf
DIR_COLORS.lightbgcolor  krb5.conf     pkcs11                    shadow          yum.repos.d
dracut.conf              krb5.conf.d   pki                       shadow-
dracut.conf.d            ld.so.cache   plymouth                  shells
e2fsck.conf              ld.so.conf    pm                        skel
environment              ld.so.conf.d  polkit-1                  ssh
ls /etc
[root@localhost ~]# ls /etc/rc.d
init.d  rc0.d  rc1.d  rc2.d  rc3.d  rc4.d  rc5.d  rc6.d  rc.local
ls /etc/rc.d

 

2、路径(path)

  目录结构背后是关联好磁盘空间,意味着操作目录(往目录里放文件或者删文件)其实背后对应的都是操作某一个硬盘分区。

  我们在用目录是就是在用目录里的某一个文件,但凡是文件都有路径。

  有两种路径:绝对路径(absolute)和相对路径(relative)。

  绝对路径:

    由根目录(/)开始写起的文件名或目录名称,例如/a/b/c/1.txt;

    特点:无论当前你在哪个目录下,用绝对路径肯定能找到目标文件。

  相对路径:

    相对于目前路径的文件名写法,例如./home/dmtsai或../../home/dmtsai/等,反正开头不是/就属于相对路径的写法。

    特点:基于当前文件夹往后查找。

    cd /a/b/

    c/1.txt

  相对路径是以你目前所在路径的相对位置来表示的。举例来说,你目前在/home这个目录下,如果想要进入/var/log这个目录,可以这么写:

  1、cd /var/log (absolute)

  2、cd ../var/log (relative)

 

Windows的文件夹不区分大小写,而Linux的文件夹区分大小写。

\n会直接被识别成换行符,

  特殊符号:

       代表当前目录  例如 ./b/c/1.txt  路径当中只有最开头那个/代表文件夹的意思,后面每一个/都是路径分割符的意思

    ..    代表上一级目录

    ../../../a.txt

    ~  当前所登录用户的家目录(主目录)

      root用户---->/root

      普通用户---->/home/用户名

[root@localhost ~]# pwd
/root
[root@localhost ~]# ls
1.txt  anaconda-ks.cfg  nginx-1.28.0.tar.gz  wahaha  wahaha.txt
[root@localhost ~]# ls .
1.txt  anaconda-ks.cfg  nginx-1.28.0.tar.gz  wahaha  wahaha.txt
[root@localhost ~]# ls ~
1.txt  anaconda-ks.cfg  nginx-1.28.0.tar.gz  wahaha  wahaha.txt
[root@localhost ~]# 

  ~还可以代表指定用户名 ls ~zrg 就是指定查看用户zrg的家目录。

如何进入/var/spool/mail目录,再进入到/var/spool/cron目录中呢?
由于/var/spool/mail与/var/spool/cron时同样在/var/spool/目录中,因此最简单的命令执行方法为:
    1. cd /var/spool/mail
    2. cd ../cron

 

3、文件类型

Linux系统当中一切皆文件,所有东西都是文件,系统当中管理的各种设备...等等都是文件,但是给你展示的时候不能都是同一种种类的文件,分了不同的种类:

  ll /etc/hosts  查看根路径下etc下hosts文件的详细信息

[root@localhost ~]# ll /etc/hosts
-rw-r--r--. 1 root root 158 6月   7 2013 /etc/hosts

 

-代表是普通文件,包含文本文件、图片、视频、

在Windows中文件的类型通常由后缀名决定,而在Linux中文件的后缀名的作用则没有如此强大。当然在Linux中文件的扩展名也遵循一些约定,如压缩文件一般用“.zip”,RPM软件包一般用“.rpm”,TAR归档包一般用“.tar”,GZIP压缩文件一般用“.gz”等。
Linux的文件类型一般由创建文件的命令来决定。
文件类型Windows和Linux的区别
常见文件类型
文件类型 符号 说明
普通文件 - 文本文件和二进制文件(图片、视频),一般的文档和程序都属于普通文件。一般由编辑器或cp、touch等命令创建,使用rm命令删除
目录文件 d 本质就是文件夹内。主要用于管理和组织系统中的大量文件。一般由mkdir命令创建,使用rmdir、rm -r命令删除
链接文件 l 软链接,就相当于Windows系统中的快捷方式。一般由 ln -s命令创建。使用rm命令删除
块设备文件 b 代表的是block块设备文件,由mknod命令创建。使用rm命令删除。 
本地套接字 s 本地套接字由系统调用socket创建,套接字不再有用户使用时,系统会调用unlink命令来删除

 

b 代表的是block块设备文件,存储设备硬盘,u盘,比如:/dev/sda

  站在硬盘的角度最小读写单位是一个扇区

  站在操作系统的角度最小的读写单位默认是一个block块,一个block块默认是由8个扇区组成。 

c 代表设备文件(字符设备)里面的串行端口设备比如打印机、键盘、鼠标(一次性读取设备);终端/dev/tty1

 

4、Linux系统各个目录的作用

  我们用操作系统有点其实就是一堆文件,文件肯定会组织到不同的目录里面去了。

[root@localhost ~]# ll /
总用量 20
lrwxrwxrwx.   1 root root    7 5月  11 05:40 bin -> usr/bin              /bin  放经常使用的命令   usr/bin   放一些权限的一些命令
dr-xr-xr-x.   5 root root 4096 5月  11 05:43 boot               /boot  内核及加载内核所需的文件
drwxr-xr-x.  19 root root 3180 5月  24 13:25 dev                /dev   设备文件
drwxr-xr-x.  76 root root 8192 5月  24 13:25 etc                /etc  启动文件及配置文件
drwxr-xr-x.   5 root root   52 5月  21 00:20 home                /home用户的主目录,每一个用户都有一个自己的主目录,目录名与账号名相同。
lrwxrwxrwx.   1 root root    7 5月  11 05:40 lib -> usr/lib          
lrwxrwxrwx.   1 root root    9 5月  11 05:40 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 4月  11 2018 media
drwxr-xr-x.   2 root root    6 4月  11 2018 opt
dr-xr-xr-x. 109 root root    0 5月  24 13:25 proc              伪文件系统,用于存放正在运行的进程的映像
dr-xr-x---.   4 root root  235 5月  23 01:54 root                超级用户的主目录
drwxr-xr-x.  26 root root  780 5月  24 13:25 run
lrwxrwxrwx.   1 root root    8 5月  11 05:40 sbin -> usr/sbin              存放超级管理员的权限命令。
drwxr-xr-x.   2 root root    6 4月  11 2018 srv
dr-xr-xr-x.  13 root root    0 5月  24 13:25 sys
drwxrwxrwt.  16 root root 4096 5月  25 04:00 tmp        /tmp相当于Windows系统的回收站
drwxr-xr-x.  13 root root  155 5月  11 05:40 usr
drwxr-xr-x.  20 root root  278 5月  22 14:21 var       不断变化的比如日志文件            

 

 

[root@localhost ~]# ll /etc
总用量 1120
-rw-r--r--.  1 root root       16 5月  11 05:43 adjtime
-rw-r--r--.  1 root root     1529 4月   1 2020 aliases
-rw-r--r--.  1 root root    12288 5月  11 05:44 aliases.db
drwxr-xr-x.  2 root root      236 5月  11 05:41 alternatives
-rw-------.  1 root root      541 8月   9 2019 anacrontab
-rw-r--r--.  1 root root       55 8月   8 2019 asound.conf
drwxr-x---.  3 root root       43 5月  11 05:41 audisp
drwxr-x---.  3 root root       83 5月  11 05:44 audit
drwxr-xr-x.  2 root root      141 5月  23 00:08 bash_completion.d
-rw-r--r--.  1 root root     2853 4月   1 2020 bashrc
drwxr-xr-x.  2 root root        6 10月  2 2020 binfmt.d
-rw-r--r--.  1 root root       37 10月 23 2020 centos-release
-rw-r--r--.  1 root root       51 10月 23 2020 centos-release-upstream
drwxr-xr-x.  2 root root        6 10月 13 2020 chkconfig.d
-rw-r--r--.  1 root root     1108 8月   8 2019 chrony.conf
-rw-r-----.  1 root chrony    481 8月   8 2019 chrony.keys
drwxr-xr-x.  2 root root       21 5月  11 05:41 cron.d
drwxr-xr-x.  2 root root       42 5月  11 05:41 cron.daily
-rw-------.  1 root root        0 8月   9 2019 cron.deny
drwxr-xr-x.  2 root root       22 6月  10 2014 cron.hourly
drwxr-xr-x.  2 root root        6 6月  10 2014 cron.monthly
-rw-r--r--.  1 root root      451 6月  10 2014 crontab
drwxr-xr-x.  2 root root        6 6月  10 2014 cron.weekly
-rw-------.  1 root root        0 5月  11 05:40 crypttab
-rw-r--r--.  1 root root     1620 4月   1 2020 csh.cshrc
-rw-r--r--.  1 root root     1103 4月   1 2020 csh.login
drwxr-xr-x.  4 root root       78 5月  11 05:40 dbus-1
drwxr-xr-x.  2 root root       44 5月  23 01:42 default
drwxr-xr-x.  2 root root       23 5月  11 05:40 depmod.d
drwxr-x---.  4 root root       53 5月  11 05:41 dhcp
-rw-r--r--.  1 root root     5090 8月   6 2019 DIR_COLORS
-rw-r--r--.  1 root root     5725 8月   6 2019 DIR_COLORS.256color
-rw-r--r--.  1 root root     4669 8月   6 2019 DIR_COLORS.lightbgcolor
-rw-r--r--.  1 root root     1285 9月  30 2020 dracut.conf
drwxr-xr-x.  2 root root        6 9月  30 2020 dracut.conf.d
-rw-r--r--.  1 root root      112 9月  30 2020 e2fsck.conf
-rw-r--r--.  1 root root        0 4月   1 2020 environment
-rw-r--r--.  1 root root     1317 4月  11 2018 ethertypes
-rw-r--r--.  1 root root        0 6月   7 2013 exports
lrwxrwxrwx.  1 root root       56 5月  11 05:40 favicon.png -> /usr/share/icons/hicolor/16x16/apps/fedora-logo-icon.png
-rw-r--r--.  1 root root       70 4月   1 2020 filesystems
drwxr-x---.  7 root root      133 5月  11 05:41 firewalld
-rw-r--r--.  1 root root      501 5月  11 05:40 fstab
-rw-r--r--.  1 root root       38 10月 30 2018 fuse.conf
drwxr-xr-x.  2 root root        6 8月   2 2017 gcrypt
drwxr-xr-x.  2 root root        6 7月  13 2018 gnupg
-rw-r--r--.  1 root root       94 3月  25 2017 GREP_COLORS
drwxr-xr-x.  4 root root       40 5月  11 05:40 groff
-rw-r--r--.  1 root root      537 5月  22 14:21 group
-rw-r--r--.  1 root root      524 5月  21 00:20 group-
lrwxrwxrwx.  1 root root       22 5月  11 05:41 grub2.cfg -> ../boot/grub2/grub.cfg
drwx------.  2 root root      182 5月  11 05:41 grub.d
----------.  1 root root      422 5月  22 14:21 gshadow
----------.  1 root root      411 5月  21 00:20 gshadow-
drwxr-xr-x.  3 root root       20 12月  1 2022 gss
-rw-r--r--.  1 root root        9 6月   7 2013 host.conf
-rw-r--r--.  1 root root       22 5月  11 05:43 hostname
-rw-r--r--.  1 root root      158 6月   7 2013 hosts
-rw-r--r--.  1 root root      370 6月   7 2013 hosts.allow
-rw-r--r--.  1 root root      460 6月   7 2013 hosts.deny
drwxr-xr-x.  5 root root       92 5月  22 14:21 httpd
lrwxrwxrwx.  1 root root       11 5月  11 05:40 init.d -> rc.d/init.d
-rw-r--r--.  1 root root      511 10月 13 2020 inittab
-rw-r--r--.  1 root root      942 6月   7 2013 inputrc
drwxr-xr-x.  2 root root      159 5月  11 05:40 iproute2
-rw-r--r--.  1 root root       23 10月 23 2020 issue
-rw-r--r--.  1 root root       22 10月 23 2020 issue.net
-rw-r--r--.  1 root root     7274 5月  11 05:41 kdump.conf
drwxr-xr-x.  3 root root       24 5月  11 05:41 kernel
-rw-r--r--.  1 root root      646 1月  14 2022 krb5.conf
drwxr-xr-x.  2 root root        6 12月  1 2022 krb5.conf.d
-rw-r--r--.  1 root root    23148 5月  24 03:18 ld.so.cache
-rw-r--r--.  1 root root       28 2月  28 2013 ld.so.conf
drwxr-xr-x.  2 root root      106 6月   4 2024 ld.so.conf.d
-rw-r-----.  1 root root      191 3月   2 2019 libaudit.conf
drwxr-xr-x.  2 root root       35 5月  11 05:40 libnl
-rw-r--r--.  1 root root     2391 10月 13 2013 libuser.conf
-rw-r--r--.  1 root root       19 5月  11 05:43 locale.conf
lrwxrwxrwx.  1 root root       35 5月  11 05:43 localtime -> ../usr/share/zoneinfo/Asia/Shanghai
-rw-r--r--.  1 root root     2027 8月   6 2019 login.defs
-rw-r--r--.  1 root root      662 7月  31 2013 logrotate.conf
drwxr-xr-x.  2 root root      112 5月  22 14:21 logrotate.d
drwxr-xr-x.  6 root root      100 5月  24 03:18 lvm
-r--r--r--.  1 root root       33 5月  11 05:40 machine-id
-rw-r--r--.  1 root root      111 10月  1 2020 magic
-rw-r--r--.  1 root root      272 5月  15 2013 mailcap
-rw-r--r--.  1 root root     5122 10月  2 2020 makedumpfile.conf.sample
-rw-r--r--.  1 root root     5171 10月 31 2018 man_db.conf
-rw-r--r--.  1 root root    51787 5月  15 2013 mime.types
-rw-r--r--.  1 root root     1106 9月  30 2020 mke2fs.conf
drwxr-xr-x.  2 root root       81 5月  11 05:41 modprobe.d
drwxr-xr-x.  2 root root        6 10月  2 2020 modules-load.d
-rw-r--r--.  1 root root        0 6月   7 2013 motd
lrwxrwxrwx.  1 root root       17 5月  11 05:40 mtab -> /proc/self/mounts
-rw-r--r--.  1 root root      570 9月  30 2020 my.cnf
drwxr-xr-x.  2 root root       31 5月  11 05:40 my.cnf.d
drwxr-xr-x.  7 root root      134 5月  11 05:41 NetworkManager
-rw-r--r--.  1 root root       58 10月 13 2020 networks
-rw-r--r--.  1 root root     1949 5月  11 05:40 nsswitch.conf
-rw-r--r--.  1 root root     1938 9月  30 2020 nsswitch.conf.bak
drwxr-xr-x.  3 root root       36 5月  11 05:40 openldap
drwxr-xr-x.  2 root root        6 4月  11 2018 opt
lrwxrwxrwx.  1 root root       21 5月  11 05:40 os-release -> ../usr/lib/os-release
drwxr-xr-x.  2 root root     4096 5月  11 05:43 pam.d
-rw-r--r--.  1 root root     1036 5月  22 14:21 passwd
-rw-r--r--.  1 root root      983 5月  21 00:20 passwd-
drwxr-xr-x.  3 root root       21 5月  11 05:40 pkcs11
drwxr-xr-x. 10 root root      116 5月  11 05:41 pki
drwxr-xr-x.  2 root root       28 5月  11 05:41 plymouth
drwxr-xr-x.  5 root root       52 5月  11 05:40 pm
drwxr-xr-x.  5 root root       72 5月  11 05:40 polkit-1
drwxr-xr-x.  2 root root        6 6月  10 2014 popt.d
drwxr-xr-x.  2 root root      154 5月  11 05:41 postfix
drwxr-xr-x.  3 root root      123 5月  11 05:41 ppp
drwxr-xr-x.  2 root root       78 5月  11 05:41 prelink.conf.d
-rw-r--r--.  1 root root      233 6月   7 2013 printcap
-rw-r--r--.  1 root root     1819 4月   1 2020 profile
drwxr-xr-x.  2 root root      280 5月  14 13:17 profile.d
-rw-r--r--.  1 root root     6545 4月   1 2020 protocols
drwxr-xr-x.  2 root root       35 5月  11 05:40 python
lrwxrwxrwx.  1 root root       10 5月  11 05:40 rc0.d -> rc.d/rc0.d
lrwxrwxrwx.  1 root root       10 5月  11 05:40 rc1.d -> rc.d/rc1.d
lrwxrwxrwx.  1 root root       10 5月  11 05:40 rc2.d -> rc.d/rc2.d
lrwxrwxrwx.  1 root root       10 5月  11 05:40 rc3.d -> rc.d/rc3.d
lrwxrwxrwx.  1 root root       10 5月  11 05:40 rc4.d -> rc.d/rc4.d
lrwxrwxrwx.  1 root root       10 5月  11 05:40 rc5.d -> rc.d/rc5.d
lrwxrwxrwx.  1 root root       10 5月  11 05:40 rc6.d -> rc.d/rc6.d
drwxr-xr-x. 10 root root      127 5月  11 05:40 rc.d
lrwxrwxrwx.  1 root root       13 5月  11 05:40 rc.local -> rc.d/rc.local
lrwxrwxrwx.  1 root root       14 5月  11 05:40 redhat-release -> centos-release
-rw-r--r--.  1 root root       78 5月  24 13:25 resolv.conf
-rw-r--r--.  1 root root     1634 12月 25 2012 rpc
drwxr-xr-x.  2 root root       44 5月  14 13:17 rpm
-rw-r--r--.  1 root root     3232 10月 13 2020 rsyslog.conf
drwxr-xr-x.  2 root root       25 10月 14 2020 rsyslog.d
-rw-r--r--.  1 root root      986 10月 13 2020 rwtab
drwxr-xr-x.  2 root root       23 10月 13 2020 rwtab.d
drwxr-xr-x.  2 root root       24 5月  11 05:41 sasl2
-rw-------.  1 root root      221 4月   1 2020 securetty
drwxr-xr-x.  6 root root     4096 5月  11 05:40 security
drwxr-xr-x.  5 root root       81 5月  11 05:41 selinux
-rw-r--r--.  1 root root   670293 6月   7 2013 services
-rw-r--r--.  1 root root      216 4月   1 2020 sestatus.conf
----------.  1 root root      890 5月  22 14:21 shadow
----------.  1 root root      868 5月  21 00:23 shadow-
-rw-r--r--.  1 root root       44 4月   1 2020 shells
drwxr-xr-x.  2 root root       62 5月  11 05:40 skel
drwxr-xr-x.  2 root root      225 5月  11 05:44 ssh
drwxr-xr-x.  2 root root       19 5月  11 05:40 ssl
-rw-r--r--.  1 root root      212 10月 13 2020 statetab
drwxr-xr-x.  2 root root        6 10月 13 2020 statetab.d
-rw-r--r--.  1 root root       64 5月  21 00:20 subgid
-rw-r--r--.  1 root root       42 5月  20 20:51 subgid-
-rw-r--r--.  1 root root       64 5月  21 00:20 subuid
-rw-r--r--.  1 root root       42 5月  20 20:51 subuid-
-rw-r-----.  1 root root     1786 9月  30 2020 sudo.conf
-r--r-----.  1 root root     4328 5月  21 18:31 sudoers
drwxr-x---.  2 root root        6 10月  1 2020 sudoers.d
-rw-r-----.  1 root root     3181 9月  30 2020 sudo-ldap.conf
drwxr-xr-x.  6 root root     4096 5月  22 14:21 sysconfig
-rw-r--r--.  1 root root      449 10月 13 2020 sysctl.conf
drwxr-xr-x.  2 root root       28 5月  11 05:41 sysctl.d
drwxr-xr-x.  4 root root      151 5月  11 05:40 systemd
lrwxrwxrwx.  1 root root       14 5月  11 05:40 system-release -> centos-release
-rw-r--r--.  1 root root       23 10月 23 2020 system-release-cpe
drwxr-xr-x.  2 root root        6 9月   7 2017 terminfo
drwxr-xr-x.  2 root root        6 10月  2 2020 tmpfiles.d
drwxr-xr-x.  3 root root      109 5月  11 05:41 tuned
drwxr-xr-x.  3 root root       54 5月  11 05:44 udev
-rw-r--r--.  1 root root       37 5月  11 05:43 vconsole.conf
-rw-r--r--.  1 root root     1982 12月 16 2020 vimrc
-rw-r--r--.  1 root root     1982 10月 14 2020 virc
drwxr-xr-x.  4 root root      208 5月  11 05:41 vmware-tools
-rw-r--r--.  1 root root     4479 5月  16 2019 wgetrc
drwxr-xr-x.  2 root root       33 5月  11 05:41 wpa_supplicant
drwxr-xr-x.  5 root root       57 5月  11 05:40 X11
drwxr-xr-x.  4 root root       38 5月  11 05:40 xdg
drwxr-xr-x.  2 root root        6 4月  11 2018 xinetd.d
drwxr-xr-x.  6 root root      100 5月  22 12:43 yum
-rw-r--r--.  1 root root      970 10月  2 2020 yum.conf
drwxr-xr-x.  3 root root       83 5月  22 21:23 yum.repos.d
ll /etc
/etc下各个文件的作用
fstab   系统挂载目录  开机自动挂载列表
hostname  系统主机名配置文件
passwd    系统用户文件
resolv.conf   dns客户端配置文件
hosts          本地域名解析配置文件
sysconfig/network-scripts/  网络配置文件目录,具体的网卡配置
   
[root@localhost ~]# ll /var
total 12
drwxr-xr-x.  2 root root    6 May 16  2022 adm
drwxr-xr-x.  8 root root   88 May  9 04:13 cache
drwxr-xr-x.  2 root root    6 Nov  1  2023 crash
drwxr-xr-x.  3 root root   18 May  9 04:13 db
drwxr-xr-x.  2 root root    6 May 16  2022 empty
drwxr-xr-x.  2 root root    6 May 16  2022 ftp
drwxr-xr-x.  2 root root    6 May 16  2022 games
drwxr-xr-x.  3 root root   18 May  9 04:12 kerberos
drwxr-xr-x. 22 root root 4096 May 24 16:00 lib
drwxr-xr-x.  2 root root    6 May 16  2022 local
lrwxrwxrwx.  1 root root   11 May  9 04:12 lock -> ../run/lock
drwxr-xr-x.  8 root root 4096 May 25 00:00 log
lrwxrwxrwx.  1 root root   10 May 16  2022 mail -> spool/mail
drwxr-xr-x.  2 root root    6 May 16  2022 nis
drwxr-xr-x.  2 root root    6 May 16  2022 opt
drwxr-xr-x.  2 root root    6 May 16  2022 preserve
lrwxrwxrwx.  1 root root    6 May  9 04:12 run -> ../run
drwxr-xr-x.  6 root root   56 May  9 04:12 spool
drwxrwxrwt.  6 root root 4096 May 25 00:00 tmp
drwxr-xr-x.  2 root root    6 May 16  2022 yp
ll /var

/var/log/messages    系统日志   不建议用cat命令查看日志。

 

5、文件管理相关命令:

  基础命令:

    查看当前所在的文件夹

    pwd

    切换目录

    cd /etc/sysconfig    跟绝对路径。

    cd 跟相对路径,得先切到某个路径下面,然后再以他作为基础再往后去切。

    cd ~等同于cd 什么都不跟。

    cd -  切换到上一次在的目录。

    查看目录层级结构

    tree 跟文件或目录,查看指定的

    tree -a  显示所有的文件  在Linux系统在.开头的文件是隐藏文件

    tree -d  只显示目录

[root@localhost ~]# tree -d /tmp
/tmp
├── systemd-private-2b5066a0561b4ba9a56021e6fba83368-chronyd.service-CxB8CQ
│   └── tmp
├── systemd-private-74600f829382420d9407ee0fcdee8f8e-chronyd.service-cL9dzQ
│   └── tmp
├── systemd-private-97baadf9dfb64310990d90601a158176-chronyd.service-BhKFJO
│   └── tmp
├── systemd-private-c76feef49911405c87543bb94e2655cc-chronyd.service-uQHZSN
│   └── tmp
├── systemd-private-df6975f14d0c498c8907f50d2377a973-chronyd.service-dIExbV
│   └── tmp
├── vmware-root_524-2965513588
├── vmware-root_535-4290690870
├── vmware-root_541-4248680634
└── vmware-root_549-4248811676
tree -d /tmp

    tree F   针对目录会在结尾加/

    tree -L 1    指定层级    

    查看当前目录

      在命令行模式下确认当前所使用的目录的绝对路径非常重要,可以使用pwd命令来检查当前工作目录名称。

    ls -l  查看的的是文件的元数据。

    对于系统在一个文件来讲,他应该包含两部分数据:文件的元数据(文件名字、类型、权限、所属主/组)和文件的内容部分。

    ll -h  文件大小带着单位

    ls -a  显示当前目录下的所有文件和目录。

    ls -d -l /tmp  只想看tmp本身。查看某个目录本身的元数据。

    ls -d -l /tmp/  看tmp下的。

 

 

[root@localhost ~]# ll 1.txt
-rw-r--r--. 1 zrg group1 0 5月  20 20:50 1.txt
- 文件的类型
rw-r--r-- 文件的权限
.   代表该文件是在开启selinux的情况下创键    查看getenforce  
1   代表的是硬链接数
zrg   属主
group1 属组
0   文件的大小
5月  20 20:50  文件的内容修改时间
1.txt  文件名

 

 

 文件的三种时间(mtime、atime、ctime)

  stat 跟上文件名字

[root@localhost ~]# stat 1.txt
  文件:"1.txt"
  大小:0             块:0          IO 块:4096   普通空文件
设备:803h/2051d    Inode:33581388    硬链接:1
权限:(0644/-rw-r--r--)  Uid:( 1001/     zrg)   Gid:( 1003/  group1)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2025-05-20 20:50:25.063399986 +0800
最近更改:2025-05-20 20:50:25.063399986 +0800
最近改动:2025-05-20 20:54:05.090808784 +0800
创建时间:-

  atime对应的就是最近访问时间,查看一次文件内容就是变一次。

  mtime对应就是最近更改时间,文件内容改变该时间才会变

  ctime对应的就是最近改动时间,文件只要变他就跟着变,一个文件数据包含两部分:元数据+文件内容。

 

6、文件管理常规命令 

  我们学习的文件管理命令主要是两种,一种类型是横杠-(普通文件),另一种类型是d(目录)。

  创建普通文件、目录文件

创建文件命令 touch

  touch  <文件名>

touch命令创建出的文件不区分文本文件、图片文件和音频文件。

在/tmp的目录下创建文件:

touch /tmp/222.txt
touch /tmp/1.jpg
touch /tmp/1.mp4

 创建多个文件 ..是一个范围,看也可以写逗号意思是自己指定有几个{z,A,Z,R,G}

touch /tmp/{1..10}.txt

我们可以看到结果:创键的都是普通文件!!!!

[root@localhost ~]# touch /tmp/222.txt
[root@localhost ~]# touch /tmp/1.jpg
[root@localhost ~]# touch /tmp/1.mp4
[root@localhost ~]# touch /tmp/{1..10}.txt
[root@localhost ~]# ll /tmp/
总用量 0
-rw-r--r--. 1 root root  0 5月  25 19:36 10.txt
-rw-r--r--. 1 root root  0 5月  25 19:35 1.jpg
-rw-r--r--. 1 root root  0 5月  25 19:35 1.mp4
-rw-r--r--. 1 root root  0 5月  25 19:36 1.txt
-rw-r--r--. 1 root root  0 5月  25 19:35 222.txt
-rw-r--r--. 1 root root  0 5月  25 19:36 2.txt
-rw-r--r--. 1 root root  0 5月  25 19:36 3.txt
-rw-r--r--. 1 root root  0 5月  25 19:36 4.txt
-rw-r--r--. 1 root root  0 5月  25 19:36 5.txt
-rw-r--r--. 1 root root  0 5月  25 19:36 6.txt
-rw-r--r--. 1 root root  0 5月  25 19:36 7.txt
-rw-r--r--. 1 root root  0 5月  25 19:36 8.txt
-rw-r--r--. 1 root root  0 5月  25 19:36 9.txt

  创建目录

创建目录一般用mkdir命令。

如果带路径创键,一定要确定父级目录一定存在!!

mkdir /tmp/a

=p递归创建:

mkdir -p /tmp/a/b/c

 创键多个

mkdir /tmp/test{1..5}

结果:

[root@localhost ~]# mkdir /tmp/a
[root@localhost ~]# ll /tmp/
total 0
-rw-r--r--. 1 root root  0 May 13 23:54 1.jpg
-rw-r--r--. 1 root root  0 May 13 23:54 1.mp4
-rw-r--r--. 1 root root  0 May 13 23:53 222.txt
drwxr-xr-x. 2 root root  6 May 14 00:06 a
drwx------. 3 root root 17 May 13 16:54 systemd-private-73b52ffba72647758a757e55bd645ed0-chronyd.service-GCyL6Z
drwx------. 3 root root 17 May 13 16:54 systemd-private-73b52ffba72647758a757e55bd645ed0-dbus-broker.service-KkBqYX
drwx------. 3 root root 17 May 13 16:54 systemd-private-73b52ffba72647758a757e55bd645ed0-systemd-logind.service-JLzYF3
[root@localhost ~]# mkdir -p /tmp/a/b/c
[root@localhost ~]# tree /tmp/a
/tmp/a
└── b
    └── c

2 directories, 0 files

 

  复制文件或目录

cp命令用来复制(copy)文件或目录。

cp命令的各式如下:cp [选项] <源文件> <目标文件>

cp [选项] <源文件> <目标文件>

 把/etc/hosts拷贝到/tmp目录下 复制的时候目标路径下如果没有写名字,默认就和原路径的名字一样。

cp /etc/hosts /tmp/
[root@localhost ~]# cp /etc/hosts /tmp/
[root@localhost ~]# ll /tmp/
总用量 4
-rw-r--r--. 1 root root 158 5月  25 20:01 hosts

如果我们在复制时在目标路径下写名字了,就会把源文件拷贝过来名字改成我自己起的名字。

[root@localhost ~]# cp /etc/hosts /tmp/1.txt
[root@localhost ~]# ll /tmp/
总用量 8
-rw-r--r--. 1 root root 158 5月  25 20:08 1.txt
-rw-r--r--. 1 root root 158 5月  25 20:01 hosts

复制目录,目录有很多层级所以复制的时候一定要加-r。

[root@localhost ~]# cp -r /etc /tmp/
[root@localhost ~]# ll /tmp/
总用量 12
drwxr-xr-x. 76 root root 8192 5月  25 20:12 etc

如果目标路径下也有一个和你跟你原路径重名的文件,这就涉及到覆盖。在命令的前面加\意思就是临时取消命令的别名。

 

  移动文件或目录

mv命令用于对文件或目录进行移动(move)或改名。

mv命令格式如下:mv [选项] <源文件或目录> <目标文件或目录>

mv [选项] <源文件或目录> <目标文件或目录>

将file1改名成file1 .bak     前提是在同一目录下!

[root@localhost ~]# mv file1 file1.bak

将/subdir1下的file1文件移动到/subdir2下,并改名为file2

[root@localhost ~]# mv /subdirl/file1 /subdir2/file2

 注意有多个源文件或目录,则最后一个目标文件一定是目录!!!

每个文件的完整文件名包含了前面的目录和最终的文件名,,而每个文件名的长度都可以达到255个字符。那怎么知道哪个是文件名?哪个是目录?就是利反斜线左斜杠(/)来辨别。
[root@localhost ~]# basename /etc/sysconfig/network
network       文件名
[root@localhost ~]# dirname /etc/sysconfig/network
/etc/sysconfig     目录名
获取路径的文件名与目录名称

 

  删除

rm -rf /tmp/*

-r 代表递归的删 -f 代表在删的时候不提示直接删  /tmp/ 代表要删的东西,这里是删掉/tmp目录下的所有东西所以要带/*,如果不带/代表的是删掉/tmp目录。

 

 

7、直接查看文件内容

  cat[-AbEnTv]后面跟文件路径。

  less   展示完了时候可以上下键来翻。

  more  展示完了按回车根据百分比进行查看。

  head   可以提指定查看文件的开头几行,默认显示10行。

  tail   可以指定查看文件的末尾几行,默认显示10行。

  tail -f 可以动态查看文件新增的内容。

[root@localhost ~]# tail -f /var/log/messages
May 25 20:31:28 localhost NetworkManager[608]: <info>  [1748176288.4322] dhcp4 (ens33):   nameserver '192.168.1.1'
May 25 20:31:28 localhost NetworkManager[608]: <info>  [1748176288.4322] dhcp4 (ens33): state changed bound -> bound
May 25 20:31:28 localhost dbus[535]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service'
May 25 20:31:28 localhost systemd: Starting Network Manager Script Dispatcher Service...
May 25 20:31:28 localhost dhclient[1142]: bound to 192.168.1.59 -- renewal in 2884 seconds.
May 25 20:31:28 localhost dbus[535]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
May 25 20:31:28 localhost systemd: Started Network Manager Script Dispatcher Service.
May 25 20:31:28 localhost nm-dispatcher: req:1 'dhcp4-change' [ens33]: new request (3 scripts)
May 25 20:31:28 localhost nm-dispatcher: req:1 'dhcp4-change' [ens33]: start running ordered scripts...
May 25 21:01:01 localhost systemd: Started Session 35 of user root.
tail -f /var/log/messages

  ps aux 可以查看除系统当中当前所有正在运行的进程。

[root@localhost ~]# ps aux |head
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.6 128012  6672 ?        Ss   5月24   0:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root          2  0.0  0.0      0     0 ?        S    5月24   0:00 [kthreadd]
root          4  0.0  0.0      0     0 ?        S<   5月24   0:00 [kworker/0:0H]
root          5  0.0  0.0      0     0 ?        S    5月24   0:01 [kworker/u256:0]
root          6  0.0  0.0      0     0 ?        S    5月24   0:01 [ksoftirqd/0]
root          7  0.0  0.0      0     0 ?        S    5月24   0:00 [migration/0]
root          8  0.0  0.0      0     0 ?        S    5月24   0:00 [rcu_bh]
root          9  0.0  0.0      0     0 ?        R    5月24   0:05 [rcu_sched]
root         10  0.0  0.0      0     0 ?        S<   5月24   0:00 [lru-add-drain]
ps aux |head
[root@localhost ~]# ps aux |tail -3
root      13164  0.0  0.0      0     0 ?        S    20:57   0:00 [kworker/0:0]
root      13257  0.0  0.1 155448  1860 pts/0    R+   20:59   0:00 ps aux
root      13258  0.0  0.0 115544   616 pts/0    R+   20:59   0:00 -bash

 

posted @ 2025-05-14 15:16  张仁国  阅读(37)  评论(0)    收藏  举报
目录代码