Linux-第二周作业

2020-12-04  本文已影响0人  运维少年

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

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

元数据信息(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: -

修改时间戳

[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: -
[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
[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: -

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

# 创建软链接后,文件属性中的链接数不会增加
[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
# 创建硬链接,文件的链接数会增加
[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

硬链接和软链接的区别

# 硬链接
[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
# 软连接
[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 -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
[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
[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: -
[root@Centos8-157 ~]# mv messages messagesbak
[root@Centos8-157 ~]# ll messagesbak 
-rw------- 1 root root 110361 Dec  4 21:14 messagesbak
[root@Centos8-157 ~]# rm messagesbak 
rm: remove regular file 'messagesbak'? y

[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


上一篇 下一篇

猜你喜欢

热点阅读