Linux-第二周作业

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

  • /etc :保存配置文件信息
  • /proc:保存系统运行状态信息
  • /home:保存用户家目录数据
  • /bin/bin64:可执行命令文件
  • /usr:用户配置文件、软件安装路径、用户可执行命令等
  • /lib/lib64:系统和软件库文件路径
  • /var:主要是存放系统日志
  • /boor:启动内核
  • /run:系统运行时的pid和socket、lock信息
  • /dev:块设备文件信息
  • /root:root用户信息

第二题 描述文件的元数据信息有哪些,分别标识什么含义,如何查看,如何修改文件的时间戳信息

元数据信息(inode信息、时间戳信息、文件大小、链接数、块大小、权限等)

[root@Centos8-157 ~]# stat nohup.out 
  File: nohup.out
  Size: 11066       Blocks: 24         IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 404108516   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-11-24 15:11:37.513000000 +0800
Modify: 2020-12-03 09:41:57.887000000 +0800
Change: 2020-12-03 09:41:57.887000000 +0800
 Birth: -
  • Size:文件大小
  • Inode:inode编号,Linux内部将inode编号唯一标识了一个文件。
  • Blocks:使用了多少个数据块
  • IO Block:IO块大小
  • Device:使用的设备
  • Links:链接数
  • Access:权限
  • AccessModifyChange:访问、修改内容、修改属性时间。

修改时间戳

  • Access:访问时间`atime,使用cat查看文件后,文件的atime就会变化
[root@Centos8-157 ~]# stat cp.sh 
  File: cp.sh
  Size: 30          Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 403594145   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-11-04 20:41:03.208000000 +0800
Modify: 2020-11-04 20:41:01.231000000 +0800
Change: 2020-11-04 20:41:01.248000000 +0800
 Birth: -
[root@Centos8-157 ~]# cat cp.sh 
\cp -af /etc/redhat-release .
[root@Centos8-157 ~]# stat cp.sh 
  File: cp.sh
  Size: 30          Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 403594145   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-12-04 21:30:01.970000000 +0800
Modify: 2020-11-04 20:41:01.231000000 +0800
Change: 2020-11-04 20:41:01.248000000 +0800
 Birth: -
  • ‘Modify’:mtime最后一次修改内容时间,修改内容时间就会变化
[root@Centos8-157 ~]# stat cp.sh 
  File: cp.sh
  Size: 30          Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 403594145   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-12-04 21:30:01.970000000 +0800
Modify: 2020-11-04 20:41:01.231000000 +0800
Change: 2020-11-04 20:41:01.248000000 +0800
 Birth: -
[root@Centos8-157 ~]# echo 123 >> cp.sh 
[root@Centos8-157 ~]# stat cp.sh 
  File: cp.sh
  Size: 34          Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 403594145   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-12-04 21:30:01.970000000 +0800
Modify: 2020-12-04 21:31:55.073000000 +0800
Change: 2020-12-04 21:31:55.073000000 +0800
  • change:ctime,一般文件的元数据发生变化时(权限、大小等),ctime就会跟着变
[root@Centos8-157 ~]# stat cp.sh 
  File: cp.sh
  Size: 34          Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 403594145   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-12-04 21:30:01.970000000 +0800
Modify: 2020-12-04 21:31:55.073000000 +0800
Change: 2020-12-04 21:31:55.073000000 +0800
 Birth: -
[root@Centos8-157 ~]# chmod 600 cp.sh 
[root@Centos8-157 ~]# stat cp.sh 
  File: cp.sh
  Size: 34          Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 403594145   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-12-04 21:30:01.970000000 +0800
Modify: 2020-12-04 21:31:55.073000000 +0800
Change: 2020-12-04 21:33:12.642000000 +0800
 Birth: -

第三题 软连接和硬链接的区别

  • 软链接:软链接类似于Windows的快捷方式
# 创建软链接后,文件属性中的链接数不会增加
[root@Centos8-157 ~]# ln -s cp.sh cp2.sh
[root@Centos8-157 ~]# ll cp2.sh 
lrwxrwxrwx 1 root root 5 Dec  4 21:35 cp2.sh -> cp.sh
[root@Centos8-157 ~]# ll cp.sh 
-rw------- 1 root root 34 Dec  4 21:31 cp.sh

# 删除软链接文件不会影响源文件
[root@Centos8-157 ~]# rm -rf cp2.sh 
[root@Centos8-157 ~]# cat cp.sh 
\cp -af /etc/redhat-release .
123

# 删除源文件会影响链接文件
[root@Centos8-157 ~]# ll cp2.sh 
lrwxrwxrwx 1 root root 5 Dec  4 21:37 cp2.sh -> cp.sh
[root@Centos8-157 ~]# ll cp.sh
-rw------- 1 root root 34 Dec  4 21:31 cp.sh
[root@Centos8-157 ~]# rm cp.sh
rm: remove regular file 'cp.sh'? y
[root@Centos8-157 ~]# cat cp2.sh 
cat: cp2.sh: No such file or directory
[root@Centos8-157 ~]# ls -l cp2.sh 
lrwxrwxrwx 1 root root 5 Dec  4 21:37 cp2.sh -> cp.sh
  • 硬链接:硬链接类似于windows的副本
# 创建硬链接,文件的链接数会增加
[root@Centos8-157 ~]# ln -v cp.sh cp2.sh
'cp2.sh' => 'cp.sh'
[root@Centos8-157 ~]# ll cp.sh
-rw------- 2 root root 34 Dec  4 21:31 cp.sh
[root@Centos8-157 ~]# ll cp2.sh 
-rw------- 2 root root 34 Dec  4 21:31 cp2.sh

# 删除源文件或者目标文件都不会影响对方
[root@Centos8-157 ~]# rm -rf cp2.sh 
[root@Centos8-157 ~]# cat cp.sh
\cp -af /etc/redhat-release .
123
[root@Centos8-157 ~]# ln -v cp.sh cp2.sh
'cp2.sh' => 'cp.sh'
[root@Centos8-157 ~]# rm -f cp.sh
[root@Centos8-157 ~]# cat cp2.sh 
\cp -af /etc/redhat-release .
123

硬链接和软链接的区别

  • 硬链接本质上是同一个文件(inode相同),而软链接不是
# 硬链接
[root@Centos8-157 ~]# ll -i cp.sh cp2.sh 
403594145 -rw------- 2 root root 34 Dec  4 21:31 cp2.sh
403594145 -rw------- 2 root root 34 Dec  4 21:31 cp.sh

# 软链接
[root@Centos8-157 ~]# ll -i nohup.out no.out 
404108516 -rw------- 1 root root 11066 Dec  3 09:41 nohup.out
404108520 lrwxrwxrwx 1 root root     9 Dec  4 21:44 no.out -> nohup.out
  • 软链接支持跨设备,硬链接不支持(因为硬链接inode一样,所以不能跨设备)
# 软连接
[root@Centos8-157 ~]# ln -sv nohup.out /data/disk1/no1.out
'/data/disk1/no1.out' -> 'nohup.out'

# 硬链接
[root@Centos8-157 ~]# ln -v cp.sh /data/disk1/cp2.sh
ln: failed to create hard link '/data/disk1/cp2.sh' => 'cp.sh': Invalid cross-device link

  • 软连接支持文件夹,硬链接不支持文件夹
# 软链接
[root@Centos8-157 ~]# ln -sv /var/log /opt/log1
'/opt/log1' -> '/var/log'

# 硬链接
[root@Centos8-157 ~]# ln -v /var/log /opt/log2
ln: /var/log: hard link not allowed for directory

  • 目标文件类型不同
# 软链接 - 文件类型和属性和源端不一样
[root@Centos8-157 ~]# ll no.out 
lrwxrwxrwx 1 root root 9 Dec  4 21:44 no.out -> nohup.out

# 硬链接 - 文件类型和属性和源端保持一致
[root@Centos8-157 ~]# ll cp2.sh 
-rw------- 2 root root 34 Dec  4 21:31 cp2.sh

第四题 Linux上文件管理类的命令都有哪些,其常用的办法及相关的示例演示

  • ls:查看文件类表和属性
# ls -l 查看文件列表,以长格式信息输出
[root@Centos8-157 ~]# ls -l no.out 
lrwxrwxrwx 1 root root 9 Dec  4 21:44 no.out -> nohup.out

# ls -i 查看文件的inode信息
[root@Centos8-157 ~]# ls -i no.out 
404108520 no.out

# 查看文件,并按照时间排序
[root@Centos8-157 ~]# ls -tlr
total 10164
lrwxrwxrwx. 1 root root      14 Jun  3  2020 redhat-release.~3~ -> centos-release
lrwxrwxrwx. 1 root root      14 Jun  3  2020 redhat-release.~1~ -> centos-release
lrwxrwxrwx. 1 root root      14 Jun  3  2020 redhat-release~ -> centos-release
lrwxrwxrwx. 1 root root      14 Jun  3  2020 redhat-release -> centos-release
-rw-r--r--  1 root root 8345123 Nov  3 19:33 frp_0.29.0_linux_amd64.tar.gz
-rw-------. 1 root root    1540 Nov  4 20:14 anaconda-ks.cfg
-rw-r--r--  1 root root      38 Nov  4 20:38 redhat-release.~2~
-rw-r--r--  1 root root 2027520 Nov 24 13:35 cmatrix-v2.0-Butterscotch.tar
-rw-------  1 root root   11066 Dec  3 09:41 nohup.out
-rw-------  1 root root      34 Dec  4 21:31 cp.sh.bak
-rw-------  2 root root      34 Dec  4 21:31 cp.sh
-rw-------  2 root root      34 Dec  4 21:31 cp2.sh
lrwxrwxrwx  1 root root       9 Dec  4 21:44 no.out -> nohup.out
  • catlessmore:查看文件内容

  • file:查看文件类型

[root@Centos8-157 ~]# file no.out 
no.out: symbolic link to nohup.out
[root@Centos8-157 ~]# file cp.sh
cp.sh: ASCII text
[root@Centos8-157 ~]# file /var
/var: directory
  • stat:查看文件元数据信息
[root@Centos8-157 ~]# stat cp.sh
  File: cp.sh
  Size: 34          Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 403594145   Links: 2
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-12-04 21:57:42.393000000 +0800
Modify: 2020-12-04 21:31:55.073000000 +0800
Change: 2020-12-04 21:44:20.211000000 +0800
 Birth: -
  • touch:创建文件

  • vivim:编辑文件

  • mv:移动文件

[root@Centos8-157 ~]# mv messages messagesbak
[root@Centos8-157 ~]# ll messagesbak 
-rw------- 1 root root 110361 Dec  4 21:14 messagesbak
  • rm:删除文件
[root@Centos8-157 ~]# rm messagesbak 
rm: remove regular file 'messagesbak'? y

  • cp:复制文件
[root@Centos8-157 ~]# ll messages 
-rw------- 1 root root 110361 Dec  4 21:14 messages

第五题 用查找替换命令删除profile文件中行首的空白字符

[root@Centos8-157 disk1]# sed -Ei "/^[[:space:]]/s/[[:space:]]+(.*)/\1/g" profile 
[root@Centos8-157 disk1]# cat 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

# 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

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
export HISTTIMEFORMAT="%F %T `whoami` "
[root@Centos8-157 disk1]# c


你可能感兴趣的:(Linux-第二周作业)