Linux系统文件管理基础

Stella981
• 阅读 522

Linux系统文件管理基础

一、Linux系统文件系统

1.1 Linux文件系统结构

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
  • 链接数(指向这个文件名路径名称个数)
  • 该文件的大小和不同的时间戳
  • 指向磁盘上文件的数据块指针
  • 有关文件的其他数据

Linux系统文件管理基础

2.2 文件系统如何查找文件

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

Linux系统文件管理基础

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

Linux系统文件管理基础

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命令。

# df -hi
Filesystem     Inodes IUsed IFree IUse% Mounted on
devtmpfs         110K   393  110K    1% /dev
tmpfs            113K     1  113K    1% /dev/shm
tmpfs            113K   748  112K    1% /run
tmpfs            113K    16  113K    1% /sys/fs/cgroup
/dev/sda3         59M   41K   59M    1% /
/dev/sda1        512K   332  512K    1% /boot
tmpfs            113K     1  113K    1% /run/user/0

查看每个inode节点的大小,可以用如下命令:

# df -T
Filesystem     Type     1K-blocks    Used Available Use% Mounted on
devtmpfs       devtmpfs    450284       0    450284   0% /dev
tmpfs          tmpfs       460984       0    460984   0% /dev/shm
tmpfs          tmpfs       460984    7428    453556   2% /run
tmpfs          tmpfs       460984       0    460984   0% /sys/fs/cgroup
/dev/sda3      xfs      122622468 1836724 120785744   2% /
/dev/sda1      xfs        1038336  163080    875256  16% /boot
tmpfs          tmpfs        92200       0     92200   0% /run/user/0
[root@localhost ~]#
# xfs_info /dev/sda3
meta-data=/dev/sda3              isize=512    agcount=4, agsize=7667648 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=30670592, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=14975, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]#

由于每个文件都必须有一个inode,因此有可能发生inode已经用光,但是硬盘还未存满的情况。这时,就无法在硬盘上创建新文件。

ls命令只列出目录文件中的所有文件名,ls -i命令列出整个目录文件,即文件名和inode号码:

# ls -i /etc
 67547984 adjtime                   67110248 host.conf                  67547947 rc0.d
 67110239 aliases                   67108942 hostname                   67159133 rc1.d
 67746346 aliases.db                67110249 hosts                      67167319 rc2.d
134466363 alternatives              67110250 hosts.allow                67167320 rc3.d
 67565891 anacrontab                67110251 hosts.deny                 67167321 rc4.d
 67462354 asound.conf               67167297 init.d                     67167322 rc5.d
 67120879 at.deny                   67547942 inittab                    67167323 rc6.d
   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 如果文件不存在,则不予创建

# stat anaconda-ks.cfg
  File: ‘anaconda-ks.cfg’
  Size: 1582            Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 134337602   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:admin_home_t:s0
Access: 2020-11-28 08:32:25.680046954 +0800
Modify: 2020-11-24 01:43:08.007006302 +0800
Change: 2020-11-27 21:35:53.853756153 +0800
 Birth: -
# touch -at 201910101010.10
touch: missing file operand
Try 'touch --help' for more information.
[root@localhost ~]# touch -at 201910101010.10 anaconda-ks.cfg
[root@localhost ~]# stat anaconda-ks.cfg
  File: ‘anaconda-ks.cfg’
  Size: 1582            Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 134337602   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:admin_home_t:s0
Access: 2019-10-10 10:10:10.000000000 +0800
Modify: 2020-11-24 01:43:08.007006302 +0800
Change: 2020-12-02 22:32:20.563535891 +0800
 Birth: -
# touch -ct 202212121111.11 anaconda-ks.cfg
# stat anaconda-ks.cfg
  File: ‘anaconda-ks.cfg’
  Size: 1582            Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 134337602   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:admin_home_t:s0
Access: 2022-12-12 11:11:11.000000000 +0800
Modify: 2022-12-12 11:11:11.000000000 +0800
Change: 2020-12-02 22:33:35.250538297 +0800
 Birth: -

三、硬链接与软连接区别

  1. 本质:
    硬链接:本质是同一个文件
    软链接:本质不是同一个文件

    # echo abc > 1.txt     #新建1.txt文件
    [root@localhost tmp]# stat 1.txt
    File: ‘1.txt’
    Size: 4               Blocks: 8          IO Block: 4096   regular file
    Device: 803h/2051d      Inode: 67831651    Links: 1   #Inode:67831651
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2020-12-03 09:37:37.911015780 +0800
    Modify: 2020-12-03 09:38:02.723016701 +0800
    Change: 2020-12-03 09:38:02.723016701 +0800
    Birth: -
    
    # ln 1.txt link_1.txt    #硬链接
    # stat 1.txt  link_1.txt
    File: ‘1.txt’
    Size: 4               Blocks: 8          IO Block: 4096   regular file
    Device: 803h/2051d      Inode: 67831651    Links: 2 #Inode  Links
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2020-12-03 09:37:37.911015780 +0800
    Modify: 2020-12-03 09:38:02.723016701 +0800
    Change: 2020-12-03 09:41:14.903023828 +0800
    Birth: -
    File: ‘link_1.txt’
    Size: 4               Blocks: 8          IO Block: 4096   regular file
    Device: 803h/2051d      Inode: 67831651    Links: 2  #Inode与源文件相同  Links
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2020-12-03 09:37:37.911015780 +0800
    Modify: 2020-12-03 09:38:02.723016701 +0800
    Change: 2020-12-03 09:41:14.903023828 +0800
    Birth: -
    
    # ln -s 1.txt  link_s_1.txt   #软连接
    # stat 1.txt  link_s_1.txt
    File: ‘1.txt’
    Size: 4               Blocks: 8          IO Block: 4096   regular file
    Device: 803h/2051d      Inode: 67831651    Links: 2  #源文件的Inode  Links没变 
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2020-12-03 09:37:37.911015780 +0800
    Modify: 2020-12-03 09:38:02.723016701 +0800
    Change: 2020-12-03 09:41:14.903023828 +0800
    Birth: -
    File: ‘link_s_1.txt’ -> ‘1.txt’
    Size: 5               Blocks: 0          IO Block: 4096   symbolic link
    Device: 803h/2051d      Inode: 67831652    Links: 1  #新生成的Inode,新的文件
    Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2020-12-03 09:47:04.432036792 +0800
    Modify: 2020-12-03 09:46:55.507036461 +0800
    Change: 2020-12-03 09:46:55.507036461 +0800
    Birth: -
    
  2. 跨设备
    硬链接:不支持
    软链接:支持

    # df -T
    Filesystem     Type     1K-blocks    Used Available Use% Mounted on
    devtmpfs       devtmpfs    450284       0    450284   0% /dev
    tmpfs          tmpfs       460984       0    460984   0% /dev/shm
    tmpfs          tmpfs       460984    7364    453620   2% /run
    tmpfs          tmpfs       460984       0    460984   0% /sys/fs/cgroup
    /dev/sda3      xfs      122622468 1835928 120786540   2% /
    /dev/sda1      xfs        1038336  163080    875256  16% /boot
    tmpfs          tmpfs        92200       0     92200   0% /run/user/0
    
    # echo 112233 > 2.txt
    # ln 2.txt /boot/link_2.txt  #硬链接不能跨设备
    ln: failed to create hard link ‘/boot/link_2.txt’ => ‘2.txt’: Invalid cross-device link
    
    # ln -s 2.txt /boot/link_s_2.txt
    # stat 2.txt
    File: ‘2.txt’
    Size: 7               Blocks: 8          IO Block: 4096   regular file
    Device: 803h/2051d      Inode: 67831653    Links: 1   #源文件的Inode
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2020-12-03 09:55:22.936055281 +0800
    Modify: 2020-12-03 09:55:35.404055743 +0800
    Change: 2020-12-03 09:55:35.404055743 +0800
    Birth: -
    # stat /boot/link_s_2.txt
    File: ‘/boot/link_s_2.txt’ -> ‘2.txt’
    Size: 5               Blocks: 0          IO Block: 4096   symbolic link
    Device: 801h/2049d      Inode: 84          Links: 1   #新生成的文件
    Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:boot_t:s0
    Access: 2020-12-03 09:58:38.783062545 +0800
    Modify: 2020-12-03 09:58:38.783062545 +0800
    Change: 2020-12-03 09:58:38.783062545 +0800
    Birth: -
    
  3. inode
    硬链接:相同
    软链接:不同

  4. 链接数
    硬链接:创建新的硬链接,链接数会增加,删除硬链接,链接数减少
    软链接:创建或删除,链接数不会变化

    # stat 1.txt
    File: ‘1.txt’
    Size: 4               Blocks: 8          IO Block: 4096   regular file
    Device: 803h/2051d      Inode: 67831651    Links: 2
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2020-12-03 09:37:37.911015780 +0800
    Modify: 2020-12-03 09:38:02.723016701 +0800
    Change: 2020-12-03 09:41:14.903023828 +0800
    Birth: -
    [root@localhost tmp]# rm link_s_1.txt
    rm: remove symbolic link ‘link_s_1.txt’? y
    [root@localhost tmp]# stat 1.txt
    File: ‘1.txt’
    Size: 4               Blocks: 8          IO Block: 4096   regular file
    Device: 803h/2051d      Inode: 67831651    Links: 2   #删除软连接,链接数没有变化
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2020-12-03 09:37:37.911015780 +0800
    Modify: 2020-12-03 09:38:02.723016701 +0800
    Change: 2020-12-03 09:41:14.903023828 +0800
    Birth: -
    
    # rm -rf link_1.txt
    [root@localhost tmp]# stat 1.txt
    File: ‘1.txt’
    Size: 4               Blocks: 8          IO Block: 4096   regular file
    Device: 803h/2051d      Inode: 67831651    Links: 1  #删除硬链接,链接数减少
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2020-12-03 09:37:37.911015780 +0800
    Modify: 2020-12-03 09:38:02.723016701 +0800
    Change: 2020-12-03 10:18:24.411106518 +0800
    Birth: -
    
  5. 文件夹
    硬链接:不支持
    软链接:支持

    # mkdir a b
    [root@localhost tmp]# ls
    1.txt  2.txt  a  b
    [root@localhost tmp]# ln a a_link  #文件夹硬链接不允许
    ln: ‘a’: hard link not allowed for directory
    # ln -s a a_s_link    #文件夹可以创建软连接
    [root@localhost tmp]# ls -l
    total 8
    -rw-r--r--. 1 root root 4 Dec  3 09:38 1.txt
    -rw-r--r--. 1 root root 7 Dec  3 09:55 2.txt
    drwxr-xr-x. 2 root root 6 Dec  3 10:21 a
    lrwxrwxrwx. 1 root root 1 Dec  3 10:21 a_s_link -> a
    drwxr-xr-x. 2 root root 6 Dec  3 10:21 b
    
  6. 相对路径
    硬链接:原始文件相对路径是相对于当前工作目录
    软链接:原始文件的相对路径是相对于链接文件的相对路径

    # pwd
    /root/tmp
    [root@localhost tmp]# ls
    1.txt  2.txt  a  a_s_link  b
    [root@localhost tmp]#cd /root/link_test
    # ln ../tmp/2.txt  link_2.txst   # 硬链接的源文件路径相对于当前工作目录
    [root@localhost link_test]# ls -l
    total 4
    -rw-r--r--. 2 root root 7 Dec  3 09:55 link_2.txst
    [root@localhost link_test]# stat link_2.txst
    File: ‘link_2.txst’
    Size: 7               Blocks: 8          IO Block: 4096   regular file
    Device: 803h/2051d      Inode: 67831653    Links: 2
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2020-12-03 09:55:22.936055281 +0800
    Modify: 2020-12-03 09:55:35.404055743 +0800
    Change: 2020-12-03 10:28:26.324128842 +0800
    Birth: -
    [root@localhost link_test]# stat /root/tmp/2.txt
    File: ‘/root/tmp/2.txt’
    Size: 7               Blocks: 8          IO Block: 4096   regular file
    Device: 803h/2051d      Inode: 67831653    Links: 2
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2020-12-03 09:55:22.936055281 +0800
    Modify: 2020-12-03 09:55:35.404055743 +0800
    Change: 2020-12-03 10:28:26.324128842 +0800
    Birth: -
    
    # pwd
    /root
    # ln -s ./tmp/2.txt link_s_2.txt    #软连接的源文件的相对路径,相对软连接文件的相对路径
    [root@localhost ~]# ls -l
    total 8
    -rw-------. 1 root root 1582 Dec 12  2022 anaconda-ks.cfg
    -rw-r--r--. 1 root root   18 Dec  2 18:06 EOF
    lrwxrwxrwx. 1 root root   11 Dec  3 10:33 link_s_2.txt -> ./tmp/2.txt
    drwxr-xr-x. 2 root root   25 Dec  3 10:28 link_test
    drwxr-xr-x. 4 root root   66 Dec  3 10:21 tmp
    
  7. 删除源文件
    硬链接:只是链接数减一,但链接文件的访问不受影响
    软链接:链接文件将无法访问

    # pwd
    /root/tmp
    # ln 1.txt link_1.txt        #创建硬链接、软连接
    # ln -s 2.txt lin_s_2.txt
    # ls -l
    total 12
    -rw-r--r--. 2 root root 4 Dec  3 09:38 1.txt
    -rw-r--r--. 2 root root 7 Dec  3 09:55 2.txt
    drwxr-xr-x. 2 root root 6 Dec  3 10:21 a
    lrwxrwxrwx. 1 root root 1 Dec  3 10:21 a_s_link -> a
    drwxr-xr-x. 2 root root 6 Dec  3 10:21 b
    -rw-r--r--. 2 root root 4 Dec  3 09:38 link_1.txt
    lrwxrwxrwx. 1 root root 5 Dec  3 10:44 lin_s_2.txt -> 2.txt
    # rm -rf 1.txt 2.txt    # 删除源文件
    [root@localhost tmp]# ls -l
    total 4
    drwxr-xr-x. 2 root root 6 Dec  3 10:21 a
    lrwxrwxrwx. 1 root root 1 Dec  3 10:21 a_s_link -> a
    drwxr-xr-x. 2 root root 6 Dec  3 10:21 b
    -rw-r--r--. 1 root root 4 Dec  3 09:38 link_1.txt
    lrwxrwxrwx. 1 root root 5 Dec  3 10:44 lin_s_2.txt -> 2.txt
    # cat link_1.txt   #硬链接正常访问
    abc
    [root@localhost tmp]# cat lin_s_2.txt  #软连接无法访问
    cat: lin_s_2.txt: No such file or directory
    [root@localhost tmp]#
    
  8. 文件类型
    硬链接:和源文件相同
    软链接:链接文件,和源文件无关

    # pwd
    /root/tmp
    # ln 1.txt link_1.txt        #创建硬链接、软连接
    # ln -s 2.txt lin_s_2.txt
    # ls -l
    total 12
    -rw-r--r--. 2 root root 4 Dec  3 09:38 1.txt
    -rw-r--r--. 2 root root 7 Dec  3 09:55 2.txt
    drwxr-xr-x. 2 root root 6 Dec  3 10:21 a
    lrwxrwxrwx. 1 root root 1 Dec  3 10:21 a_s_link -> a
    drwxr-xr-x. 2 root root 6 Dec  3 10:21 b
    -rw-r--r--. 2 root root 4 Dec  3 09:38 link_1.txt     #硬链接的文件类型是-,与源文件1.txt相同
    lrwxrwxrwx. 1 root root 5 Dec  3 10:44 lin_s_2.txt -> 2.txt #软连接的文件类型是l,与源文件2.txt不同
    

    四、Linux文件管理命令基础

    pwd命令: printing working directory。

    # pwd
    /root/tmp
    

    命令 cd : change directory 改变目录。可以使用绝对或相对路径:
    切换至父目录: cd ..
    切换至当前用户主目录: cd
    切换至以前的工作目录: cd -

    # cd /etc/sysconfig/
    [root@localhost sysconfig]# pwd
    /etc/sysconfig
    # cd ..
    [root@localhost etc]# pwd
    /etc
    # cd -
    /etc/sysconfig
    # cd
    [root@localhost ~]# pwd
    /root
    

    ls 命令可以列出当前目录的内容或指定目录。

    ]# ls
    anaconda-ks.cfg  EOF  link_s_2.txt  link_test  tmp
    [root@localhost ~]# ls -l
    total 8
    -rw-------. 1 root root 1582 Dec 12  2022 anaconda-ks.cfg
    -rw-r--r--. 1 root root   18 Dec  2 18:06 EOF
    lrwxrwxrwx. 1 root root   11 Dec  3 10:33 link_s_2.txt -> ./tmp/2.txt
    drwxr-xr-x. 2 root root   25 Dec  3 10:28 link_test
    drwxr-xr-x. 4 root root   77 Dec  3 10:45 tmp
    

    查看文件状态 stat 。

    # stat anaconda-ks.cfg
    File: ‘anaconda-ks.cfg’
    Size: 1582            Blocks: 8          IO Block: 4096   regular file
    Device: 803h/2051d      Inode: 134337602   Links: 1
    Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: system_u:object_r:admin_home_t:s0
    Access: 2022-12-12 11:11:11.000000000 +0800
    Modify: 2022-12-12 11:11:11.000000000 +0800
    Change: 2020-12-02 22:33:35.250538297 +0800
    Birth: -
    

    利用echo命令显示字符串或者创建文件。

    # echo  112233 > 0.txt
    

    利用 cp(copy)命令可以实现文件或目录的复制。

    # cp 0.txt 00.txt
    

    利用cat命令显示文件内容。

    # cat 00.txt
    112233
    

    利用rm命令删除文件。

    # rm -rf 00.txt
    # ls -l
    total 8
    -rw-r--r--. 1 root root 7 Dec  3 11:09 0.txt
    drwxr-xr-x. 2 root root 6 Dec  3 10:21 a
    lrwxrwxrwx. 1 root root 1 Dec  3 10:21 a_s_link -> a
    drwxr-xr-x. 2 root root 6 Dec  3 10:21 b
    -rw-r--r--. 1 root root 4 Dec  3 09:38 link_1.txt
    lrwxrwxrwx. 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
    

五、删除文件行首空白字符

方法一:
# pwd
/tmp
# cp /etc/profile profile
# sed 's/^[ \t]*//g' profile    #利用sed工具删除profile文件行首的空白字符
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}

if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
# ksh workaround
EUID=`/usr/bin/id -u`
UID=`/usr/bin/id -ru`
fi
USER="`/usr/bin/id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fi

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
export HISTCONTROL=ignoreboth
else
export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

export HISTTIMEFORMAT="%F %T `whoami` "
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
umask 002
else
umask 022
fi

for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done

unset i
unset -f pathmunge


方法二:
# pwd
/tmp
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

export HISTTIMEFORMAT="%F %T `whoami` "
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge
:%s@^[[:space:]]\+@@g    #查找行首空格并替换

六、在vim中设置tab缩进为4个空格

# cd
# ls -a
# vim .vimrc
set et
set ts=4
# cd /tmp
# vim 11.txt
1   2

~

-- INSERT --
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
2年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
2个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这