练习-Linux 命令-文件相关

练习 - 文件相关

概念类

1、Linux 系统中文件类型一共有几种,分别是什么?使用哪个命令可以查看文件的类型?

答:文件类型共有7种,分别是:
普通文件(-),目录文件(d),块设备文件(b),字符设备文件(c),管道文件(p),链接文件(l),套接字文件(s)

使用ls -l 命令查看指定文件,显示出文件信息的第一个字符代表文件类型
[root@centos8 ~]#ls -l /etc/issue
-rw-r--r--. 1 root root 23 Jan  2  2020 /etc/issue

使用stat filename 命令查看文件类型
[root@centos8 ~]#stat /etc/issue
  File: /etc/issue
  Size: 23        	Blocks: 8          IO Block: 4096   regular file
Device: 10302h/66306d	Inode: 134346126   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:etc_t:s0
Access: 2020-08-01 15:00:28.005010991 +0800
Modify: 2020-01-02 23:21:12.000000000 +0800
Change: 2020-07-30 09:33:28.610680427 +0800
 Birth: -

2、软链接和硬链接的区别,使用 cp 命令复制文件和创建硬链接文件有什么区别

软链接和硬链接的区别:
	1 本质不同:软链接是链接文件,
	2 是否可跨分区:软链接支持,硬链接不支持
	3 文件类型:软链接是链接文件,硬链接跟源文件类型相同
	4 文件大小:软链接的大小是源文件路径的字符串长度,硬链接跟源文件大小相同
	5 相对路径:软链接是相对于源文件的相对路径,硬链接是相对当前工作目录的工作路径
	6 inode:软链接与源文件不同;硬链接跟源文件相同
	7 链接数:软链接增删操作不会影响源文件;硬链接增删会导致源文件的链接数的增删
	8 删除源文件:软链接失效;硬链接可正常使用
	9 文件夹:可创建软链接,不能创建硬链接

cp 命令复制文件和创建硬链接文件的区别
	1 相对源文件:复制文件产生的是一个新文件,硬链接指向的还是源文件
	2 文件链接数:复制新文件和删除复制文件,都不会影响源文件的链接数;
	  创建硬链接或删除硬链接,会改变源文件的链接数;

3、描述/etc/rc.local 和 /var/log/messages 两文件内容是做什么的?

/etc/rc.loacl 文件在Linux 系统启动流程后期被运行的shell 脚本文件,可在改文件中添加功能或逻辑,实现开机自启程序
/var/log/message 文件用于存储Linux 系统启动日志,如:mail,daemon,kern等内容

4、df du 命令的作用分别是什么?

df 用于查看磁盘使用信息情况用
du 显示文件或目录所占用的磁盘空间

[root@centos8 ~]#df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        967M     0  967M   0% /dev
tmpfs           983M     0  983M   0% /dev/shm
tmpfs           983M  8.7M  974M   1% /run
tmpfs           983M     0  983M   0% /sys/fs/cgroup
/dev/nvme0n1p2  100G  2.2G   98G   3% /
/dev/nvme0n1p5   30G  247M   30G   1% /data
/dev/nvme0n1p1  976M  112M  797M  13% /boot
tmpfs           197M     0  197M   0% /run/user/0
tmpfs           197M     0  197M   0% /run/user/1002
[root@centos8 ~]#du -h
32K	.

5、使用cp 命令拷贝文件时,使用哪个选项可以连源文件的权限信息是一起拷贝?

cp 使用选项 --preserve=mode 选项可指定复制过程中需要一起复制的元数据
[root@centos8 ~]#ll
total 4
-rw-------. 1 root root 1401 Jul 30 09:36 anaconda-ks.cfg
[root@centos8 ~]#cp --preserve=mode anaconda-ks.cfg  file1
[root@centos8 ~]#ll
total 8
-rw-------. 1 root root 1401 Jul 30 09:36 anaconda-ks.cfg
-rw-------. 1 root root 1401 Aug  1 20:28 file1

6、cd~ , cd - , cd 三个命令的作用和区别

cd~ 	切换到家目录
cd-		表示返回上一次的工作目录,其值存储在$OLDPWDcd		功能等同于cd~ ,切换到当前用户的家目录

命令类

1、显示/var 目录下所有以 l开头,以一个小写字母结尾,且中间出现至少一个数字的文件或目录列表

[root@centos8 ~]#echo 121212 > /var/l8ss
[root@centos8 ~]#echo 22 > /var/l9dafs10jui
[root@centos8 ~]#ls -d /var/l*[0-9]*[a-z]
/var/l8ss  /var/l9dafs10jui
[root@centos8 ~]#ls -d /var/l*[0-9]*[[:lower:]]
/var/l8ss  /var/l9dafs10jui

2、显示/etc 目录下以任意一位数字开头,且以非数字结尾的文件或目录列表

[root@centos8 ~]#echo 11 > /etc/89ll
[root@centos8 ~]#echo 12 > /etc/2hell
[root@centos8 ~]#echo 12 > /etc/2hell23
[root@centos8 ~]#ls -d /etc/[0-9]*[^0-9]
/etc/2hell  /etc/89ll

3、显示/etc/ 目录下非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录列表

[root@centos8 ~]#echo 11 > /etc/89ll
[root@centos8 ~]#echo 12 > /etc/2hell
[root@centos8 ~]#echo 12 > /etc/2hell23
[root@centos8 ~]#ls -d /etc/[^[:alpha:]][[:alpha:]]*
/etc/2hell  /etc/2hell23

4、显示/etc/ 目录下所有以rc 开头,并后面是0-6之间的数字,其它为任意字符的文件或目录列表

[root@centos8 ~]#ls -d /etc/rc[0-6]*
/etc/rc0.d  /etc/rc1.d  /etc/rc2.d  /etc/rc3.d  /etc/rc4.d  /etc/rc5.d  /etc/rc6.d

5、显示/etc 目录下,所有.conf 结尾,且以m,n,r,p 开头的文件或目录列表

[root@centos8 ~]#ls -al /etc/[nmrp]*.conf
-rw-r--r--. 1 root root 5165 May 11  2019 /etc/man_db.conf
-rw-r--r--. 1 root root  812 Nov  9  2019 /etc/mke2fs.conf
-rw-r--r--. 1 root root 1516 Jul 30 09:33 /etc/nsswitch.conf
-rw-r--r--. 1 root root   74 Aug  1 15:00 /etc/resolv.conf
-rw-r--r--. 1 root root 3185 Nov  9  2019 /etc/rsyslog.conf

6、如何分别查看/root 下的隐藏文件和非隐藏列表

#ls 默认不查看隐藏文件
[root@centos8 ~]#ls /root
anaconda-ks.cfg  bc  linux.txt  test.txt  window.txt

#-a 列出所有的文件,包括隐藏文件
[root@centos8 ~]#ls -a /root
.   anaconda-ks.cfg  .bash_logout   .bashrc  .cshrc     .tcshrc   .viminfo
..  .bash_history    .bash_profile  bc       linux.txt  test.txt  window.txt

#-A 列出所有除. 和 .. 的所有文件
[root@centos8 ~]#ls -A /root
anaconda-ks.cfg  .bash_logout   .bashrc  .cshrc     .tcshrc   .viminfo
.bash_history    .bash_profile  bc       linux.txt  test.txt  window.txt

7、只显示/etc 下的非隐藏目录列表

[root@centos8 ~]#ls -d /etc/*

8、每天将/etc/ 目录下所有文件,备份到/data 独立的子目录下,并要求子目录为backupYYYY-mm-dd,备份过程可见

[root@centos8 ~]#cp -a /etc/ /data/backup`date +%F`
[root@centos8 ~]#ls /data
backup2020-08-02
[root@centos8 ~]#ls -al /data/backup2020-08-02/

9、创建/data/rootdir 目录,并复制/root 下所有文件到该目录内,要求保留原有权限

[root@centos8 data]#cp -pr /root /data/rootdir
[root@centos8 data]#ll /root /data/rootdir
/data/rootdir:
total 4
-rw-------. 1 root root 1401 Jul 30 09:36 anaconda-ks.cfg

/root:
total 4
-rw-------. 1 root root 1401 Jul 30 09:36 anaconda-ks.cfg

10、如何创建/testdir/dir1/x,/testdir/dir1/y,/testdir/dir1/x/a,/testdir/dir1/x/b,
/testdir/dir1/y/a,/testdir/dir1/y/b

[root@centos8 data]#mkdir -pv testdir/dir1/{x,y}/{a,b}
mkdir: created directory 'testdir'
mkdir: created directory 'testdir/dir1'
mkdir: created directory 'testdir/dir1/x'
mkdir: created directory 'testdir/dir1/x/a'
mkdir: created directory 'testdir/dir1/x/b'
mkdir: created directory 'testdir/dir1/y'
mkdir: created directory 'testdir/dir1/y/a'
mkdir: created directory 'testdir/dir1/y/b'

11、如何创建/testdir/dir2/x,/testdir/dir2/y,/testdir/dir2/x/a,/testdir/dir2/x/b

[root@centos8 data]#mkdir -pv testdir/dir1/{x/{a,b},y}
mkdir: created directory 'testdir'
mkdir: created directory 'testdir/dir1'
mkdir: created directory 'testdir/dir1/x'
mkdir: created directory 'testdir/dir1/x/a'
mkdir: created directory 'testdir/dir1/x/b'
mkdir: created directory 'testdir/dir1/y'

12、如何创建/testdir/dir3,/testdir/dir4,/testdir/dir5,/testdir/dir5/dir6,/testdir/dir5/dir7

[root@centos8 data]#mkdir -pv testdir/dir{3,4,5/dir{6,7}}
mkdir: created directory 'testdir'
mkdir: created directory 'testdir/dir3'
mkdir: created directory 'testdir/dir4'
mkdir: created directory 'testdir/dir5'
mkdir: created directory 'testdir/dir5/dir6'
mkdir: created directory 'testdir/dir5/dir7'

13、请写出创建文件夹权限为 777 的 /data/magedu 文件夹命令

[root@centos8 ~]#mkdir -pv -m 777 /data/magedu
mkdir: created directory '/data/magedu'
[root@centos8 ~]#ls -ald /data/magedu
drwxrwxrwx. 2 root root 6 Aug  2 15:35 /data/magedu

14、在Linux 系统中使用mkdir 命令的哪个选项可在/data 文件夹不存在的情况下成功创建/data/mysql/back 文件夹

使用-p 选项可自动创建不存在的父目录,使用-v 选项可显示创建过程
[root@centos8 ~]#mkdir -pv /data/mysql/back
mkdir: created directory '/data'
mkdir: created directory '/data/mysql'
mkdir: created directory '/data/mysql/back'

15、如何使用文件的inode 编号删除该文件

[root@centos8 ~]#ll
total 8
-rw-------. 1 root root 1401 Jul 30 09:36 anaconda-ks.cfg
-rw-------. 1 root root 1401 Aug  1 20:28 file1
[root@centos8 ~]#ls -i file1 
201760925 file1
[root@centos8 ~]#find /root -inum 201760925
/root/file1
[root@centos8 ~]#find /root -inum 201760925 -delete
[root@centos8 ~]#ll
total 4
-rw-------. 1 root root 1401 Jul 30 09:36 anaconda-ks.cfg
[root@centos8 ~]#

16、Linux 系统中如何给一个文件修改名称,rename 命令的作用是什么

使用mv 命令给文件修改命令
[root@centos8 data]#ll /data
total 0
-rw-r--r--. 1 root root  0 Aug  1 20:47 file10.log
[root@centos8 data]#mv file10.log filea.txt
[root@centos8 data]#ll
total 0
-rw-r--r--. 1 root root  0 Aug  1 20:47 filea.txt

rename 的作用是批量修改文件或目录名称
命令格式:rename 被修改内容 替换内容 修改的文件
[root@centos8 data]#ll
total 0
-rw-r--r--. 1 root root  0 Aug  1 20:47 file1.log
-rw-r--r--. 1 root root  0 Aug  1 20:47 file2.log
-rw-r--r--. 1 root root  0 Aug  1 20:47 file3.log
-rw-r--r--. 1 root root  0 Aug  1 20:47 file4.log
-rw-r--r--. 1 root root  0 Aug  1 20:47 file5.log
[root@centos8 data]#rename .log .log.bak *.log
[root@centos8 data]#ll
total 0
-rw-r--r--. 1 root root  0 Aug  1 20:47 file1.log.bak
-rw-r--r--. 1 root root  0 Aug  1 20:47 file2.log.bak
-rw-r--r--. 1 root root  0 Aug  1 20:47 file3.log.bak
-rw-r--r--. 1 root root  0 Aug  1 20:47 file4.log.bak
-rw-r--r--. 1 root root  0 Aug  1 20:47 file5.log.bak


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值