在开始之前先将Linux系统的基础文件夹关系图列出来
编辑
前提知识
1、./是当前目录
2、../是父级目录
3、/是根目录
1、cd(改变当前工作目录)
[root@CentOS-7-64 ~]# cd /etc
[root@CentOS-7-64 etc]#
[root@CentOS-7-64 etc]# cd ../opt
[root@CentOS-7-64 opt]#
2、ll(显示出指定目录下的所有文件和文件夹和快捷方式)
[root@CentOS-7-64 etc]# ll
total 1028
-rw-r--r--. 1 root root 16 May 11 13:09 adjtime
-rw-r--r--. 1 root root 1518 Jun 7 2013 aliases
-rw-r--r--. 1 root root 12288 May 11 13:15 aliases.db
drwxr-xr-x. 2 root root 236 May 11 13:06 alternatives
-rw-------. 1 root root 541 Aug 9 2019 anacrontab
-rw-r--r--. 1 root root 55 Aug 8 2019 asound.conf
drwxr-x---. 3 root root 43 May 11 13:06 audisp
drwxr-x---. 3 root root 83 May 11 13:15 audit
drwxr-xr-x. 2 root root 22 May 11 13:06 bash_completion.d
-rw-r--r--. 1 root root 2853 Oct 31 2018 bashrc
drwxr-xr-x. 2 root root 6 Aug 8 2019 binfmt.d
-rw-r--r--. 1 root root 37 Sep 5 2019 centos-release
-rw-r--r--. 1 root root 51 Sep 5 2019 centos-release-upstream
drwxr-xr-x. 2 root root 6 Aug 4 2017 chkconfig.d
-rw-r--r-- 1 root root 0 May 11 20:53 config
drwxr-xr-x. 2 root root 21 May 11 13:06 cron.d
drwxr-xr-x. 2 root root 42 May 11 13:06 cron.daily
-rw-------. 1 root root 0 Aug 9 2019 cron.deny
drwxr-xr-x. 2 root root 22 Jun 10 2014 cron.hourly
drwxr-xr-x. 2 root root 6 Jun 10 2014 cron.monthly
-rw-r--r--. 1 root root 451 Jun 10 2014 crontab
drwxr-xr-x. 2 root root 6 Jun 10 2014 cron.weekly
-rw-------. 1 root root 0 May 11 13:05 crypttab
-rw-r--r--. 1 root root 1620 Oct 31 2018 csh.cshrc
-rw-r--r--. 1 root root 866 Oct 31 2018 csh.login
drwxr-xr-x. 4 root root 78 May 11 13:06 dbus-1
drwxr-xr-x. 2 root root 44 May 11 13:09 default
drwxr-xr-x. 2 root root 23 May 11 13:06 depmod.d
drwxr-x---. 4 root root 53 May 11 13:06 dhcp
-rw-r--r--. 1 root root 5090 Aug 6 2019 DIR_COLORS
-rw-r--r--. 1 root root 5725 Aug 6 2019 DIR_COLORS.256color
-rw-r--r--. 1 root root 4669 Aug 6 2019 DIR_COLORS.lightbgcolor
-rw-r--r--. 1 root root 1285 Aug 9 2019 dracut.conf
drwxr-xr-x. 2 root root 6 Aug 9 2019 dracut.conf.d
-rw-r--r--. 1 root root 112 Aug 6 2019 e2fsck.conf
-rw-r--r--. 1 root root 0 Oct 31 2018 environment
-rw-r--r--. 1 root root 1317 Apr 11 2018 ethertypes
-rw-r--r--. 1 root root 0 Jun 7 2013 exports
lrwxrwxrwx. 1 root root 56 May 11 13:06 favicon.png -> /usr/share/icons/hicolor/16x16/apps/fedora-logo-icon.png
-rw-r--r--. 1 root root 70 Oct 31 2018 filesystems
drwxr-x---. 7 root root 133 May 11 13:06 firewalld
-rw-r--r--. 1 root root 465 May 11 13:05 fstab
drwxr-xr-x. 2 root root 6 Aug 2 2017 gcrypt
drwxr-xr-x. 2 root root 6 Jul 13 2018 gnupg
-rw-r--r--. 1 root root 94 Mar 25 2017 GREP_COLORS
drwxr-xr-x. 4 root root 40 May 11 13:06 groff
-rw-r--r--. 1 root root 435 May 11 13:06 group
-rw-r--r--. 1 root root 428 May 11 13:06 group-
lrwxrwxrwx. 1 root root 22 May 11 13:06 grub2.cfg -> ../boot/grub2/grub.cfg
drwx------. 2 root root 182 May 11 13:06 grub.d
----------. 1 root root 345 May 11 13:06 gshadow
----------. 1 root root 338 May 11 13:06 gshadow-
drwxr-xr-x. 3 root root 20 May 11 13:06 gss
注:
- "-rw-------": -开头代表是一个文件, rw代表对该文件的操作权限
- "drwxr-xr-x": d开头指的是一个文件夹
- "lrwxrwxrwx": l开头代表的是一个链接, 在windows系统中代表快捷方式
3、ls(显示出指定目录下的所有文件)跟ll是一样的作用, 只不过显示效果不太一样.
[root@CentOS-7-64 etc]# ls
adjtime dbus-1 gshadow localtime passwd- resolv.conf sudoers
aliases default gshadow- login.defs pkcs11 resolv.conf.save sudoers.d
aliases.db depmod.d gss logrotate.conf pki rpc sudo-ldap.conf
alternatives dhcp host.conf logrotate.d plymouth rpm sysconfig
anacrontab DIR_COLORS hostname lvm pm rsyslog.conf sysctl.conf
asound.conf DIR_COLORS.256color hosts machine-id polkit-1 rsyslog.d sysctl.d
audisp DIR_COLORS.lightbgcolor hosts.allow magic popt.d rwtab systemd
audit dracut.conf hosts.deny makedumpfile.conf.sample postfix rwtab.d system-release
bash_completion.d dracut.conf.d init.d man_db.conf ppp sasl2 system-release-cpe
bashrc e2fsck.conf inittab mke2fs.conf prelink.conf.d securetty terminfo
binfmt.d environment inputrc modprobe.d printcap security tmpfiles.d
centos-release ethertypes iproute2 modules-load.d profile selinux tuned
centos-release-upstream exports issue motd profile.d services udev
chkconfig.d favicon.png issue.net mtab protocols sestatus.conf vconsole.conf
config filesystems kdump.conf my.cnf python shadow virc
cron.d firewalld kernel my.cnf.d rc0.d shadow- wpa_supplicant
cron.daily fstab krb5.conf NetworkManager rc1.d shells X11
cron.deny gcrypt krb5.conf.d networks rc2.d skel xdg
cron.hourly gnupg ld.so.cache nsswitch.conf rc3.d ssh xinetd.d
cron.monthly GREP_COLORS ld.so.conf nsswitch.conf.bak rc4.d ssl yum
crontab groff ld.so.conf.d openldap rc5.d statetab yum.conf
cron.weekly group libaudit.conf opt rc6.d statetab.d yum.repos.d
crypttab group- libnl os-release rc.d subgid
csh.cshrc grub2.cfg libuser.conf pam.d rc.local subuid
csh.login grub.d locale.conf passwd redhat-release sudo.conf
4、mkdir(创建文件目录), rmdir(删除空文件夹)
[root@CentOS-7-64 ~]# mkdir aaa
[root@CentOS-7-64 ~]# ll
total 4
drwxr-xr-x 2 root root 6 May 13 18:51 aaa
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
一下子创建多个文件夹加 "-p"
[root@CentOS-7-64 ~]# mkdir -p a/b/c
[root@CentOS-7-64 ~]# ll
total 4
drwxr-xr-x 3 root root 15 May 13 18:54 a
drwxr-xr-x 2 root root 6 May 13 18:51 aaa
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
[root@CentOS-7-64 ~]# cd a
[root@CentOS-7-64 a]# cd b
[root@CentOS-7-64 b]# cd c
在同一个目录下创建多个文件夹
[root@CentOS-7-64 c]# mkdir -p shiren{libai,baijuyi,dufu}
[root@CentOS-7-64 c]# ll
total 0
drwxr-xr-x 2 root root 6 May 13 19:00 shirenbaijuyi
drwxr-xr-x 2 root root 6 May 13 19:00 shirendufu
drwxr-xr-x 2 root root 6 May 13 19:00 shirenlibai
在shiren目录下创建多个文件夹
[root@CentOS-7-64 ~]# ll
total 4
drwxr-xr-x 3 root root 15 May 13 18:54 a
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
[root@CentOS-7-64 ~]# mkdir -p shiren/{libai,baijuyi,dufu}
[root@CentOS-7-64 ~]# ll
total 4
drwxr-xr-x 3 root root 15 May 13 18:54 a
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x 5 root root 46 May 13 20:27 shiren
[root@CentOS-7-64 ~]# cd shiren
[root@CentOS-7-64 shiren]# ll
total 0
drwxr-xr-x 2 root root 6 May 13 20:27 baijuyi
drwxr-xr-x 2 root root 6 May 13 20:27 dufu
drwxr-xr-x 2 root root 6 May 13 20:27 libai
5、rmdir(删除空文件夹)
[root@CentOS-7-64 ~]# ll
total 4
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
[root@CentOS-7-64 ~]# mkdir aaa
[root@CentOS-7-64 ~]# ll
total 4
drwxr-xr-x 2 root root 6 May 13 20:44 aaa
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
[root@CentOS-7-64 ~]# rmdir aaa
[root@CentOS-7-64 ~]# ll
total 4
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
[root@CentOS-7-64 ~]# ll
total 4
drwxr-xr-x 2 root root 6 May 13 20:45 aaa
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
[root@CentOS-7-64 ~]# cd aaa
[root@CentOS-7-64 aaa]# mkdir bbb
[root@CentOS-7-64 aaa]# cd
[root@CentOS-7-64 ~]# rmdir aaa
rmdir: failed to remove ‘aaa’: Directory not empty
[root@CentOS-7-64 ~]# rm -rf aaa
[root@CentOS-7-64 ~]# ll
total 4
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
6、cp(拷贝文件或者文件目录)
将aaa文件夹中的test文件夹拷贝到bbb文件夹中
[root@CentOS-7-64 ~]# ll
total 4
drwxr-xr-x 3 root root 18 May 13 20:51 aaa
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 May 13 20:56 bbb
[root@CentOS-7-64 ~]# cd aaa
[root@CentOS-7-64 aaa]# ll
total 0
drwxr-xr-x 2 root root 6 May 13 20:51 test
[root@CentOS-7-64 aaa]# cd
[root@CentOS-7-64 ~]# cd bbb
[root@CentOS-7-64 bbb]# ll
total 0
[root@CentOS-7-64 bbb]# cp -r aaa/test bbb
cp: cannot stat ‘aaa/test’: No such file or directory
[root@CentOS-7-64 bbb]# cd
[root@CentOS-7-64 ~]# cp -r aaa/test bbb
[root@CentOS-7-64 ~]# cd bbb
[root@CentOS-7-64 bbb]# ll
total 0
drwxr-xr-x 2 root root 6 May 13 20:57 test
[root@CentOS-7-64 bbb]#
将文件拷贝到aaa文件夹下
[root@CentOS-7-64 ~]# ll
total 4
drwxr-xr-x 3 root root 18 May 13 20:51 aaa
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x 3 root root 18 May 13 20:57 bbb
[root@CentOS-7-64 ~]# cp anaconda-ks.cfg aaa
[root@CentOS-7-64 ~]# cd aaa
[root@CentOS-7-64 aaa]# ll
total 4
-rw------- 1 root root 1255 May 13 21:05 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 May 13 20:51 test
7、mv(移动文件或者文件夹)
[root@CentOS-7-64 ~]# ll
total 4
drwxr-xr-x 3 root root 41 May 13 21:05 aaa
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x 3 root root 18 May 13 20:57 bbb
[root@CentOS-7-64 ~]# mv aaa bbb
[root@CentOS-7-64 ~]# ll
total 4
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x 4 root root 29 May 13 21:09 bbb
[root@CentOS-7-64 ~]# cd bbb
[root@CentOS-7-64 bbb]# ll
total 0
drwxr-xr-x 3 root root 41 May 13 21:05 aaa
drwxr-xr-x 2 root root 6 May 13 20:57 test
mv还有修改文件或者文件夹名字的作用
[root@CentOS-7-64 ~]# ll
total 4
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x 4 root root 29 May 13 21:09 bbb
[root@CentOS-7-64 ~]# mv bbb bbb1
[root@CentOS-7-64 ~]# ll
total 4
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x 4 root root 29 May 13 21:09 bbb1
8、rm(删除文件或者文件夹)
rm删除文件的时候需要输入(y, n), 如果输入y则是删除, 否则则是不删除, 如果只用rm只能删除文件.
[root@CentOS-7-64 ~]# ll
total 4
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x 4 root root 29 May 13 21:09 bbb1
[root@CentOS-7-64 ~]# cd bbb1
[root@CentOS-7-64 bbb1]# ll
total 0
drwxr-xr-x 3 root root 41 May 13 21:05 aaa
drwxr-xr-x 2 root root 6 May 13 20:57 test
[root@CentOS-7-64 bbb1]# cd aaa
[root@CentOS-7-64 aaa]# ll
total 4
-rw------- 1 root root 1255 May 13 21:05 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 May 13 20:51 test
[root@CentOS-7-64 aaa]# rm anaconda-ks.cfg
rm: remove regular file ‘anaconda-ks.cfg’? y
[root@CentOS-7-64 aaa]# ll
total 0
drwxr-xr-x 2 root root 6 May 13 20:51 test
rm -f: 强制删除文件, 不需要手动输入y,n
[root@CentOS-7-64 bbb1]# ll
total 4
drwxr-xr-x 3 root root 18 May 13 21:21 aaa
-rw-r--r-- 1 root root 16 May 13 21:23 adjtime
drwxr-xr-x 2 root root 6 May 13 20:57 test
[root@CentOS-7-64 bbb1]# rm -f adjtime
[root@CentOS-7-64 bbb1]# ll
total 0
drwxr-xr-x 3 root root 18 May 13 21:21 aaa
drwxr-xr-x 2 root root 6 May 13 20:57 test
rm -r(删除1文件夹, 只不过也要输入y,n)
rm -rf(前置删除文件夹, 不需要手动输入y,n)
9、touch, stat
touch(如果没有就创建一个文件)
[root@CentOS-7-64 ~]# ll
total 4
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x 4 root root 29 May 13 21:24 bbb1
[root@CentOS-7-64 ~]# touch test.txt
[root@CentOS-7-64 ~]# ll
total 4
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x 4 root root 29 May 13 21:24 bbb1
-rw-r--r-- 1 root root 0 May 13 21:36 test.txt
stat(查看文件/文件夹状态)
[root@CentOS-7-64 ~]# stat test.txt
File: ‘test.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 67172211 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-05-13 21:36:33.572958157 +0800
Modify: 2022-05-13 21:36:33.572958157 +0800
Change: 2022-05-13 21:36:33.572958157 +0800
解析:
- Inode 当前文件在文件系统的唯一标识,类似于ID
时间
- access 访问时间
- modify 修改文件内容时间
- change 修改文件元数据信息时间 (文件大小 ,文件所有者 ,文件权限, 对于文件的描述信息)
[root@CentOS-7-64 ~]# ll
total 8
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x 4 root root 29 May 13 21:24 bbb1
-rwxr-xr-x 1 root root 1 May 13 22:07 test.txt
[root@CentOS-7-64 ~]# vi test.txt
[root@CentOS-7-64 ~]# stat test.txt
File: ‘test.txt’
Size: 16 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 67172212 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-05-13 22:08:22.873880847 +0800
Modify: 2022-05-13 22:08:22.873880847 +0800
Change: 2022-05-13 22:08:22.873880847 +0800
修改了文件内容, 其中Accesss, Modify, Change的时间都会发生变化; 且时间都一致
[root@CentOS-7-64 ~]# ll
total 8
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x 4 root root 29 May 13 21:24 bbb1
-rwxr-xr-x 1 root root 16 May 13 22:08 test.txt
[root@CentOS-7-64 ~]# touch test.txt
[root@CentOS-7-64 ~]# stat test.txt
File: ‘test.txt’
Size: 16 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 67172212 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-05-13 22:10:20.597876081 +0800
Modify: 2022-05-13 22:10:20.597876081 +0800
Change: 2022-05-13 22:10:20.597876081 +0800
其中touch 文件名: 如果该文件已经存在, 修改文件的三个时间, 将三个时间都改为当前时间;
10、ln
[root@CentOS-7-64 ~]# ll
total 8
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x 4 root root 29 May 13 21:24 bbb1
-rwxr-xr-x 1 root root 16 May 13 22:10 test.txt
//创建软链接(ln -s)
[root@CentOS-7-64 ~]# ln -s test.txt test1.txt
[root@CentOS-7-64 ~]# ll
total 8
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x 4 root root 29 May 13 21:24 bbb1
lrwxrwxrwx 1 root root 8 May 13 22:27 test1.txt -> test.txt
-rwxr-xr-x 1 root root 16 May 13 22:10 test.txt
//创建硬链接(ln )
[root@CentOS-7-64 ~]# ln test.txt test2.txt
[root@CentOS-7-64 ~]# ll
total 12
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x 4 root root 29 May 13 21:24 bbb1
lrwxrwxrwx 1 root root 8 May 13 22:27 test1.txt -> test.txt
-rwxr-xr-x 2 root root 16 May 13 22:10 test2.txt
-rwxr-xr-x 2 root root 16 May 13 22:10 test.txt
[root@CentOS-7-64 ~]# vi test.txt
[root@CentOS-7-64 ~]# cat test1.txt
世界, 你好(hello world)!
[root@CentOS-7-64 ~]# vi test.txt
[root@CentOS-7-64 ~]# cat test2.txt
世界, 你好(hello world)!
床前明月光, 疑是地上霜.
[root@CentOS-7-64 ~]# cat test1.txt
世界, 你好(hello world)!
床前明月光, 疑是地上霜.
//Links = 2 这个文件地址(Inode: 67172212)被两个所指向(关联)
[root@CentOS-7-64 ~]# stat test.txt
File: ‘test.txt’
Size: 64 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 67172212 Links: 2
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-05-13 22:30:29.353827137 +0800
Modify: 2022-05-13 22:30:21.518827454 +0800
Change: 2022-05-13 22:30:21.518827454 +0800
Birth: -
//Links = 1 这个文件指向(关联)的是test.txt文件
[root@CentOS-7-64 ~]# stat test1.txt
File: ‘test1.txt’ -> ‘test.txt’
Size: 8 Blocks: 0 IO Block: 4096 symbolic link
Device: fd00h/64768d Inode: 67172211 Links: 1
Access: (0777/lrwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-05-13 22:27:57.728833276 +0800
Modify: 2022-05-13 22:27:45.512833771 +0800
Change: 2022-05-13 22:27:45.512833771 +0800
Birth: -
//Links = 2 这个文件地址(Inode: 67172212)被两个所指向(关联)
[root@CentOS-7-64 ~]# stat test2.txt
File: ‘test2.txt’
Size: 64 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 67172212 Links: 2
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-05-13 22:30:29.353827137 +0800
Modify: 2022-05-13 22:30:21.518827454 +0800
Change: 2022-05-13 22:30:21.518827454 +0800
Birth: -
[root@CentOS-7-64 ~]# rm -rf test.txt
//删除test.txt之后, 软地址(链接)已经没有指向的, 这个时候 test1.txt -> test.txt会报错.
[root@CentOS-7-64 ~]# ll
total 8
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x 4 root root 29 May 13 21:24 bbb1
lrwxrwxrwx 1 root root 8 May 13 22:27 test1.txt -> test.txt
-rwxr-xr-x 1 root root 64 May 13 22:30 test2.txt
[root@CentOS-7-64 ~]# stat test1.txt
File: ‘test1.txt’ -> ‘test.txt’
Size: 8 Blocks: 0 IO Block: 4096 symbolic link
Device: fd00h/64768d Inode: 67172211 Links: 1
Access: (0777/lrwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-05-13 22:27:57.728833276 +0800
Modify: 2022-05-13 22:27:45.512833771 +0800
Change: 2022-05-13 22:27:45.512833771 +0800
Birth: -
//删除test.txt之后, Links:1 指向67172211硬盘地址的就只有test2.txt文件了;
[root@CentOS-7-64 ~]# stat test2.txt
File: ‘test2.txt’
Size: 64 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 67172212 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-05-13 22:30:29.353827137 +0800
Modify: 2022-05-13 22:30:21.518827454 +0800
Change: 2022-05-13 22:32:14.842822865 +0800