描述Linux发行版的系统目录名称命名规则以及用途

1、描述Linux发行版的系统目录名称命名规则以及用途。

Linux发行版的系统目录
[15:55:29root@centos8/]#tree -L 1
.
├── bin -> usr/bin
├── boot
├── data
├── dev
├── etc
├── home
├── lib -> usr/lib
├── lib64 -> usr/lib64
├── media
├── misc
├── mnt
├── net
├── opt
├── proc
├── root
├── run
├── sbin -> usr/sbin
├── srv
├── sys
├── tmp
├── usr
└── var

Linux系统命名规则

  • 文件和目录被组织成一个单根倒置树结构
  • 文件系统从根目录下开始,用"/"表示
  • 严格区分字符的大小写
  • 文件名可以使用除斜杠(/)以外的任意字符
  • 文件名最长255个字节
  • 包括路径在内文件名称最长4095个字节
  • 除了斜杠和NUL,所有字符都有效。

各个目录的用途

  • /boot 系统启动引导文件,内核文件(vmlinuz)以及引导加载器(bootloader,grub)都存 放在次目录下
  • /bin 所有用户使用的基本命令;不能关联至独立分区,OS启动即会用到的程序
  • /sbin 管理类的基本命令;不能关联至独立分区,OS启动即会用到的程序
  • /lib 存放系统启动时程序依赖的基本共享库文件和内核模块文件(/lib/modules)
  • /lib64 专用于x86_64系统上的辅助共享库文件存放位置
  • /etc 配置文件目录
  • /home 普通用户的家目录
  • /root 管理员的家目录
  • /media 移动式设备的挂载点
  • /mnt 临时文件系统挂载点
  • /dev 设备文件和特殊文件存储位置
  • /opt 第三方引用程序的安装位置
  • /srv 系统上运行服务用到的数据
  • /tmp 临时文件存储位置
  • /usr 全局共享的只读数据路径
  • /var 存储常发生变化的数据库的目录 一般情况下,使用较多的时 /var/log 日志目录
  • /proc 内存映射,用于输出内核与进程信息相关的虚拟文件系统
  • /sys 用于输出当前系统上硬件设备相关信息虚拟文件系统

2、描述文件的元数据信息有哪些,分别表示什么含义,如何查看?如何修改文件的时间戳信息?

文件中的元数据信息

File: 文件名
Size: 文件大小(单位:B)
Blocks: 文件所占块个数
Inode: 文件的索引节点号
Links: 硬链接次数
UID: 文件属主
GID:文件所属组
三个时间戳
access time:访问时间,简写atime,读写文件内容
modify time: 修改时间,mtime,改变文件内容(数据)
change time:改变时间,ctime,元数据发生改变

查看文件元数据的方法:stat + 文件名

[18:12:48lqf@centos8~]$stat test.txt
  File: test.txt
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 10302h/66306d   Inode: 134322331   Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/     lqf)   Gid: ( 1000/     lqf)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2020-12-05 18:12:39.481889538 +0800
Modify: 2020-12-05 18:12:39.481889538 +0800
Change: 2020-12-05 18:12:39.481889538 +0800
 Birth: -

修改文件时间戳信息的方法

touch 命令,格式:touch [OPTION] FILE
选项说明:
-a 仅改变 atime和ctime
-m 仅改变 mtime和ctime
-t [[CC]YY]MMDDhhmm[.ss],指定atime和mtime的时间戳
-c 如果文件不存在,则不予创建

3、总结软连接和硬连接区别,并用实例操作说明

(1)硬链接与原文件指向同一个物理文件,具有相同的inode号,创建硬链接时链接数递增。下图比较了对f1文件建立硬链接前后的文件信息

[18:29:40lqf@centos8~]$ll -i f1
134322331 -rw-rw-r--. 1 lqf lqf 0 Dec  5 18:29 f1
[18:29:46lqf@centos8~]$ln f1 f1_hlink
[18:30:21lqf@centos8~]$ll -i f1*
134322331 -rw-rw-r--. 2 lqf lqf 0 Dec  5 18:29 f1
134322331 -rw-rw-r--. 2 lqf lqf 0 Dec  5 18:29 f1_hlink

(2)软链接与原文件是不同的文件,具有不同的inode号,创建软链接时链接数不变。软链接是一个指向原文件的符号链接,类似于windows中的快捷方式。下图比较了对文件f2建立软链接前后的文件信息

[18:35:08lqf@centos8~]$ll -i f2
134314087 -rw-rw-r--. 1 lqf lqf 0 Dec  5 18:35 f2
[18:35:16lqf@centos8~]$ln -s f2 f2_slink
[18:35:37lqf@centos8~]$ll -i f2*
134314087 -rw-rw-r--. 1 lqf lqf 0 Dec  5 18:35 f2
134734741 lrwxrwxrwx. 1 lqf lqf 2 Dec  5 18:35 f2_slink -> f2

(3)创建硬链接时原文件的路径相对的是当前工作目录,创建软链接时原文件的路径相对的是软链接的文件路径。如下所示:

[18:39:42lqf@centos8~]$ln -s ../f1 dir1/f1_slink
[18:40:26lqf@centos8~]$ll dir1/
total 0
lrwxrwxrwx. 1 lqf lqf 5 Dec  5 18:40 f1_slink -> ../f1

(4)创建硬链接时不能跨硬盘分区;软链接可以跨硬盘分区创建。如下所示:

[18:49:31root@centos8~]#ln f1 /data/f1_hlink
ln: failed to create hard link '/data/f1_hlink' => 'f1': Invalid cross-device link
[18:49:49root@centos8~]#ln -s f1 /data/f1_slink
[18:50:01root@centos8~]#ll /data/f1_slink
lrwxrwxrwx. 1 root root 2 Dec  5 18:50 /data/f1_slink -> f1

(5)不能对目录创建硬链接,但是可以对目录创建软链接。如下所示:

[18:53:52root@centos8~]#ln dir1 dir1_hlink
ln: dir1: hard link not allowed for directory
[18:54:02root@centos8~]#ln -s dir1 dir1_slink
[18:54:17root@centos8~]#ll
total 0
drwxr-xr-x. 2 root root 6 Dec  5 18:53 dir1
lrwxrwxrwx. 1 root root 4 Dec  5 18:54 dir1_slink -> dir1

4、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。

  • ls
    查看目录下文件的详细信息
    ls 选项[-ald] [文件或目录]
    -a显示所有文件,包括隐藏文件
    -l 详细信息显示
    -d 查看目录属性

  • cd
    切换目录
    cd / (切换到根目录)
    cd .. (回到上一级目录)

  • pwd
    显示当前所在的工作目录

  • tree
    tree -L level指定要显示的层级
    引用命令的执行结果
    $(command)或者command
    bash的基于特性:引用
    "强引用"
    "弱引用"
    "命令引用"
    bash基础特性:快捷键
    ctrl+a 跳转至命令行行首
    ctrl+e 跳转至命令行行尾
    ctrl+u 删除行首至光标
    ctrl+k 删除行尾至光标
    ctrl+l 清屏 相当于clear

文件查看命令:cat,tac,head,tail,more,less

  • stat
    用法 stat file
    Access: 2018-05-03 05:39:38.954690311 +0800 最近访问时间
    Modify: 2018-05-03 05:03:07.815759609 +0800 最近更改时间
    Change: 2018-05-03 05:03:07.815759609 +0800 最近改动时间 元数据改变

  • touch
    改变文件的时间stat信息
    -c 指定的文件路径不存在时不创建
    -a 仅修改access time
    -m 仅修改modify time
    -t 改为指定时间yymmddhhmm.ss

文件管理工具:cp,mv,rm

  • cp
    copy
    源文件:目标文件
    cp [OPTION]… [-T] SOURCE DEST
    cp [OPTION]… SOURCE… DIRECTORY
    cp [OPTION]… -t DIRECTORY SOURCE..
    常用选项
    -i 交互式复制,即覆盖之前提醒用户确认
    -f 强制覆盖目标文件
    -r 递归复制目录
    -d 复制符号链接文件本身,而非其指向文件
    -a -dr –preserve=all,archive,用于实现归档

  • mv
    move
    -i 交互式
    -f force

  • rm
    remove
    -f force
    -i 交互式 interact
    -r recursive 递归
    删除目录 rm -rf /PATH
    rm -rf / 危险操作
    注意,所有文件不建议直接删除,而是移动到某个专用目录

5、复制/etc/profile至/tmp/目录,用查找替换命令删除/tmp/profile文件中的行首的空白字符。在命令模式下输入 :%s@^[[:blank:]]+@@g

[20:26:22root@centos8tmp]#vim 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
}

:%s@^[[:blank:]]\+@@g

以下是执行结果

[20:26:22root@centos8tmp]#vim profile

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
        
if [ -n "${BASH_VERSION-}" ] ; then 
if [ -f /etc/bashrc ] ; then
# Bash login shells run only /etc/profile
# Bash non-login shells run only /etc/bashrc
# Check for double sourcing is done in /etc/bashrc.
. /etc/bashrc
fi                                                                                                                                    
fi
39 substitutions on 39 lines                                                                                        84,1          Bot

6、在vim中设置tab缩进为4个字符

需要设置两行
set et
set ts=4

[20:40:26root@centos8~]#vim .vimrc

set et
set ts=4                                                                                                                              
set ignorecase
set cursorline
set autoindent
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
    if expand("%:e") == 'sh'
    call setline(1,"#!/bin/bash")
    call setline(2,"#")
    call setline(3,"#***************************************************************************")
    call setline(4,"#Author:            lqf")
    call setline(5,"#QQ:                3938")
    call setline(6,"#Date:              ".strftime("%Y-%m-%d"))
    call setline(7,"#FileName:          ".expand("%"))
    call setline(8,"#URL:               http://www.magedu.com")
    call setline(9,"#Description:           The test script")
    call setline(10,"#Coryright (c):                 ".strftime("%Y")." All rights reserved")
    call setline(11,"#**************************************************************************")
    call setline(12,"")
    endif
endfunc
autocmd BufNewFile * normal G
".vimrc" 23L, 820C                                                                                                  2,1           All

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容