Linux系统文件管理基础
一、Linux系统文件系统
1.1 Linux文件系统结构

在Linux系统中,目录被组织成一个:单根倒置树结构,文件系统从根目录开始,用/来表示。文件名称区分大小写( 大小写敏感还需要看具体的文件系统格式 ),以.开头的为隐藏文件,路径用/来进行分割(windows中使用\来分割),文件有两个种类:元数据与数据本身。在操作linux系统时,通常会遵循以下两种分层结构规则:LSB (Linux Standard Base) / FHS(Filesystem Hierarchy Standard)
1.2 文件系统命名规则
Linux文件系统名称最长为255字节(字节,不是字符)。包括路径在内的文件名称最长为4095字节。文件颜色显示不同,代表了不同的文件格式。(以下为默认格式,可以在/etc/DIR_COLORS中自定义)
-
蓝色 -> 文件夹。
-
绿色 -> 可执行文件
-
红色 -> 压缩文件
-
浅蓝色 -> 链接文件
-
灰色 ->其他文件
在Linux中,除了斜杠和NUL字符,都可以用来命名。不推荐使用特殊符号来命名文件或者目录,容易引发混淆.比如空格, -,非要创建就用””引用起来,但如果遇到用特殊字符命名的特殊文件,在该文件上加上文件路径后即可进行查看,创建,删除。.. 表示父目录 .表示当前目录。
1.3 Linux系统目录及功能
/:根目录,位于Linux文件系统目录结构的顶层,一般根目录下只存放目录,不要存放文件,/etc、/bin、/dev、/lib、/sbin应该和根目录放置在一个分区中。
/bin : 提供用户使用的基本命令, 存放二进制命令,不允许关联到独立分区,OS启动会用到里面的程序。
/boot:用于存放引导文件,内核文件,引导加载器。
/sbin:管理类的基本命令,不能关联到独立分区,OS启动时会用到的程序(重要的命令通常处于bin,不重要的则安装在sbin)。
/lib:存放系统在启动时依赖的基本共享库文件以及内核模块文件。系统使用的函数库的目录 也存放了大量的脚本库文件,程序在执行过程中,需要调用时会用到。
/lib64:存放64位系统上的辅助共享库文件。
/etc: 系统配置文件存放的目录,该目录存放系统的大部分配置文件和子目录,不建议在此目录下存放可执行文件 。
/home:普通用户主目录,当新建账户时,都会分配在此,建议单独分区,并分配额外空间用于存储数据。
/root:系统管理员root的宿主目录,系统第一个启动的分区为/,所以最好将/root和/放置在一个分区下。
/media:便携式移动设备挂载点目录。
/mnt:临时文件系统挂载点。
/dev:设备(device)文件目录,存放linux系统下的设备文件,访问该目录下某个文件,相当于访问某个设备,存放连接到计算机上的设备(终端、磁盘驱动器、光驱及网卡等)的对应文件 (b 块文件随机访问,c 字符文件线性访问)。
/opt:第三方应用程序的安装位置。
/srv: 服务启动之后需要访问的数据目录,存放系统上运行的服务用到的数据,如www服务需要访问的网页数据存放在/srv/www内。
/tmp:存储临时文件, 任何人都可以访问,重要数据一定不要放在此目录下。
/usr:应用程序存放目录,/usr/bin 存放保证系统拥有完整功能而提供的应用程序, /usr/share 存放共享数据,/usr/lib 存放不能直接运行的,却是许多程序运行所必需的一些函数库文件,/usr/local 存放软件升级包,第三方应用程序的安装位置,/usr/share/doc 系统说明文件存放目录。
/var :放置系统中经常要发生变化的文件,如日志文件。/var/log 日志目录及文件。/var/tmp:保存系统两次重启之间产生的临时数据。
/proc:用于输出内核与进程信息相关的虚拟文件系统,目录中的数据都在内存中,如系统核心,外部设备,网络状态,由于数据都存放于内存中,所以不占用磁盘空间。
/sys : 用于输出当前系统上硬件设备相关的虚拟文件系统。
/selinux:存放selinux相关的信息安全策略等信息。
二、文件的元数据
2.1 Inode表结构
每个文件都有属性信息,比如:文件的大小,时间,类型等称为文件的元数据(meta data),这些元数据是存放在node(index node)表中。node表中有很多条记录组成,第一条记录对应的存放了一个文件的元数据信息。第一个node表记录对应的保存了一下信息:
- inode number 节点号
- 文件类型
- 权限
- UID
- GID
- 链接数(指向这个文件名路径名称个数)
- 该文件的大小和不同的时间戳
- 指向磁盘上文件的数据块指针
- 有关文件的其他数据

2.2 文件系统如何查找文件
1、根据文件名,通过Directory里的对应关系,找到文件对应的Inode number。
2、再根据Inode number读取到文件的Inode table里对应的条目。
3、再根据Inode table中的Pointer读取到相应的Blocks。

这里有一个重要的内容,就是Directory,他不是我们通常说的目录,而是一个列表,记录了一个文件/目录名称对应的Inode number。如下图:

2.3 查看Inode信息
inode也会消耗硬盘空间,所以硬盘格式化的时候,操作系统自动将硬盘分成两个区域。一个是数据区,存放文件数据;另一个是inode区(inode table),存放inode所包含的信息。
每个inode节点的大小,一般是128字节或256字节。inode节点的总数,在格式化时就给定,一般是每1KB或每2KB就设置一个inode。假定在一块1GB的硬盘中,每个inode节点的大小为128字节,每1KB就设置一个inode,那么inode table的大小就会达到128MB,占整块硬盘的12.8%。
查看每个硬盘分区的inode总数和已经使用的数量,可以使用df命令。
1# df -hi 2Filesystem Inodes IUsed IFree IUse% Mounted on 3devtmpfs 110K 393 110K 1% /dev 4tmpfs 113K 1 113K 1% /dev/shm 5tmpfs 113K 748 112K 1% /run 6tmpfs 113K 16 113K 1% /sys/fs/cgroup 7/dev/sda3 59M 41K 59M 1% / 8/dev/sda1 512K 332 512K 1% /boot 9tmpfs 113K 1 113K 1% /run/user/0
查看每个inode节点的大小,可以用如下命令:
1# df -T 2Filesystem Type 1K-blocks Used Available Use% Mounted on 3devtmpfs devtmpfs 450284 0 450284 0% /dev 4tmpfs tmpfs 460984 0 460984 0% /dev/shm 5tmpfs tmpfs 460984 7428 453556 2% /run 6tmpfs tmpfs 460984 0 460984 0% /sys/fs/cgroup 7/dev/sda3 xfs 122622468 1836724 120785744 2% / 8/dev/sda1 xfs 1038336 163080 875256 16% /boot 9tmpfs tmpfs 92200 0 92200 0% /run/user/0 10[root@localhost ~]# 11# xfs_info /dev/sda3 12meta-data=/dev/sda3 isize=512 agcount=4, agsize=7667648 blks 13 = sectsz=512 attr=2, projid32bit=1 14 = crc=1 finobt=0 spinodes=0 15data = bsize=4096 blocks=30670592, imaxpct=25 16 = sunit=0 swidth=0 blks 17naming =version 2 bsize=4096 ascii-ci=0 ftype=1 18log =internal bsize=4096 blocks=14975, version=2 19 = sectsz=512 sunit=0 blks, lazy-count=1 20realtime =none extsz=4096 blocks=0, rtextents=0 21[root@localhost ~]#
由于每个文件都必须有一个inode,因此有可能发生inode已经用光,但是硬盘还未存满的情况。这时,就无法在硬盘上创建新文件。
ls命令只列出目录文件中的所有文件名,ls -i命令列出整个目录文件,即文件名和inode号码:
1# ls -i /etc 2 67547984 adjtime 67110248 host.conf 67547947 rc0.d 3 67110239 aliases 67108942 hostname 67159133 rc1.d 4 67746346 aliases.db 67110249 hosts 67167319 rc2.d 5134466363 alternatives 67110250 hosts.allow 67167320 rc3.d 6 67565891 anacrontab 67110251 hosts.deny 67167321 rc4.d 7 67462354 asound.conf 67167297 init.d 67167322 rc5.d 8 67120879 at.deny 67547942 inittab 67167323 rc6.d 9 534523 audisp 67110252 inputrc 95562 rc.d
目录文件的读权限(r)和写权限(w),都是针对目录文件本身。由于目录文件内只有文件名和inode号码,所以如果只有读权限,只能获取文件名,无法获取其他信息,因为其他信息都储存在inode节点中,而读取inode节点内的信息需要目录文件的执行权限(x)。
2.4 修改文件时间戳信息
每个文件有三个时间戳:
access time 访问时间,atime,读取文件内容
modify time 修改时间,mtime,改变文件内容(数据)
change time 改变时间,ctime,元数据发生改变
touch命令可以用来创建空文件或刷新文件的时间
格式:
选项说明:
-a 仅改变 atime和ctime
-m 仅改变 mtime和ctime
-t [[CC]YY]MMDDhhmm[.ss] 指定atime和mtime的时间戳
-c 如果文件不存在,则不予创建
1# stat anaconda-ks.cfg 2 File: ‘anaconda-ks.cfg’ 3 Size: 1582 Blocks: 8 IO Block: 4096 regular file 4Device: 803h/2051d Inode: 134337602 Links: 1 5Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root) 6Context: system_u:object_r:admin_home_t:s0 7Access: 2020-11-28 08:32:25.680046954 +0800 8Modify: 2020-11-24 01:43:08.007006302 +0800 9Change: 2020-11-27 21:35:53.853756153 +0800 10 Birth: - 11# touch -at 201910101010.10 12touch: missing file operand 13Try 'touch --help' for more information. 14[root@localhost ~]# touch -at 201910101010.10 anaconda-ks.cfg 15[root@localhost ~]# stat anaconda-ks.cfg 16 File: ‘anaconda-ks.cfg’ 17 Size: 1582 Blocks: 8 IO Block: 4096 regular file 18Device: 803h/2051d Inode: 134337602 Links: 1 19Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root) 20Context: system_u:object_r:admin_home_t:s0 21Access: 2019-10-10 10:10:10.000000000 +0800 22Modify: 2020-11-24 01:43:08.007006302 +0800 23Change: 2020-12-02 22:32:20.563535891 +0800 24 Birth: - 25# touch -ct 202212121111.11 anaconda-ks.cfg 26# stat anaconda-ks.cfg 27 File: ‘anaconda-ks.cfg’ 28 Size: 1582 Blocks: 8 IO Block: 4096 regular file 29Device: 803h/2051d Inode: 134337602 Links: 1 30Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root) 31Context: system_u:object_r:admin_home_t:s0 32Access: 2022-12-12 11:11:11.000000000 +0800 33Modify: 2022-12-12 11:11:11.000000000 +0800 34Change: 2020-12-02 22:33:35.250538297 +0800 35 Birth: -
三、硬链接与软连接区别
-
本质:
硬链接:本质是同一个文件
软链接:本质不是同一个文件1# echo abc > 1.txt #新建1.txt文件 2[root@localhost tmp]# stat 1.txt 3File: ‘1.txt’ 4Size: 4 Blocks: 8 IO Block: 4096 regular file 5Device: 803h/2051d Inode: 67831651 Links: 1 #Inode:67831651 6Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 7Context: unconfined_u:object_r:admin_home_t:s0 8Access: 2020-12-03 09:37:37.911015780 +0800 9Modify: 2020-12-03 09:38:02.723016701 +0800 10Change: 2020-12-03 09:38:02.723016701 +0800 11Birth: - 12 13# ln 1.txt link_1.txt #硬链接 14# stat 1.txt link_1.txt 15File: ‘1.txt’ 16Size: 4 Blocks: 8 IO Block: 4096 regular file 17Device: 803h/2051d Inode: 67831651 Links: 2 #Inode Links 18Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 19Context: unconfined_u:object_r:admin_home_t:s0 20Access: 2020-12-03 09:37:37.911015780 +0800 21Modify: 2020-12-03 09:38:02.723016701 +0800 22Change: 2020-12-03 09:41:14.903023828 +0800 23Birth: - 24File: ‘link_1.txt’ 25Size: 4 Blocks: 8 IO Block: 4096 regular file 26Device: 803h/2051d Inode: 67831651 Links: 2 #Inode与源文件相同 Links 27Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 28Context: unconfined_u:object_r:admin_home_t:s0 29Access: 2020-12-03 09:37:37.911015780 +0800 30Modify: 2020-12-03 09:38:02.723016701 +0800 31Change: 2020-12-03 09:41:14.903023828 +0800 32Birth: - 33 34# ln -s 1.txt link_s_1.txt #软连接 35# stat 1.txt link_s_1.txt 36File: ‘1.txt’ 37Size: 4 Blocks: 8 IO Block: 4096 regular file 38Device: 803h/2051d Inode: 67831651 Links: 2 #源文件的Inode Links没变 39Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 40Context: unconfined_u:object_r:admin_home_t:s0 41Access: 2020-12-03 09:37:37.911015780 +0800 42Modify: 2020-12-03 09:38:02.723016701 +0800 43Change: 2020-12-03 09:41:14.903023828 +0800 44Birth: - 45File: ‘link_s_1.txt’ -> ‘1.txt’ 46Size: 5 Blocks: 0 IO Block: 4096 symbolic link 47Device: 803h/2051d Inode: 67831652 Links: 1 #新生成的Inode,新的文件 48Access: (0777/lrwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root) 49Context: unconfined_u:object_r:admin_home_t:s0 50Access: 2020-12-03 09:47:04.432036792 +0800 51Modify: 2020-12-03 09:46:55.507036461 +0800 52Change: 2020-12-03 09:46:55.507036461 +0800 53Birth: - -
跨设备
硬链接:不支持
软链接:支持1# df -T 2Filesystem Type 1K-blocks Used Available Use% Mounted on 3devtmpfs devtmpfs 450284 0 450284 0% /dev 4tmpfs tmpfs 460984 0 460984 0% /dev/shm 5tmpfs tmpfs 460984 7364 453620 2% /run 6tmpfs tmpfs 460984 0 460984 0% /sys/fs/cgroup 7/dev/sda3 xfs 122622468 1835928 120786540 2% / 8/dev/sda1 xfs 1038336 163080 875256 16% /boot 9tmpfs tmpfs 92200 0 92200 0% /run/user/0 10 11# echo 112233 > 2.txt 12# ln 2.txt /boot/link_2.txt #硬链接不能跨设备 13ln: failed to create hard link ‘/boot/link_2.txt’ => ‘2.txt’: Invalid cross-device link 14 15# ln -s 2.txt /boot/link_s_2.txt 16# stat 2.txt 17File: ‘2.txt’ 18Size: 7 Blocks: 8 IO Block: 4096 regular file 19Device: 803h/2051d Inode: 67831653 Links: 1 #源文件的Inode 20Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 21Context: unconfined_u:object_r:admin_home_t:s0 22Access: 2020-12-03 09:55:22.936055281 +0800 23Modify: 2020-12-03 09:55:35.404055743 +0800 24Change: 2020-12-03 09:55:35.404055743 +0800 25Birth: - 26# stat /boot/link_s_2.txt 27File: ‘/boot/link_s_2.txt’ -> ‘2.txt’ 28Size: 5 Blocks: 0 IO Block: 4096 symbolic link 29Device: 801h/2049d Inode: 84 Links: 1 #新生成的文件 30Access: (0777/lrwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root) 31Context: unconfined_u:object_r:boot_t:s0 32Access: 2020-12-03 09:58:38.783062545 +0800 33Modify: 2020-12-03 09:58:38.783062545 +0800 34Change: 2020-12-03 09:58:38.783062545 +0800 35Birth: - -
inode
硬链接:相同
软链接:不同 -
链接数
硬链接:创建新的硬链接,链接数会增加,删除硬链接,链接数减少
软链接:创建或删除,链接数不会变化1# stat 1.txt 2File: ‘1.txt’ 3Size: 4 Blocks: 8 IO Block: 4096 regular file 4Device: 803h/2051d Inode: 67831651 Links: 2 5Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 6Context: unconfined_u:object_r:admin_home_t:s0 7Access: 2020-12-03 09:37:37.911015780 +0800 8Modify: 2020-12-03 09:38:02.723016701 +0800 9Change: 2020-12-03 09:41:14.903023828 +0800 10Birth: - 11[root@localhost tmp]# rm link_s_1.txt 12rm: remove symbolic link ‘link_s_1.txt’? y 13[root@localhost tmp]# stat 1.txt 14File: ‘1.txt’ 15Size: 4 Blocks: 8 IO Block: 4096 regular file 16Device: 803h/2051d Inode: 67831651 Links: 2 #删除软连接,链接数没有变化 17Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 18Context: unconfined_u:object_r:admin_home_t:s0 19Access: 2020-12-03 09:37:37.911015780 +0800 20Modify: 2020-12-03 09:38:02.723016701 +0800 21Change: 2020-12-03 09:41:14.903023828 +0800 22Birth: - 23 24# rm -rf link_1.txt 25[root@localhost tmp]# stat 1.txt 26File: ‘1.txt’ 27Size: 4 Blocks: 8 IO Block: 4096 regular file 28Device: 803h/2051d Inode: 67831651 Links: 1 #删除硬链接,链接数减少 29Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 30Context: unconfined_u:object_r:admin_home_t:s0 31Access: 2020-12-03 09:37:37.911015780 +0800 32Modify: 2020-12-03 09:38:02.723016701 +0800 33Change: 2020-12-03 10:18:24.411106518 +0800 34Birth: - -
文件夹
硬链接:不支持
软链接:支持1# mkdir a b 2[root@localhost tmp]# ls 31.txt 2.txt a b 4[root@localhost tmp]# ln a a_link #文件夹硬链接不允许 5ln: ‘a’: hard link not allowed for directory 6# ln -s a a_s_link #文件夹可以创建软连接 7[root@localhost tmp]# ls -l 8total 8 9-rw-r--r--. 1 root root 4 Dec 3 09:38 1.txt 10-rw-r--r--. 1 root root 7 Dec 3 09:55 2.txt 11drwxr-xr-x. 2 root root 6 Dec 3 10:21 a 12lrwxrwxrwx. 1 root root 1 Dec 3 10:21 a_s_link -> a 13drwxr-xr-x. 2 root root 6 Dec 3 10:21 b -
相对路径
硬链接:原始文件相对路径是相对于当前工作目录
软链接:原始文件的相对路径是相对于链接文件的相对路径1# pwd 2/root/tmp 3[root@localhost tmp]# ls 41.txt 2.txt a a_s_link b 5[root@localhost tmp]#cd /root/link_test 6# ln ../tmp/2.txt link_2.txst # 硬链接的源文件路径相对于当前工作目录 7[root@localhost link_test]# ls -l 8total 4 9-rw-r--r--. 2 root root 7 Dec 3 09:55 link_2.txst 10[root@localhost link_test]# stat link_2.txst 11File: ‘link_2.txst’ 12Size: 7 Blocks: 8 IO Block: 4096 regular file 13Device: 803h/2051d Inode: 67831653 Links: 2 14Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 15Context: unconfined_u:object_r:admin_home_t:s0 16Access: 2020-12-03 09:55:22.936055281 +0800 17Modify: 2020-12-03 09:55:35.404055743 +0800 18Change: 2020-12-03 10:28:26.324128842 +0800 19Birth: - 20[root@localhost link_test]# stat /root/tmp/2.txt 21File: ‘/root/tmp/2.txt’ 22Size: 7 Blocks: 8 IO Block: 4096 regular file 23Device: 803h/2051d Inode: 67831653 Links: 2 24Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 25Context: unconfined_u:object_r:admin_home_t:s0 26Access: 2020-12-03 09:55:22.936055281 +0800 27Modify: 2020-12-03 09:55:35.404055743 +0800 28Change: 2020-12-03 10:28:26.324128842 +0800 29Birth: - 30 31# pwd 32/root 33# ln -s ./tmp/2.txt link_s_2.txt #软连接的源文件的相对路径,相对软连接文件的相对路径 34[root@localhost ~]# ls -l 35total 8 36-rw-------. 1 root root 1582 Dec 12 2022 anaconda-ks.cfg 37-rw-r--r--. 1 root root 18 Dec 2 18:06 EOF 38lrwxrwxrwx. 1 root root 11 Dec 3 10:33 link_s_2.txt -> ./tmp/2.txt 39drwxr-xr-x. 2 root root 25 Dec 3 10:28 link_test 40drwxr-xr-x. 4 root root 66 Dec 3 10:21 tmp -
删除源文件
硬链接:只是链接数减一,但链接文件的访问不受影响
软链接:链接文件将无法访问1# pwd 2/root/tmp 3# ln 1.txt link_1.txt #创建硬链接、软连接 4# ln -s 2.txt lin_s_2.txt 5# ls -l 6total 12 7-rw-r--r--. 2 root root 4 Dec 3 09:38 1.txt 8-rw-r--r--. 2 root root 7 Dec 3 09:55 2.txt 9drwxr-xr-x. 2 root root 6 Dec 3 10:21 a 10lrwxrwxrwx. 1 root root 1 Dec 3 10:21 a_s_link -> a 11drwxr-xr-x. 2 root root 6 Dec 3 10:21 b 12-rw-r--r--. 2 root root 4 Dec 3 09:38 link_1.txt 13lrwxrwxrwx. 1 root root 5 Dec 3 10:44 lin_s_2.txt -> 2.txt 14# rm -rf 1.txt 2.txt # 删除源文件 15[root@localhost tmp]# ls -l 16total 4 17drwxr-xr-x. 2 root root 6 Dec 3 10:21 a 18lrwxrwxrwx. 1 root root 1 Dec 3 10:21 a_s_link -> a 19drwxr-xr-x. 2 root root 6 Dec 3 10:21 b 20-rw-r--r--. 1 root root 4 Dec 3 09:38 link_1.txt 21lrwxrwxrwx. 1 root root 5 Dec 3 10:44 lin_s_2.txt -> 2.txt 22# cat link_1.txt #硬链接正常访问 23abc 24[root@localhost tmp]# cat lin_s_2.txt #软连接无法访问 25cat: lin_s_2.txt: No such file or directory 26[root@localhost tmp]# -
文件类型
硬链接:和源文件相同
软链接:链接文件,和源文件无关1# pwd 2/root/tmp 3# ln 1.txt link_1.txt #创建硬链接、软连接 4# ln -s 2.txt lin_s_2.txt 5# ls -l 6total 12 7-rw-r--r--. 2 root root 4 Dec 3 09:38 1.txt 8-rw-r--r--. 2 root root 7 Dec 3 09:55 2.txt 9drwxr-xr-x. 2 root root 6 Dec 3 10:21 a 10lrwxrwxrwx. 1 root root 1 Dec 3 10:21 a_s_link -> a 11drwxr-xr-x. 2 root root 6 Dec 3 10:21 b 12-rw-r--r--. 2 root root 4 Dec 3 09:38 link_1.txt #硬链接的文件类型是-,与源文件1.txt相同 13lrwxrwxrwx. 1 root root 5 Dec 3 10:44 lin_s_2.txt -> 2.txt #软连接的文件类型是l,与源文件2.txt不同四、Linux文件管理命令基础
pwd命令: printing working directory。
1# pwd 2/root/tmp命令 cd : change directory 改变目录。可以使用绝对或相对路径:
切换至父目录: cd ..
切换至当前用户主目录: cd
切换至以前的工作目录: cd -1# cd /etc/sysconfig/ 2[root@localhost sysconfig]# pwd 3/etc/sysconfig 4# cd .. 5[root@localhost etc]# pwd 6/etc 7# cd - 8/etc/sysconfig 9# cd 10[root@localhost ~]# pwd 11/rootls 命令可以列出当前目录的内容或指定目录。
1]# ls 2anaconda-ks.cfg EOF link_s_2.txt link_test tmp 3[root@localhost ~]# ls -l 4total 8 5-rw-------. 1 root root 1582 Dec 12 2022 anaconda-ks.cfg 6-rw-r--r--. 1 root root 18 Dec 2 18:06 EOF 7lrwxrwxrwx. 1 root root 11 Dec 3 10:33 link_s_2.txt -> ./tmp/2.txt 8drwxr-xr-x. 2 root root 25 Dec 3 10:28 link_test 9drwxr-xr-x. 4 root root 77 Dec 3 10:45 tmp查看文件状态 stat 。
1# stat anaconda-ks.cfg 2File: ‘anaconda-ks.cfg’ 3Size: 1582 Blocks: 8 IO Block: 4096 regular file 4Device: 803h/2051d Inode: 134337602 Links: 1 5Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root) 6Context: system_u:object_r:admin_home_t:s0 7Access: 2022-12-12 11:11:11.000000000 +0800 8Modify: 2022-12-12 11:11:11.000000000 +0800 9Change: 2020-12-02 22:33:35.250538297 +0800 10Birth: -利用echo命令显示字符串或者创建文件。
# echo 112233 > 0.txt利用 cp(copy)命令可以实现文件或目录的复制。
# cp 0.txt 00.txt利用cat命令显示文件内容。
1# cat 00.txt 2112233利用rm命令删除文件。
1# rm -rf 00.txt 2# ls -l 3total 8 4-rw-r--r--. 1 root root 7 Dec 3 11:09 0.txt 5drwxr-xr-x. 2 root root 6 Dec 3 10:21 a 6lrwxrwxrwx. 1 root root 1 Dec 3 10:21 a_s_link -> a 7drwxr-xr-x. 2 root root 6 Dec 3 10:21 b 8-rw-r--r--. 1 root root 4 Dec 3 09:38 link_1.txt 9lrwxrwxrwx. 1 root root 5 Dec 3 10:44 lin_s_2.txt -> 2.txt利用mv命令移动并更改文件名字。
# mv 0.txt /root/xsd.txt利用vim创建并编辑文件。
# vim file.txt
五、删除文件行首空白字符
1方法一: 2# pwd 3/tmp 4# cp /etc/profile profile 5# sed 's/^[ \t]*//g' profile #利用sed工具删除profile文件行首的空白字符 6# /etc/profile 7 8# System wide environment and startup programs, for login setup 9# Functions and aliases go in /etc/bashrc 10 11# It's NOT a good idea to change this file unless you know what you 12# are doing. It's much better to create a custom.sh shell script in 13# /etc/profile.d/ to make custom changes to your environment, as this 14# will prevent the need for merging in future updates. 15 16pathmunge () { 17case ":${PATH}:" in 18*:"$1":*) 19;; 20*) 21if [ "$2" = "after" ] ; then 22PATH=$PATH:$1 23else 24PATH=$1:$PATH 25fi 26esac 27} 28 29if [ -x /usr/bin/id ]; then 30if [ -z "$EUID" ]; then 31# ksh workaround 32EUID=`/usr/bin/id -u` 33UID=`/usr/bin/id -ru` 34fi 35USER="`/usr/bin/id -un`" 36LOGNAME=$USER 37MAIL="/var/spool/mail/$USER" 38fi 39 40# Path manipulation 41if [ "$EUID" = "0" ]; then 42pathmunge /usr/sbin 43pathmunge /usr/local/sbin 44else 45pathmunge /usr/local/sbin after 46pathmunge /usr/sbin after 47fi 48 49HOSTNAME=`/usr/bin/hostname 2>/dev/null` 50HISTSIZE=1000 51if [ "$HISTCONTROL" = "ignorespace" ] ; then 52export HISTCONTROL=ignoreboth 53else 54export HISTCONTROL=ignoredups 55fi 56 57export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL 58 59export HISTTIMEFORMAT="%F %T `whoami` " 60# By default, we want umask to get set. This sets it for login shell 61# Current threshold for system reserved uid/gids is 200 62# You could check uidgid reservation validity in 63# /usr/share/doc/setup-*/uidgid file 64if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then 65umask 002 66else 67umask 022 68fi 69 70for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do 71if [ -r "$i" ]; then 72if [ "${-#*i}" != "$-" ]; then 73. "$i" 74else 75. "$i" >/dev/null 76fi 77fi 78done 79 80unset i 81unset -f pathmunge 82 83 84方法二: 85# pwd 86/tmp 87HOSTNAME=`/usr/bin/hostname 2>/dev/null` 88HISTSIZE=1000 89if [ "$HISTCONTROL" = "ignorespace" ] ; then 90 export HISTCONTROL=ignoreboth 91else 92 export HISTCONTROL=ignoredups 93fi 94 95export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL 96 97export HISTTIMEFORMAT="%F %T `whoami` " 98# By default, we want umask to get set. This sets it for login shell 99# Current threshold for system reserved uid/gids is 200 100# You could check uidgid reservation validity in 101# /usr/share/doc/setup-*/uidgid file 102if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then 103 umask 002 104else 105 umask 022 106fi 107 108for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do 109 if [ -r "$i" ]; then 110 if [ "${-#*i}" != "$-" ]; then 111 . "$i" 112 else 113 . "$i" >/dev/null 114 fi 115 fi 116done 117 118unset i 119unset -f pathmunge 120:%s@^[[:space:]]\+@@g #查找行首空格并替换
六、在vim中设置tab缩进为4个空格
1# cd 2# ls -a 3# vim .vimrc 4set et 5set ts=4 6# cd /tmp 7# vim 11.txt 81 2 9 10~ 11 12-- INSERT --