1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。
答:文件管理类命令有:cp,mv,rm
cp
命令功能:将一个或多个源文件或目录复制到指定的目标文件或目录
命令格式:
cp [OPTION]... [-T] SOURCE DEST //cp [选项]…[-T]源目的
cp [OPTION]... SOURCE... DIRECTORY // cp [选项]…源…目录
cp [OPTION]... -t DIRECTORY SOURCE... // cp [选项]…-t 目录 源…
常用选项:
-i:交互式复制,即覆盖之前提醒用户确认
-f:强制覆盖目标文件
-r:递归复制目录(大写R也是这个功能)
-d:--no-dereference --preserv=links 复制符号链接文件本身,而非其指向的源文件
-a:归档,相当于-dR --preserve=all,archive,用于实现归档;
--preserve[=ATTR_LIST]
mode:权限 #默认
ownership:属主和属组 #默认
timestamps:时间戳 #默认
context:安全标签
xattr:扩展属性
links:符号链接
all:上述所有属性
-p:--preserv=mode,ownership,timestamp
-v:--verbose
命令示例:
(1)复制和改名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@C7-1 ~]
# cp anaconda-ks.cfg /tmp/ #复制anaconda-ks.cfg到/tmp目录下
[root@C7-1 ~]
# ls /tmp/ #ls查看已复制
anaconda-ks.cfg
[root@C7-1 ~]
# cp anaconda-ks.cfg /tmp/ #再一次执行此命令,没有加入-i参数,也会提示用户确认
cp
: overwrite
'/tmp/anaconda-ks.cfg'
? y
[root@C7-1 ~]
# alias #接上,因为cp的别名默认被系统设置为cp -i,执行cp就相当于cp -i
alias
cp
=
'cp -i'
alias
egrep
=
'egrep --color=auto'
alias
fgrep
=
'fgrep --color=auto'
alias
grep
=
'grep --color=auto'
alias
l.=
'ls -d .* --color=auto'
alias
ll=
'ls -l --color=auto'
alias
ls
=
'ls --color=auto'
alias
mv
=
'mv -i'
alias
rm
=
'rm -i'
alias
which
=
'alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@C7-1 ~]
# cp anaconda-ks.cfg /tmp/an.cfg #复制anaconda-ks.cfg到/tmp目录下并改名为an.cfg
[root@C7-1 ~]
# ls /tmp/ #查看已复制并改名
an.cfg anaconda-ks.cfg
|
(2)使用-r参数,复制目录
1
2
3
4
5
|
[root@C7-1 ~]
# cp /test1/ /tmp #复制目录时,如果不加入-r参数会出现如下错误提示
cp
: omitting directory
'/test1/'
[root@C7-1 ~]
# cp -r /test1/ /tmp #复制目录,要使用-r参数,递归复制才可以
[root@C7-1 ~]
# ls /tmp/test1/ #如果源是目录,目标则创建指定的目录,并复制源目录中所有文件至目标目录中
1.txt 2.txt
|
(3)使用-a参数,把权限等都复制过来
1
2
3
4
5
6
7
8
9
10
11
|
[root@C7-1 tmp]
# ls -l /var/log/wtmp #首先查看/var/log/wtmp的权限为-rw-rw-r--
-rw-rw-r--. 1 root utmp 41088 Aug 23 17:26
/var/log/wtmp
[root@C7-1 tmp]
# cp /var/log/wtmp . #复制/var/log目录下的wtmp到当前目录下(.代表当前目录)
[root@C7-1 tmp]
# ls -l #查看wtmp权限为-rw-r--r--,和原本不一样
total 44
-rw-r--r--. 1 root root 41088 Aug 23 17:44 wtmp
[root@C7-1 tmp]
# cp -a /var/log/wtmp . #这次复制使用-a参数
cp
: overwrite
'./wtmp'
? y
[root@C7-1 tmp]
# ls -l #查看wtmp权限和/var/log下一样了,都为-rw-rw-r--
total 44
-rw-rw-r--. 1 root utmp 41088 Aug 23 17:26 wtmp
|
mv
命令功能:为文件或目录改名,或将文件或目录移动到其它位置
命令格式:
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
常用选项:
-i:交互式
-f:force强制覆盖,不提示
-v:显示移动过程
-u:若目标文件已经存在,且 source 比较新,才会更新(update)
-b:若需覆盖文件,则覆盖前先行备份
-t:即指定mv的目标目录,该选项适用于移动多个源文件到一个目录的情况,此时目标目录在前,源文件在后
命令示例:
(1)移动文件
1
2
3
4
5
6
|
[root@C7-1 tmp]
# mv wtmp test/ #移动当前目录的wtmp文件到test目录下
[root@C7-1 tmp]
# ls
mylinux
test
wtmp_1 wtmp_2
[root@C7-1 tmp]
# ls -l test/ #wtmp文件已移动到test目录下
total 44
-rw-rw-r--. 1 root utmp 41088 Aug 23 17:26 wtmp
|
(2)使用-f参数,强制移动不提示
1
2
3
4
5
6
|
[root@C7-1 tmp]
# touch wtmp #在tmp下再创建一个wtmp
[root@C7-1 tmp]
# mv wtmp test/ #不加参数移动到test下,由于之前已经复制过,所以出现提示,输入n取消。(mv是mv -i的别名。)
mv
: overwrite
'test/wtmp'
? n
[root@C7-1 tmp]
# mv -f wtmp test/ #加入-f参数,则不会提示,直接移动过去了
[root@C7-1 tmp]
# ls test/
wtmp
|
rm
命令功能:删除一个目录中的一个或多个文件或目录
命令格式:rm [OPTION]... FILE...
命令选项:
-i:interactive交互式
-f:force强制删除
-r:recursive递归删除
删除目录:rm -rf /PATH/TO/DIR
危险操作:rm -rf /*(6和7有提示,5没有)
命令示例:
(1)使用-f参数,强制删除文件
1
2
3
4
5
6
7
|
[root@C7-1 tmp]
# ls
mylinux
test
wtmp_1 wtmp_2
[root@C7-1 tmp]
# rm wtmp_1 #不加参数删除wtmp_2文件,由于rm是rm -i的别名,所以会有如下的提示,按n取消。
rm
: remove regular
file
'wtmp_1'
? n
[root@C7-1 tmp]
# rm -f wtmp_1 #加入-f参数,则删除时不会出现提示
[root@C7-1 tmp]
# ls #wtmp_2文件已删除
mylinux
test
wtmp_2
|
(2)使用-r参数,递归删除目录
1
2
3
4
5
6
7
8
9
10
|
[root@C7-1 tmp]
# ls -l #下面显示test是目录,里面也有文件
total 48
drwxr-xr-x. 17 root root 4096 Aug 23 18:13 mylinux
drwxr-xr-x. 2 root root 17 Aug 23 18:36
test
-rw-r--r--. 1 root root 41088 Aug 23 17:51 wtmp_2
[root@C7-1 tmp]
# rm test #不加参数,提示不能删除test目录
rm
: cannot remove
'test'
: Is a directory
[root@C7-1 tmp]
# rm -rf test #加入-r参数(同时加上-f),删除test目录
[root@C7-1 tmp]
# ls #test目录已删除完成
mylinux wtmp_2
|
2、bash的工作特性之命令执行状态返回值和命令行展开所涉及的内容及其示例演示。
答:bash命令执行完成后会有一个返回值,保存在$?中,如果正常执行,返回0,错误则返回值为1-255之间的数字。当执行命令后,执行echo $?查看。
示例:
1
2
3
4
5
6
7
8
9
10
|
[root@C7-1 tmp]
# ls -l #输入一个正确的命令查看当前目录
total 48
drwxr-xr-x. 17 root root 4096 Aug 23 18:13 mylinux
-rw-r--r--. 1 root root 41088 Aug 23 17:51 wtmp_2
[root@C7-1 tmp]
# echo $? #查看,下面显示0
0
[root@C7-1 tmp]
# lsss -l #有意把ls输入错误,使用命令不能执行
-
bash
: lsss: a‰°‘¤
[root@C7-1 tmp]
# echo $? #查看,下面显示127的错误返回值
127
|
3、请使用命令行展开功能来完成以下练习:
(1)、创建/tmp目录下的:a_c, a_d, b_c, b_d
(2)、创建/tmp/mylinux目录下的:
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)
1
2
3
4
5
6
7
|
[root@
cat
~]
# mkdir -v /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@
cat
~]
# ls -d /tmp/{a,b}_{c,d}
/tmp/a_c
/tmp/a_d
/tmp/b_c
/tmp/b_d
|
(2)
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
|
[root@
cat
~]
# mkdir -pv /tmp/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}
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
/init
.d'
mkdir
: created directory `
/tmp/mylinux/etc/sysconfig
'
mkdir
: created directory `
/tmp/mylinux/etc/sysconfig/network-scripts
'
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@C7-1 ~]
# tree /tmp/mylinux/ #使用tree命令查看目录结构
/tmp/mylinux/
|-- bin
|-- boot
| `-- grub
|-- dev
|-- etc
| |-- rc.d
| | `-- init.d
| `-- sysconfig
| `-- network-scripts
|-- lib
| `-- modules
|-- lib64
|-- lock
|-- log
|-- proc
|-- run
|-- sbin
|-- sys
|-- tmp
|-- usr
| `--
local
| |-- bin
| `-- sbin
`-- var
24 directories, 0 files
|
4、文件的元数据信息有哪些,分别表示什么含义,如何查看?如何修改文件的时间戳信息。
答:
文件的元数据是指文件的属性、大小、创建时间、访问时间、属主属组等信息。
三个时间戳:
Access time: 访问时间,简写为atime,读取文件内容
Modify time: 修改时间,mtime,改变文件内容(数据)
Change time: 改动时间,ctime,元数据发生改变
使用stat命令查看元数据和时间戳信息
例:
1
2
3
4
5
6
7
8
9
|
[root@C7-1 tmp]
# stat wtmp_2
File:
'wtmp_2'
Size: 41088 Blocks: 88 IO Block: 4096 regular
file
Device: fd00h
/64768d
Inode: 2169 Links: 1
Access: (0644
/-rw-r--r--
) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2016-08-23 17:51:54.289738407 +0800
Modify: 2016-08-23 17:51:54.289738407 +0800
Change: 2016-08-23 17:51:54.289738407 +0800
|
修改时间戳信息
(1)使用touch修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[root@C7-1 tmp]
# stat wtmp_2 #查看信息
File:
'wtmp_2'
Size: 41088 Blocks: 88 IO Block: 4096 regular
file
Device: fd00h
/64768d
Inode: 2169 Links: 1
Access: (0644
/-rw-r--r--
) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2016-08-23 17:51:54.289738407 +0800
Modify: 2016-08-23 17:51:54.289738407 +0800
Change: 2016-08-23 17:51:54.289738407 +0800
Birth: -
[root@C7-1 tmp]
# touch wtmp_2 #touch一下这个文件
[root@C7-1 tmp]
# stat wtmp_2 #三个时间都变成当前的时间了
File:
'wtmp_2'
Size: 41088 Blocks: 88 IO Block: 4096 regular
file
Device: fd00h
/64768d
Inode: 2169 Links: 1
Access: (0644
/-rw-r--r--
) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2016-08-23 20:27:02.444183598 +0800
Modify: 2016-08-23 20:27:02.444183598 +0800
Change: 2016-08-23 20:27:02.444183598 +0800
Birth: -
|
(2)使用-a参数,修改访问时间
1
2
3
4
5
6
7
8
9
10
11
|
[root@C7-1 tmp]
# touch -a wtmp_2 #使用-a参数,修改访问时间
[root@C7-1 tmp]
# stat wtmp_2 #因为访问时间变了,改动时间也会随着变
File:
'wtmp_2'
Size: 41088 Blocks: 88 IO Block: 4096 regular
file
Device: fd00h
/64768d
Inode: 2169 Links: 1
Access: (0644
/-rw-r--r--
) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2016-08-23 20:30:36.898170816 +0800
Modify: 2016-08-23 20:27:02.444183598 +0800
Change: 2016-08-23 20:30:36.898170816 +0800
Birth: -
|
(3)使用-m -t参数改修改时间
1
2
3
4
5
6
7
8
9
10
11
|
[root@C7-1 tmp]
# touch -m -t 201306061020.30 wtmp_2 #使用-m -t把时间改为指定的时间
[root@C7-1 tmp]
# stat wtmp_2 #修改时间以改为上面指定的时间
File:
'wtmp_2'
Size: 41088 Blocks: 88 IO Block: 4096 regular
file
Device: fd00h
/64768d
Inode: 2169 Links: 1
Access: (0644
/-rw-r--r--
) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2016-08-23 20:30:36.898170816 +0800
Modify: 2013-06-06 10:20:30.000000000 +0800
Change: 2016-08-23 20:35:26.698153542 +0800
Birth: -
|
5、如何定义一个命令的别名,如何在命令中引用另一个命令的执行结果?
答:
(1)定义命令别名通过alias命令实现:
alias NAME='VALUE'
例:定义iptime别名为查看网卡信息的命令
1
|
[root@C7-1 tmp]
# alias iptime='cat /etc/sysconfig/network-scripts/ifcfg-eno16777736'
|
(2)命令中引用另一个命令,可使用管道符“|”来实现
例:上面已设置iptime别名,通过管道符把来网卡信息传递给tail命令,通过tail -3来显示后3行
1
2
3
4
|
[root@C7-1 tmp]
# iptime | tail -3
IPADDR=10.3.20.71
NETMASK=255.255.255.0
GATEWAY=10.3.20.1
|
6、显示/var目录下所有以l开头,以一个小写字母结尾,且中间至少出现一位数字(可以有其它字符)的文件或目录。
1
2
3
|
[root@C7-1 tmp]
# touch /var/l23dvad
[root@C7-1 tmp]
# ls -d /var/l*[[:digit:]]*[[:lower:]]
/var/l23dvad
|
7、显示/etc目录下,以任意一个数字开头,且以非数字结尾的文件或目录。
1
2
3
4
5
|
[root@C7-1 ~]
# touch /etc/66dfdsdfd
[root@C7-1 ~]
# ls -d /etc/[0-9]*[^0-9]
/etc/66dfdsdfd
[root@C7-1 ~]
# ls -d /etc/[[:digit:]]*[^[:digit:]]
/etc/66dfdsdfd
|
8、显示/etc目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录。
1
2
3
|
[root@C7-1 ~]
# touch /etc/6dfdsdfd
[root@C7-1 ~]
# ls -d /etc/[^[:alpha:]][[:alpha:]]*
/etc/6dfdsdfd
|
9、在/tmp目录下创建以tfile开头,后跟当前日期和时间的文件,文件名形如:tfile-2016-08-06-09-32-22。
1
2
3
|
[root@C7-1 ~]
# touch /tmp/tfile-`date +"%Y-%m-%d-%H-%M-%S"`
[root@C7-1 ~]
# ls -d /tmp/tf*[0-9]
/tmp/tfile-2016-08-23-21-10-15
|
10、复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中。
1
2
3
4
5
6
7
8
9
10
11
|
[root@C7-1 ~]
# mkdir /tmp/mytest1 #如题目要求创建/tmp/mytest1目录
[root@C7-1 ~]
# ls -d /etc/p*[^[:digit:]] #先用ls查看如题目要求的文件,正确后再按下面使用cp命令复制
/etc/pam
.d
/etc/pki
/etc/popt
.d
/etc/prelink
.conf.d
/etc/profile
.d
/etc/passwd
/etc/plymouth
/etc/postfix
/etc/printcap
/etc/protocols
/etc/passwd-
/etc/pm
/etc/ppp
/etc/profile
/etc/python
[root@C7-1 ~]
# cp -r /etc/p*[^[:digit:]] /tmp/mytest1/
[root@C7-1 ~]
# ls -d /tmp/mytest1/p*[^[:digit:]]
/tmp/mytest1/pam
.d
/tmp/mytest1/plymouth
/tmp/mytest1/ppp
/tmp/mytest1/profile
.d
/tmp/mytest1/passwd
/tmp/mytest1/pm
/tmp/mytest1/prelink
.conf.d
/tmp/mytest1/protocols
/tmp/mytest1/passwd-
/tmp/mytest1/popt
.d
/tmp/mytest1/printcap
/tmp/mytest1/python
/tmp/mytest1/pki
/tmp/mytest1/postfix
/tmp/mytest1/profile
|
11、复制/etc目录下所有以.d结尾的文件或目录至/tmp/mytest2目录中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@C7-1 ~]
# mkdir /tmp/mytest2 #如题目要求创建/tmp/mytest2目录
[root@C7-1 ~]
# ls -d /etc/*.d #先用ls查看如题目要求的文件,正确后再按下面使用cp命令复制
/etc/bash_completion
.d
/etc/dracut
.conf.d
/etc/modules-load
.d
/etc/rc
.d
/etc/rc5
.d
/etc/sysctl
.d
/etc/binfmt
.d
/etc/grub
.d
/etc/my
.cnf.d
/etc/rc0
.d
/etc/rc6
.d
/etc/tmpfiles
.d
/etc/chkconfig
.d
/etc/init
.d
/etc/pam
.d
/etc/rc1
.d
/etc/rsyslog
.d
/etc/xinetd
.d
/etc/cron
.d
/etc/ld
.so.conf.d
/etc/popt
.d
/etc/rc2
.d
/etc/rwtab
.d
/etc/yum
.repos.d
/etc/depmod
.d
/etc/logrotate
.d
/etc/prelink
.conf.d
/etc/rc3
.d
/etc/statetab
.d
/etc/dnsmasq
.d
/etc/modprobe
.d
/etc/profile
.d
/etc/rc4
.d
/etc/sudoers
.d
[root@C7-1 ~]
# cp -r /etc/*.d /tmp/mytest2
[root@C7-1 ~]
# ls -d /tmp/mytest2/*.d
/tmp/mytest2/bash_completion
.d
/tmp/mytest2/ld
.so.conf.d
/tmp/mytest2/rc
.d
/tmp/mytest2/rwtab
.d
/tmp/mytest2/binfmt
.d
/tmp/mytest2/logrotate
.d
/tmp/mytest2/rc0
.d
/tmp/mytest2/statetab
.d
/tmp/mytest2/chkconfig
.d
/tmp/mytest2/modprobe
.d
/tmp/mytest2/rc1
.d
/tmp/mytest2/sudoers
.d
/tmp/mytest2/cron
.d
/tmp/mytest2/modules-load
.d
/tmp/mytest2/rc2
.d
/tmp/mytest2/sysctl
.d
/tmp/mytest2/depmod
.d
/tmp/mytest2/my
.cnf.d
/tmp/mytest2/rc3
.d
/tmp/mytest2/tmpfiles
.d
/tmp/mytest2/dnsmasq
.d
/tmp/mytest2/pam
.d
/tmp/mytest2/rc4
.d
/tmp/mytest2/xinetd
.d
/tmp/mytest2/dracut
.conf.d
/tmp/mytest2/popt
.d
/tmp/mytest2/rc5
.d
/tmp/mytest2/yum
.repos.d
/tmp/mytest2/grub
.d
/tmp/mytest2/prelink
.conf.d
/tmp/mytest2/rc6
.d
/tmp/mytest2/init
.d
/tmp/mytest2/profile
.d
/tmp/mytest2/rsyslog
.d
|
12、复制/etc/目录下所有以l或m或n开头,以.conf结尾的文件至/tmp/mytest3目录中。
1
2
3
4
5
6
|
[root@C7-1 ~]
# mkdir /tmp/mytest3 #如题目要求创建/tmp/mytest3目录
[root@C7-1 ~]
# ls -d /etc/[1,m,n]*.conf #先用ls查看如题目要求的文件,正确后再按下
/etc/man_db
.conf
/etc/mke2fs
.conf
/etc/nsswitch
.conf
[root@C7-1 ~]
# cp -r /etc/[1,m,n]*.conf /tmp/mytest3/
[root@C7-1 ~]
# ls -d /tmp/mytest3/[1,m,n]*.conf
/tmp/mytest3/man_db
.conf
/tmp/mytest3/mke2fs
.conf
/tmp/mytest3/nsswitch
.conf
|