文件与目录管理
# 目录与路径
# 目录相关的操作
下面有几个特殊的目录
. 代表此层目录
.. 代表上一层目录
- 代表前一个工作目录
~ 代表『目前用户身份」所在的家目录
~account 代表account这个用户的家目录(account是个账号名)
2
3
4
5
几个常见的目录处理命令
cd:变换目录
pwd:显示当前目录
mkdir:建立一个新的目录
rmdir;删除一个空的目录
# 这几个命令很熟悉了,不多说,下面只说一下少见的(下面这个—P表示真实路径)
[root@localhost usr]# pwd -P
# 递归的创建目录
[root@localhost ~]# mkdir test1/test2
mkdir: 无法创建目录"test1/test2": 没有那个文件或目录
[root@localhost ~]# mkdir -p test1/test2
# 创建带权限的目录
mkdir -m 711 test
2
3
4
5
6
7
8
9
10
11
12
# 执行文件路径的变量 $PATH
当我们在执行一个指令的时候,举例来说『ls」好了,系统会依照PATH的设定去每个PATH定义的目录下搜寻文件名为ls的可执行文件,如果在PATH定义的目录中含有多个文件名为ls的可执行文件,那么先搜寻到的同名指令先被执行!
下面我们打印一下PATH
[root@localhost ~]# echo $PATH
/root/.nvm/versions/node/v12.14.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
2
PATH(一定是大写)这个变量的内容是由一堆目录所组成的,每个目录中间用冒号(:)来隔开。
如果我们想添加一个新目录,可以这样操作
export PATH="${PATH}:/root"
[root@localhost ~]# echo $PATH
/root/.nvm/versions/node/v12.14.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root
2
3
# 文件与目录管理
# 文件与目录验视 ls
# 复制删除与移动 cp,rm,mv
[root@localhost ~]# cp test.txt test2.txt
[root@localhost ~]# ls
test2.txt test.txt
# 复制某个地方的文件到当前目录
[root@localhost ~]# cp /bin/ls .
[root@localhost ~]# ls
ls test2.txt test.txt
# 复制多个文件到一个目录下,注意这个最后一个一定是目录
[root@localhost ~]# cp test.txt test2.txt test
[root@localhost ~]# cd test
[root@localhost test]# ls
test2.txt test.txt
2
3
4
5
6
7
8
9
10
11
12
这个命令比较简单,不介绍了
mv和CP一样,也不多说
# 取得路径的文件名与目录名
# 获取文件名
[root@localhost test]# basename /root/test.txt
test.txt
# 获取目录名
[root@localhost test]# dirname /root/test/test.txt
/root/test
2
3
4
5
6
# 文件内容查阅
有下面这几个指令来查看文件内容
cat由第一行开始显示文件内容
tac从最后一行开始显示,可以看出tac是cat的倒着写!
n1显示的时候,顺道输出行号!
more一页一页的显示文件内容
less与more类似,但是比more更好的是,他可以往前翻页!
head只看头几行
tail只看尾巴几行
od以二进制的方式读取文件内容
2
3
4
5
6
7
8
# 直接查看文件内容
[root@localhost ~]# cat test.txt -A
M-fM-5M-^KM-hM-/M-^UM-dM-8M-^@M-dM-8M-^K$
M-fM-5M-^KM-hM-/M-^U45$
456$
45$
456$
4$
65454$
[root@localhost ~]# cat -n test.txt
1 测试一下
2 测试45
3 456
4 45
5 456
6 4
7 65454
8 56
9 4
10 54
11 5
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
反向显示(可以看到,文件内容是反的顺序显示的)
[root@localhost ~]# tac test.txt
54
4
45
3
1
2
3
4
5
6
nl添加行号打印
[root@localhost ~]# nl test.txt
1 测试一下
2 测试45
3 456
4 45
5 456
2
3
4
5
6
这个东西效果和cat感觉差不多
# 可翻页查看
[root@localhost ~]# more /etc/man_db.conf
#
#
# This file is used by the man-db package to configure the man and cat paths.
# It is also used to provide a manpath for those without one by examining
2
3
4
5
可以看到,下面这里会出现这样的内容,代表我们可以往下翻
more命令几个常见的快捷键
空格键( space):代表向下翻一页;
enter 代表向下翻一行
/字符串 代表在这个显示的内容当中,向下搜寻「字符串」这个关键词
:f 立刻显示出文件名以及目前显示的行数
q 代表立刻离开more,不再显示该文件内容
·b或[ctr1]-b: 代表往冋翻页,不过这动作只对文件有用,对管线无用
2
3
4
5
6
下面我们使用 less
命令
[root@localhost ~]# less /etc/man_db.conf
less的用法比起more又更加的有弹性,怎么说呢?在more的时候,我们并没有办法向前面翻,只能往后面看,但若使用了less时,呵呵!就可以使用[ pageup] [ pagedown]等按键的功能来往前往后翻看文件,你瞧,是不是更容易使用来观看一个文件的内容了呢!下面是几个常见的快捷键
# 资料截取
我们可以使用 head
命令来取出前面几行,比如我们只取出10行,如果是负数的话就是取前面所有的不包含后面的n行
[root@localhost ~]# head -n 10 /etc/man_db.conf
#
#
# This file is used by the man-db package to configure the man and cat paths.
# It is also used to provide a manpath for those without one by examining
# their PATH environment variable. For details see the manpath(5) man page.
#
# Lines beginning with `#' are comments and are ignored. Any combination of
# tabs or spaces may be used as `whitespace' separators.
#
# There are three mappings allowed in this file:
2
3
4
5
6
7
8
9
10
11
12
下面我们使用 tail
取出后面几行
[root@localhost ~]# tail -n 10 /etc/man_db.conf
# formatted for a terminal of the given width, regardless of the width of
# the terminal actually being used. This should generally be within the
# range set by MINCATWIDTH and MAXCATWIDTH.
#
#CATWIDTH 0
#
#---------------------------------------------------------
# Flags.
# NOCACHE keeps man from creating cat pages.
#NOCACHE
# 如果不知道有多少行,但是我们想列出100行后的数据,可以这样
[root@localhost ~]# tail -n +100 /etc/man_db.conf
#---------------------------------------------------------
# Section names. Manual sections will be searched in the order listed here;
# the default is 1, n, l, 8, 3, 0, 2, 5, 4, 9, 6, 7. Multiple SECTION
# directives may be given for clarity, and will be concatenated together in
# the expected way.
# If a particular extension is not in this list (say, 1mh), it will be
# displayed with the rest of the section it belongs to. The effect of this
# is that you only need to explicitly list extensions if you want to force a
# particular order. Sections with extensions should usually be adjacent to
# their main section (e.g. "1 1mh 8 ...").
#
SECTION 1 1p 8 2 3 3p 4 5 6 7 9 0p n l p o 1x 2x 3x 4x 5x 6x 7x 8x
#
#---------------------------------------------------------
# Range of terminal widths permitted when displaying cat pages. If the
# terminal falls outside this range, cat pages will not be created (if
# missing) or displayed.
#
#MINCATWIDTH 80
#MAXCATWIDTH 80
#
# If CATWIDTH is set to a non-zero number, cat pages will always be
# formatted for a terminal of the given width, regardless of the width of
# the terminal actually being used. This should generally be within the
# range set by MINCATWIDTH and MAXCATWIDTH.
#
#CATWIDTH 0
#
#---------------------------------------------------------
# Flags.
# NOCACHE keeps man from creating cat pages.
#NOCACHE
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
# 非纯文本文档
我们使用 od
来读取二进制文件
# 请将/usr/bin/passwd的内容使用 ASCII方式来展现!
[root@localhost ~]# od -t c /usr/bin/passwd
0000000 177 E L F 002 001 001 \0 \0 \0 \0 \0 \0 \0 \0 \0
0000020 003 \0 > \0 001 \0 \0 \0 H 2 \0 \0 \0 \0 \0 \0
0000040 @ \0 \0 \0 \0 \0 \0 \0 220 e \0 \0 \0 \0 \0 \0
0000060 \0 \0 \0 \0 @ \0 8 \0 \t \0 @ \0 035 \0 034 \0
0000100 006 \0 \0 \0 005 \0 \0 \0 @ \0 \0 \0 \0 \0 \0 \0
0000120 @ \0 \0 \0 \0 \0 \0 \0 @ \0 \0 \0 \0 \0 \0 \0
0000140 370 001 \0 \0 \0 \0 \0 \0 370 001 \0 \0 \0 \0 \0 \0
2
3
4
5
6
7
8
9
10
# 修改文件时间或者新建文档
使用 touch
命令来实现
一般来说,文件里面有下面这三种时间
下面我们创建一个文件,然后观察一下时间(这个时间就是我们创建的时间)
[root@localhost ~]# touch a.txt
[root@localhost ~]# ls -l
总用量 4
-rw-r--r-- 1 root root 0 5月 15 16:44 a.txt
---x--x--x 1 www www 106 5月 15 16:22 test.txt
2
3
4
5
我们也可以使用touch来修改文件的时间,比如我们修改时间为两天前:
[root@localhost ~]# touch -d "2 days ago" a.txt
[root@localhost ~]# ls -l
总用量 4
-rw-r--r-- 1 root root 0 5月 13 16:46 a.txt
---x--x--x 1 www www 106 5月 15 16:22 test.txt
2
3
4
5
也可以指定特定时间
[root@localhost ~]# touch -t 201406150202 a.txt
[root@localhost ~]# ls -l
总用量 4
-rw-r--r-- 1 root root 0 6月 15 2014 a.txt
---x--x--x 1 www www 106 5月 15 16:22 test.txt
2
3
4
5
# 文件与目录的默认权限与隐藏权限
# 文件预设权限
如果查看自己新建一个文件的默认权限?使用 umask
命令
[root@localhost ~]# umask
0022
[root@localhost ~]# umask -S
u=rwx,g=rx,o=rx
2
3
4
我们可以使用umask来修改默认的权限
[root@localhost ~]# umask 002
[root@localhost ~]# touch a
[root@localhost ~]# ls -l
总用量 0
-rw-rw-r-- 1 root root 0 5月 15 16:53 a
2
3
4
5
# 文件隐藏属性
比如我们可以设置一个文件,让root用户也无法删除
[root@localhost ~]# touch a
[root@localhost ~]# chattr +i a
[root@localhost ~]# rm -f a
rm: 无法删除"a": 不允许的操作
2
3
4
当然解决方法也很简单,我们可以这样做
[root@localhost ~]# chattr -i a
[root@localhost ~]# rm -f a
2
那么如何显示文件的隐藏属性呢?使用 lsattr
命令
[root@localhost ~]# lsattr a
----i----------- a
2
# 文件特殊权限 SUID SGID SBIT
我们在查看某些文件,比如/tmp 时,会发现里面会有特殊的权限标识,比如下面这个居然有 t
[root@localhost ~]# ls -ld /tmp
drwxrwxrwt. 10 root root 274 5月 15 17:00 /tmp
2