magedu pro 第二周作业

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 常用的文件管理类命令有:cp,mv,rm

# cp是一个拷贝命令,常用参数:
# -i # 交互式
# -r # 递归
# -a # 全属性复制(包含-dR --preserve=all)
# 示例:

[hzz@e25f88eebde1 ~]$ cp -i test.txt /tmp/
cp: overwrite '/tmp/test.txt'? y
[hzz@e25f88eebde1 ~]$

[hzz@e25f88eebde1 ~]$ ls -ld test
drwxrwxr-x 2 hzz hzz 6 Dec 9 08:09 test
[hzz@e25f88eebde1 ~]$ cp -r test /tmp/
[hzz@e25f88eebde1 ~]$ ls -ld /tmp/test
drwxrwxr-x 3 hzz hzz 17 Dec 9 08:11 /tmp/test
[hzz@e25f88eebde1 ~]$

[hzz@e25f88eebde1 ~]$ ln -s test.txt test123
[hzz@e25f88eebde1 ~]$ cp -a test123 /tmp/test
[hzz@e25f88eebde1 ~]$ ls -l /tmp/test
total 0
lrwxrwxrwx 1 hzz hzz 8 Dec 9 08:17 test123 -> test.txt
[hzz@e25f88eebde1 ~]$

# mv命令相当于windows下的剪切粘贴,可用于移动文件或文件夹,也可用户给文件或文件夹重命名。
# 常用参数有-i和-f,参数功能和cp命令的类似。
# 使用示例:

[hzz@e25f88eebde1 ~]$ touch 123
[hzz@e25f88eebde1 ~]$ mkdir 123dir
[hzz@e25f88eebde1 ~]$ ls
123 123dir

# 移动文件
[hzz@e25f88eebde1 ~]$ mv 123 123dir/
[hzz@e25f88eebde1 ~]$ tree
.
`-- 123dir
`-- 123

1 directory, 1 file
[hzz@e25f88eebde1 ~]$

# 重命名
[hzz@e25f88eebde1 ~]$ mv 123dir 456dir
[hzz@e25f88eebde1 ~]$ tree
.
`-- 456dir
`-- 123

1 directory, 1 file
[hzz@e25f88eebde1 ~]$

# rm命令用于删除文件和文件夹,常用的参数有-i,-r和-f,参数功能和cp命令的类似。
# 人为操作尽量使用-i选项,避免使用-f选项,除非是删除含有大量文件的文件夹并进行反复确认。
# 使用示例:

[hzz@e25f88eebde1 ~]$ rm -ir 456dir/
rm: descend into directory '456dir/'? y
rm: remove regular empty file '456dir/123'? y
rm: remove directory '456dir/'? y
[hzz@e25f88eebde1 ~]$

2、bash的工作特性之命令执行状态返回值和命令行展开所涉及的内容及其示例演示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# bash的命令执行状态返回通过$?显示,其中0代表执行成功,1-255代表各种错误,如:

[root@e25f88eebde1 ~]# mkdir -p /tmp/123
[root@e25f88eebde1 ~]# echo $?
0
[root@e25f88eebde1 ~]# mkdir /tmp/123
mkdir: cannot create directory '/tmp/123': File exists
[root@e25f88eebde1 ~]# echo $?
1
[root@e25f88eebde1 ~]# mmkdir /tmp/123
bash: mmkdir: command not found
[root@e25f88eebde1 ~]# echo $?
127
[root@e25f88eebde1 ~]#

# 命令行展开,~默认展开为当前用户${HOME}路径,~username展开为指定用户${HOME}路径,如:

[root@e25f88eebde1 tmp]# cd /tmp
[root@e25f88eebde1 tmp]# pwd
/tmp
[root@e25f88eebde1 tmp]# cd ~
[root@e25f88eebde1 ~]# pwd
/root
[root@e25f88eebde1 ~]# cd ~hzz
[root@e25f88eebde1 hzz]# pwd
/home/hzz
[root@e25f88eebde1 hzz]#

# 命令行展开,{}以逗号分隔,展开进行轮询,如:

[root@e25f88eebde1 ~]# echo {123,456}789
123789 456789
[root@e25f88eebde1 ~]#

3、请使用命令行展开功能来完成以下练习:

(1)、创建/tmp目录下的:a_c, a_d, b_c, b_d

1
2
3
4
5
6
7
8
9
10
11
12
[root@e25f88eebde1 ~]# mkdir -pv /tmp/{a,b}_{c,d}
mkdir: created directory '/tmp/a_c'
mkdir: created directory '/tmp/a_d'
mkdir: created directory '/tmp/b_c'
mkdir: created directory '/tmp/b_d'
[root@e25f88eebde1 ~]# tree /tmp|egrep 'tmp|a|b'
/tmp
|-- a_c
|-- a_d
|-- b_c
|-- b_d
[root@e25f88eebde1 ~]#

(2)、创建/tmp/mylinux目录下的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
mylinux/
├── bin
├── boot
│ └── grub
├── dev
├── etc
│ ├── rc.d
│ │ └── init.d
│ └── sysconfig
│ └── network-scripts
├── lib
│ └── modules
├── lib64
├── proc
├── sbin
├── sys
├── tmp
├── usr
│ └── local
│ ├── bin
│ └── sbin
└── var
├── lock
├── log
└── run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
[root@e25f88eebde1 ~]# mkdir -pv /tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/initd.d,sysconfig/network-script},lib/modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var,lock,log,run}
mkdir: created directory '/tmp/mylinux'
mkdir: created directory '/tmp/mylinux/bin'
mkdir: created directory '/tmp/mylinux/boot'
mkdir: created directory '/tmp/mylinux/boot/grub'
mkdir: created directory '/tmp/mylinux/dev'
mkdir: created directory '/tmp/mylinux/etc'
mkdir: created directory '/tmp/mylinux/etc/rc.d'
mkdir: created directory '/tmp/mylinux/etc/rc.d/initd.d'
mkdir: created directory '/tmp/mylinux/etc/sysconfig'
mkdir: created directory '/tmp/mylinux/etc/sysconfig/network-script'
mkdir: created directory '/tmp/mylinux/lib'
mkdir: created directory '/tmp/mylinux/lib/modules'
mkdir: created directory '/tmp/mylinux/lib64'
mkdir: created directory '/tmp/mylinux/proc'
mkdir: created directory '/tmp/mylinux/sbin'
mkdir: created directory '/tmp/mylinux/sys'
mkdir: created directory '/tmp/mylinux/tmp'
mkdir: created directory '/tmp/mylinux/usr'
mkdir: created directory '/tmp/mylinux/usr/local'
mkdir: created directory '/tmp/mylinux/usr/local/bin'
mkdir: created directory '/tmp/mylinux/usr/local/sbin'
mkdir: created directory '/tmp/mylinux/var'
mkdir: created directory '/tmp/mylinux/lock'
mkdir: created directory '/tmp/mylinux/log'
mkdir: created directory '/tmp/mylinux/run'
[root@e25f88eebde1 ~]# tree /tmp/mylinux/
/tmp/mylinux/
|-- bin
|-- boot
| `-- grub
|-- dev
|-- etc
| |-- rc.d
| | `-- initd.d
| `-- sysconfig
| `-- network-script
|-- lib
| `-- modules
|-- lib64
|-- lock
|-- log
|-- proc
|-- run
|-- sbin
|-- sys
|-- tmp
|-- usr
| `-- local
| |-- bin
| `-- sbin
`-- var

24 directories, 0 files
[root@e25f88eebde1 ~]#

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 文件元数据信息查看方式为stat命令,含义如下:

[root@huangzz6 ~]# stat anaconda-ks.cfg
File: 'anaconda-ks.cfg'
Size: 3354 Blocks: 8 IO Block: 4096 regular file
Device: fd11h/64785d Inode: 918251 Links: 1
Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-12-14 14:59:34.000000000 +0000
Modify: 2016-12-14 14:59:34.000000000 +0000
Change: 2017-02-23 01:41:09.560161027 +0000
Birth: -
[root@huangzz6 ~]#

# 修改文件时间戳

# 修改访问时间:

[root@huangzz6 ~]# touch -a -t 201612081312.50 ks-script-1holkM
[root@huangzz6 ~]# stat ks-script-1holkM
File: 'ks-script-1holkM'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd11h/64785d Inode: 951105 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-12-08 13:12:50.000000000 +0000
Modify: 2017-03-11 10:55:44.102721776 +0000
Change: 2017-03-11 10:55:44.102721776 +0000
Birth: -
[root@huangzz6 ~]#

# 修改文件修改时间:

[root@huangzz6 ~]# touch -m -t 201612081312.50 ks-script-1holkM
[root@huangzz6 ~]# stat ks-script-1holkM
File: 'ks-script-1holkM'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd11h/64785d Inode: 951105 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-12-08 13:12:50.000000000 +0000
Modify: 2016-12-08 13:12:50.000000000 +0000
Change: 2017-03-11 10:56:10.729600289 +0000
Birth: -
[root@huangzz6 ~]#

5、如何定义一个命令的别名,如何在命令中引用另一个命令的执行结果?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 定义一个命令的别名,可以用alias

[root@huangzz6 ~]# ll
bash: ll: command not found
[root@huangzz6 ~]# alias ll="ls -l"
[root@huangzz6 ~]# ll
total 12
-rw------- 1 root root 3354 Dec 14 14:59 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 Mar 9 02:21 keep
-rw-r--r-- 1 root root 0 Dec 8 13:12 ks-script-1holkM
drwxr-xr-x 2 root root 4096 Mar 9 07:15 tmp
[root@huangzz6 ~]# type ll
ll is aliased to `ls -l'
[root@huangzz6 ~]#

# 去除命令别名,使用unalias

[root@huangzz6 ~]# unalias ll
[root@huangzz6 ~]# ll
bash: ll: command not found
[root@huangzz6 ~]# type ll
bash: type: ll: not found
[root@huangzz6 ~]#

# 在命令中引用另一个命令的执行结果,可以使用``或$()。

[hzz@huangzz6 ~]$ ls
[hzz@huangzz6 ~]$ mkdir `echo 123`
[hzz@huangzz6 ~]$ mkdir $(echo 456)
[hzz@huangzz6 ~]$ ls
123 456
[hzz@huangzz6 ~]$

06-黄中章