文件与目录管理
Linux 文件与目录管理
我们知道Linux的目录结构为树状结构,最顶级的目录为根目录 /。
其他目录通过挂载可以将它们添加到树中,通过解除挂载可以移除它们。
在开始本教程前我们需要先知道什么是绝对路径与相对路径。
-
绝对路径:
路径的写法,由根目录 / 写起,例如: /usr/share/doc 这个目录。 -
相对路径:
路径的写法,不是由 / 写起,例如由 /usr/share/doc 要到 /usr/share/man 底下时,可以写成: cd ../man 这就是相对路径的写法。
处理目录的常用命令
接下来我们就来看几个常见的处理目录的命令吧:
- ls(英文全拼:list files): 列出目录及文件名
- cd(英文全拼:change directory):切换目录
- pwd(英文全拼:print work directory):显示目前的目录
- mkdir(英文全拼:make directory):创建一个新的目录
- rmdir(英文全拼:remove directory):删除一个空的目录
- cp(英文全拼:copy file): 复制文件或目录
- rm(英文全拼:remove): 删除文件或目录
- mv(英文全拼:move file): 移动文件与目录,或修改文件与目录的名称
你可以使用 man [命令] 来查看各个命令的使用文档,如 :man cp。
ls (列出目录)
在Linux系统当中, ls 命令可能是最常被运行的。
选项与参数:
- -a :全部的文件,连同隐藏文件( 开头为 . 的文件) 一起列出来(常用)
- -d :仅列出目录本身,而不是列出目录内的文件数据(常用)
- -l :长数据串列出,包含文件的属性与权限等等数据;(常用)
实例:
[seven@centos7 ~]$ ls -al
总用量 72
drwx------. 16 seven seven 4096 6月 30 16:40 .
drwxr-xr-x. 3 root root 19 3月 25 11:14 ..
-rw-------. 1 seven seven 1258 6月 22 17:14 .bash_history
-rw-r--r--. 1 seven seven 18 4月 1 2020 .bash_logout
-rw-r--r--. 1 seven seven 193 4月 1 2020 .bash_profile
-rw-r--r--. 1 seven seven 231 4月 1 2020 .bashrc
drwxrwxr-x. 18 seven seven 4096 4月 8 17:30 .cache
drwxrwxr-x. 16 seven seven 4096 6月 21 12:04 .config
drwx------. 3 seven seven 25 3月 25 12:30 .dbus
cd (切换目录)
cd是Change Directory的缩写,这是用来变换工作目录的命令。
语法:
cd [相对路径或绝对路径]
[seven@centos7 ~]$ cd test1
pwd (显示目前所在的目录)
pwd 是 Print Working Directory 的缩写,也就是显示目前所在目录的命令。
语法:
pwd -P
选项与参数:
- -P :显示出确实的路径,而非使用连结 (link) 路径。
实例:单纯显示出目前的工作目录:
[seven@centos7 ~]$ pwd -P
/home/seven
mkdir (创建新目录)
如果想要创建新的目录的话,那么就使用mkdir (make directory)吧。
语法:
mkdir [-mp] 目录名称
选项与参数:
- -m :配置文件的权限喔!直接配置,不需要看默认权限 (umask) 的脸色~
- -p :帮助你直接将所需要的目录(包含上一级目录)递归创建起来!
实例:请到/tmp底下尝试创建数个新目录看看:
[seven@centos7 ~]$ mkdir test
[seven@centos7 ~]$ ls
mysql-community-release-el7-5.noarch.rpm test1.txt 模板 图片 下载 桌面
test 公共
加了这个 -p 的选项,可以自行帮你创建多层目录!
[seven@centos7 ~]$ mkdir demo/demo1/demo2/demo3/
mkdir: 无法创建目录"demo/demo1/demo2/demo3/": 没有那个文件或目录
[seven@centos7 ~]$ mkdir -p demo/demo1/demo2/demo3/
[seven@centos7 ~]$ ls
demo test1.txt
实例:创建权限为 rwx--x--x 的目录。
[seven@centos7 ~]$ mkdir -m 711 test1
[seven@centos7 ~]$ ls -l
总用量 16
drwxrwxr-x 3 seven seven 19 6月 30 22:25 demo
-rw-r--r-- 1 root root 6140 11月 12 2015 mysql-community-release-el7-5.noarch.rpm
drwx--x--x 2 seven seven 6 6月 30 22:26 test1
rmdir (删除空的目录)
语法:
rmdir [-p] 目录名称
选项与参数:
- -p :从该目录起,一次删除多级空目录
删除
[seven@centos7 ~]$ ls
mysql-community-release-el7-5.noarch.rpm test1 公共 视频 文档 音乐
test test1.txt 模板 图片 下载 桌面
[seven@centos7 ~]$ rmdir test1
[seven@centos7 ~]$ ls
mysql-community-release-el7-5.noarch.rpm test1.txt 模板 图片 下载 桌面
test
[seven@centos7 ~]$ rmdir -p demo/demo1/demo2/demo3/
[seven@centos7 ~]$ ls -l
总用量 16
-rw-r--r-- 1 root root 6140 11月 12 2015 mysql-community-release-el7-5.noarch.rpm
drwx--x--x 2 seven seven 6 6月 30 22:26 test1
-rw-r--r-- 1 root root 5914 4月 30 18:10 test1.txt
cp (复制文件或目录)
cp 即拷贝文件和目录。
语法:
[root@www ~]# cp [-adfilprsu] 来源档(source) 目标档(destination)
[root@www ~]# cp [options] source1 source2 source3 .... directory
选项与参数:
-
-a:相当於 -pdr 的意思,至於 pdr 请参考下列说明;(常用)
-
-d:若来源档为连结档的属性(link file),则复制连结档属性而非文件本身;
-
-f:为强制(force)的意思,若目标文件已经存在且无法开启,则移除后再尝试一次;
-
-i:若目标档(destination)已经存在时,在覆盖时会先询问动作的进行(常用)
-
-l:进行硬式连结(hard link)的连结档创建,而非复制文件本身;
-
-p:连同文件的属性一起复制过去,而非使用默认属性(备份常用);
-
-r:递归持续复制,用於目录的复制行为;(常用)
-
-s:复制成为符号连结档 (symbolic link),亦即『捷径』文件;
-
-u:若 destination 比 source 旧才升级 destination !
用 root 身份,将 用户 目录下的 test.c 复制到 /tmp 下
[root@centos7 seven]# vim test.c
[root@centos7 seven]# cp test.c /tmp
[root@centos7 seven]# cp -i test.c /tmp
cp:是否覆盖"/tmp/test.c"? y
rm (移除文件或目录)
语法:
rm [-fir] 文件或目录
选项与参数:
- -f :就是 force 的意思,忽略不存在的文件,不会出现警告信息;
- -i :互动模式,在删除前会询问使用者是否动作
- -r :递归删除啊!最常用在目录的删除了!这是非常危险的选项!!!
将刚刚在 cp 的实例中创建的 test.c 删除掉!
[root@centos7 tmp]# rm -i test.c
rm:是否删除普通文件 "test.c"?y
mv (移动文件与目录,或修改名称)
语法:
[root@www ~]# mv [-fiu] source destination
[root@www ~]# mv [options] source1 source2 source3 .... directory
选项与参数:
- -f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;
- -i :若目标文件 (destination) 已经存在时,就会询问是否覆盖!
- -u :若目标文件已经存在,且 source 比较新,才会升级 (update)
将文件移动到目录中
[root@centos7 seven]# mv test1/demo.c/ /tmp
[root@centos7 seven]# cd /
[root@centos7 /]# cd tmp
[root@centos7 tmp]# ls
demo.c
Linux 文件内容查看
Linux系统中使用以下命令来查看文件的内容:
- cat 由第一行开始显示文件内容
- tac 从最后一行开始显示,可以看出 tac 是 cat 的倒着写!
- nl 显示的时候,顺道输出行号!
- more 一页一页的显示文件内容
- less 与 more 类似,但是比 more 更好的是,他可以往前翻页!
- head 只看头几行
- tail 只看尾巴几行
你可以使用 _man [命令]_来查看各个命令的使用文档,如 :man cp。
cat
由第一行开始显示文件内容
语法:
cat [-AbEnTv]
选项与参数:
- -A :相当於 -vET 的整合选项,可列出一些特殊字符而不是空白而已;
- -b :列出行号,仅针对非空白行做行号显示,空白行不标行号!
- -E :将结尾的断行字节 $ 显示出来;
- -n :列印出行号,连同空白行也会有行号,与 -b 的选项不同;
- -T :将 [tab] 按键以 ^I 显示出来;
- -v :列出一些看不出来的特殊字符
[root@centos7 ~]# cat /etc/issue
\S
Kernel \r on an \m
tac
tac与cat命令刚好相反,文件内容从最后一行开始显示,可以看出 tac 是 cat 的倒着写!如:
[root@centos7 ~]# tac /etc/issue
Kernel \r on an \m
\S
nl
显示行号
语法:
nl [-bnw] 文件
选项与参数:
- -b :指定行号指定的方式,主要有两种:
-b a :表示不论是否为空行,也同样列出行号(类似 cat -n);
-b t :如果有空行,空的那一行不要列出行号(默认值); - -n :列出行号表示的方法,主要有三种:
-n ln :行号在荧幕的最左方显示;
-n rn :行号在自己栏位的最右方显示,且不加 0 ;
-n rz :行号在自己栏位的最右方显示,且加 0 ; - -w :行号栏位的占用的位数。
[root@centos7 ~]# nl /etc/issue
1 \S
2 Kernel \r on an \m
more
一页一页翻动
[root@centos7 ~]# 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
# 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:
# --------------------------------------------------------
# MANDATORY_MANPATH manpath_element
# MANPATH_MAP path_element manpath_element
# MANDB_MAP global_manpath [relative_catpath]
#---------------------------------------------------------
# every automatically generated MANPATH includes these fields
#
#MANDATORY_MANPATH /usr/src/pvm3/man
#
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
#---------------------------------------------------------
--More--(18%)
在 more 这个程序的运行过程中,你有几个按键可以按的:
- 空白键 (space):代表向下翻一页;
- Enter :代表向下翻『一行』;
- /字串 :代表在这个显示的内容当中,向下搜寻『字串』这个关键字;
- :f :立刻显示出档名以及目前显示的行数;
- q :代表立刻离开 more ,不再显示该文件内容。
- b 或 [ctrl]-b :代表往回翻页,不过这动作只对文件有用,对管线无用。
less
一页一页翻动,以下实例输出/etc/man.config文件的内容:
[root@centos7 ~]# less /etc/man_db.conf
#
# You *must* provide all system manpaths, including manpaths for alternate
# operating systems, locale specific manpaths, and combinations of both, if
# they exist, otherwise the permissions of the user running man/mandb will
# be used to manipulate the manual pages. Also, mandb will not initialise
# the database cache for any manpaths not mentioned below unless explicitly
# requested to do so.
#
# In a per-user configuration file, this directive only controls the
# location of catpaths and the creation of database caches; it has no effect
# on privileges.
#
# Any manpaths that are subdirectories of other manpaths must be mentioned
# *before* the containing manpath. E.g. /usr/man/preformat must be listed
# before /usr/man.
#
# *MANPATH* -> *CATPATH*
#
:
less运行时可以输入的命令有:
- 空白键 :向下翻动一页;
- [pagedown]:向下翻动一页;
- [pageup] :向上翻动一页;
- /字串 :向下搜寻『字串』的功能;
- ?字串 :向上搜寻『字串』的功能;
- n :重复前一个搜寻 (与 / 或 ? 有关!)
- N :反向的重复前一个搜寻 (与 / 或 ? 有关!)
- q :离开 less 这个程序;
head
取出文件前面几行
语法:
head [-n number] 文件
选项与参数:
- -n :后面接数字,代表显示几行的意思
[root@centos7 ~]# head -n 4 /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
ail
取出文件后面几行
语法:
tail [-n number] 文件
选项与参数:
- -n :后面接数字,代表显示几行的意思
- -f :表示持续侦测后面所接的档名,要等到按下[ctrl]-c才会结束tail的侦测
[root@centos7 ~]# tail -n 20 /etc/man_db.conf
#
#---------------------------------------------------------
# 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
小结
这部分对于文件系统的管理,个人觉得都是些平时会经常用到的命令,只有反复使用才能熟练掌握。