开始进行文本终端机模式底下的指令操作行为!以及一点点初阶的文件管理行为啰!
前一堂课我们谈到了文本界面 (command-line interface, CLI) 的指令操作,不过只是很简单的一些指令而已。 你会发现如果指令每个字符都要手动输入,好像很容易打字错误。这堂课我们会介绍一些组合按钮,让你可以快速的下达指令, 同时,通过 bash-completion 软件的协助,连选项与参数都可以通过热键来完成补齐的功能。另外,前一堂课我们也学到使用图形界面查阅系统文件, 这节课,就让我们来简单的通过 CLI 进行文件管理的行为,当然,在实际操作文件管理之前,本的目录内数据的了解,也是必须要知道的项目。
其实我们都是通过『软件』或『程序』在跟系统作沟通的。文本模式登录后所取得的程序被称为壳(Shell),这是因为这支程序负责最外面跟用户(我们)沟通, 所以才被戏称为壳程序!CentOS 8 的缺省壳程序为 bash,用户最好一开始就能够创建良好的操作行为,对于未来的 Linux 使用上,会有很大的帮助。
另外,关于 CLI 的环境,有几个常见会搞混的名词,这里我们先来理解一下:
bash shell 环境下,指令的下达基本上有几个需要注意的地方:
[student@localhost ~]$ command [-options] [parameter1...]
前一堂课我们使用过 ls 与 ll 这两个简易的指令来查看文件名,那如果想要知道目前的时间,或者是格式化输出时间时,就得要使用 date 这个指令来处理!
[student@localhost ~]$ date
二 3月 3 17:54:38 CST 2020
因为 student 选择中文语系的关系,所以屏幕上出现的就会是中文的星期二与月日这样。 若需要格式化的输出,就得要加上特别的选项或参数来处理,例如一般台湾我们常见 2020/03/03 这样的日期输出格式, 此时你可能要这样下达指令:
[student@localhost ~]$ date +%Y/%m/%d
2020/03/03
上述的选项数据 (+%Y/%m%d) 基本上不太需要背诵,使用在线查找的方式来处理即可。 最简单的处理方式,可以通过 --help 这个长选项来查找各个选项的功能,如下所示:
[student@localhost ~]$ date --help Usage: date [OPTION]... [+FORMAT] or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] <==上面这两行是语法的部份 Display the current time in the given FORMAT, or set the system date. <==这一行是指令说明 Mandatory arguments to long options are mandatory for short options too. <==底下是主要的 option 说明 -d, --date=STRING display time described by STRING, not 'now' --debug annotate the parsed date, and warn about questionable usage to stderr -f, --file=DATEFILE like --date; once for each line of DATEFILE -I[FMT], --iso-8601[=FMT] output date/time in ISO 8601 format. FMT='date' for date only (the default), 'hours', 'minutes', 'seconds', or 'ns' for date and time to the indicated precision. Example: 2006-08-14T02:34:56-06:00 -R, --rfc-email output date and time in RFC 5322 format. Example: Mon, 14 Aug 2006 02:34:56 -0600 --rfc-3339=FMT output date/time in RFC 3339 format. FMT='date', 'seconds', or 'ns' for date and time to the indicated precision. Example: 2006-08-14 02:34:56-06:00 -r, --reference=FILE display the last modification time of FILE -s, --set=STRING set time described by STRING -u, --utc, --universal print or set Coordinated Universal Time (UTC) --help 显示此求助说明并离开 --version 显示版本信息并离开 FORMAT controls the output. Interpreted sequences are: <==底下则是格式 (FORMAT) 的说明 %% a literal % %a locale's abbreviated weekday name (e.g., Sun) %A locale's full weekday name (e.g., Sunday) %b locale's abbreviated month name (e.g., Jan) ......
整个辅助文档大致上可分为几个部份:
你懂这个 command --help 的用法之后,未来许多指令你都可以自己查用法了!举例来说,你知道 cal 这个指令的功能与如何应用嘛?
继续来玩一下 date 这个指令!从前一小节使用 date --help 后,可以发现语法有两种情况,如下所示:
[student@localhost ~]$ date --help Usage: date [OPTION]... [+FORMAT] or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] Display the current time in the given FORMAT, or set the system date.
指令说明当中,可以是『显示, display』也能够是『设置, set』日期。语法 (Usage) 的第一行就是显示日期而已,第二行当然就是设置日期了。 如果使用 student 身份来设置日期,会有什么状况?
# 先注意,设置日期的格式是: MMDDhhmmYYYY,也就是 月日时分年,除了年,其他都有两位数 [student@station10-101 ~]$ date 三 3月 4 00:04:53 CST 2020 <==当下的日期 [student@station10-101 ~]$ date 030400052020 <==重设为当下的时间 date: 无法设置时间: 此项操作并不被允许 三 3月 4 00:05:00 CST 2020 <==这个动作其实没有生效!
可以发现到日期并没有变更到正确的日期,而且 date 也明白的告诉操作者,操作者没有权限 (Operation not permitted)!因为日期的设置要系统管理员才能够设置的。 此时我们就得要切换身份成为系统管理员(root)才行。处理的方法如下:
[student@localhost ~]$ su - 密码: <==输入密码时,不会出现 * 符号! [root@localhost ~]#
本教学系统 root 的密码为 myCentOS8,请注意大小写不同!因此在『密码:』后面输入 myCentOS8 之后,你就可以发现用户的身份变换成为 root 了! 此时再次使用 date 来看看日期能否被设置为正确?
[root@localhost ~]# date 030400052020 Wed Mar 4 00:05:00 CST 2020 [root@localhost ~]# date Wed Mar 4 00:05:20 CST 2020 [root@localhost ~]# hwclock -w
读者们可以发现上表两个指令的操作相差约 20 秒钟,因此输出的信息就会有两秒钟的误差。不过,日期确实就被修订成为目前的状态。 但如果需要完整的设置系统时间,则需要使用 hwclock -w 写入 BIOS 时间钟才行。(由于虚拟机的 BIOS 也是虚拟的,因此就不需要使用 hwclock 写入)
另外, root 的身份是作为系统管理所需要的功能,因此做完任何系统维护行为后,请回复到一般用户的身份较佳 (这个习惯请务必养成!)。
由于我们的系统环境使用中文,因此在日期的输出方面可能就是以中文为主。那如果想要显示为英文年月时,就得要修改一个变量,如下所示:
[student@localhost ~]$ date 三 3月 4 00:12:33 CST 2020 <==这个时候是中文 [student@localhost ~]$ LANG=en_US.utf8 <==这个 LANG 就可以改语系 [student@localhost ~]$ date Wed Mar 4 00:13:10 CST 2020 <==输出就变成英文了!
你可以发现日期已经变更成为英文的方式来显示了!此即 LANG 语系变量的设置功能。台湾地区经常使用的语系有中文 (zh_TW) 与英文 (en_US) 的万国码两种, 当然,比较旧的数据可能需要使用 big5 编码,所以台湾常见的语系有:
我们操作终端机 (terminal) 需要很多显示的环境,还有一些基础的设置,这些设置很多都是可以调整的。为了方便用户可以自行修订参数, 因此,我们就使用类似上面的 LANG 这个『变量』来设置一些功能。通过修改这些『变量』,就能够影响操作环境或操作行为了。那如何查看某个变量呢? 通过 echo 这个指令,搭配变量取用方法即可。常见的调用变量的方式有:
[student@localhost ~]$ echo $var [student@localhost ~]$ echo ${var} # 简单的说,就是变量名称 (var) 前面加上钱字号成为 $var 即可。详细规范则加上大括号成为 ${var} 即可。
至于语系的变化其实有两个变量可以使用,除了常用的 LANG 之外,也可以通过 LC_ALL 来修订!但一般建议使用 LANG 即可。 既然已经知道语系的设置变量为 LANG,通过上面的调用方式,则查阅目前语系的方法就是:
[student@localhost ~]$ echo ${LANG}
en_US.utf8
[student@station10-101 ~]$ LANG=zh_TW.utf8 [student@station10-101 ~]$ echo ${LANG} zh_TW.utf8 <==这样就变更了语系
[student@station10-101 ~]$ locale LANG=zh_TW.utf8 <==整体语系,底下则是个别消息的语系 LC_CTYPE="zh_TW.utf8" LC_NUMERIC="zh_TW.utf8" LC_TIME="zh_TW.utf8" LC_COLLATE="zh_TW.utf8" LC_MONETARY="zh_TW.utf8" LC_MESSAGES="zh_TW.utf8" LC_PAPER="zh_TW.utf8" LC_NAME="zh_TW.utf8" LC_ADDRESS="zh_TW.utf8" LC_TELEPHONE="zh_TW.utf8" LC_MEASUREMENT="zh_TW.utf8" LC_IDENTIFICATION="zh_TW.utf8" LC_ALL= [student@station10-101 ~]$ locale --help 使用方式: locale [参数…] 名称 或者: locale [参数…] [-a|-m] 取得语区数据特定的信息 系统相关信息: -a, --all-locales 写出存在的语区数据名称 -m, --charmaps 写出存在的字集对照表名称 ....
[student@station10-101 ~]$ locale -a C <==标准 C 编程语言语系 C.utf8 en_AG en_AU en_AU.utf8 ...... zh_TW zh_TW.euctw zh_TW.utf8上面的数据量输出比较多,如果超出整个窗口的行列范围,我们可以按下 [shift]+[pageup] 向上翻页,或 [shift]+[pagedown] 向下翻页喔!你就可以看到所有的语系数据了。
[student@station10-101 ~]$ echo ${LANG} zh_TW.utf8 <==是中文 [student@station10-101 ~]$ localectl System Locale: LANG=en_US.UTF-8 <==是英文 VC Keymap: us X11 Layout: us [student@station10-101 ~]$ localectl --help localectl [OPTIONS...] COMMAND ... ...... Commands: status Show current locale settings set-locale LOCALE... Set system locale <==看起来是可以设置成为不同的语系 list-locales Show known locales set-keymap MAP [MAP] Set console and X11 keyboard mappings ...... [student@station10-101 ~]$ localectl set-locale zh_TW.utf8 # 这个指令要经过 root 验证,因为会更动到系统设置值!所以这里先知道即可。不建议修改啦!
除了上个例题谈到的可以上下移动屏幕画面的组合按键之外,在纯文本模式 (bash shell) 的环境下,建议读者们一定要熟记且经常应用的热键与组合键有:
ll, ls, date, cal 均可使用 --help 来查找语法与相关的选项、参数数据,但某些指令则没有办法显示详细的信息。例如底下的小算盘指令:
[student@localhost ~]$ bc --help
usage: bc [options] [file ...]
-h --help print this usage and exit
-i --interactive force interactive mode
-l --mathlib use the predefined math routines
......
并没有说明算术逻辑的符号,例如指数应用、小数点位数、加减乘除的符号等等。在开始说明之前,我们先来使用 bc 软件的功能, 进行一下,如果你没有计算机在身边时,如何通过文本界面来进行数值运算:
[student@localhost ~]$ bc bc 1.07.1 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 1+2+3+4 10 1/3 0 <==缺省不输出小数点后数据 quit
bc 指令为 Linux 纯文本界面下的小算盘,你可以使用 bc --help 查找到相关的选项数据,但是如上所示,加减乘除的符号, 还有小数点位数数据,以及离开 (quit) 等信息,则没有显示于 --help 的输出画面中。Linux 有提供一个名为 manual page (手册页) 的功能, 你可以用 manual 缩写 (man) 来查找,如下所示:
[student@localhost ~]$ man bc bc(1) General Commands Manual bc(1) NAME bc - An arbitrary precision calculator language SYNTAX bc [ -hlwsqv ] [long-options] [ file ... ] DESCRIPTION bc is a language that supports arbitrary precision numbers with interactive exe‐ cution of statements. There are some similarities in the syntax to the C pro‐ gramming language. A standard math library is available by command line option. If requested, the math library is defined before processing any files. bc starts by processing code from all the files listed on the command line in the order listed. After all files have been processed, bc reads from the standard input. All code is executed as it is read. (If a file contains a command to halt the processor, bc will never read from the standard input.) ...... OPTIONS -h, --help Print the usage and exit. -i, --interactive Force interactive mode. ...... VARIABLES ...... There are four special variables, scale, ibase, obase, and last. scale defines how some operations use digits after the decimal point. The default value of scale is 0. ibase and obase define the conversion base for input and output num‐ bers. The default for both input and output is base 10. last (an extension) is a variable that has the value of the last printed number. These will be dis‐ cussed in further detail where appropriate. All of these variables may have values assigned to them as well as used in expressions. ...... - expr The result is the negation of the expression. ++ var The variable is incremented by one and the new value is the result of the expression. -- var The variable is decremented by one and the new value is the result of the expression. var ++ The result of the expression is the value of the variable and then the variable is incremented by one. var -- The result of the expression is the value of the variable and then the variable is decremented by one. expr + expr The result of the expression is the sum of the two expressions. ...... MATH LIBRARY If bc is invoked with the -l option, a math library is preloaded and the default scale is set to 20. The math functions will calculate their results to the scale set at the time of their call. The math library defines the following functions: ...... EXAMPLES In /bin/sh, the following will assign the value of "pi" to the shell variable pi. pi=$(echo "scale=10; 4*a(1)" | bc -l) ...... AUTHOR Philip A. Nelson philnelson@acm.org ......
这个 man 是比较详细的数据,在该画面中,你可以使用底下的按钮来移动屏幕显示整份文档的位置:
有兴趣的话,读者们可以自己慢慢的阅读 man page。如果是短时间要查找重要的项目,例如我们需要调整输出的小数点位数 (scale) 时, 可以『跑到整份文档的第一行,然后输入斜线 / ,输入关键字』之后 man page 就可以自动帮你找关键字。
man page 除了上述的功能之外,其实 man page 的第一行也显示了该指令/文件的功能,例如 BC(1) 代表的是 1 号 man page, 而共有 9 种左右的 man page 号码,其意义为:
代号 | 代表内容 |
1 | 用户在shell环境中可以操作的指令或可运行档 |
2 | 系统内核可调用的函数与工具等 |
3 | 一些常用的函数(function)与函数库(library),大部分为C的函数库(libc) |
4 | 设备文件的说明,通常在/dev下的文件 |
5 | 设置档或者是某些文件的格式 |
6 | 游戏(games) |
7 | 惯例与协定等,例如Linux文件系统、网络协定、ASCII code等等的说明 |
8 | 系统管理员可用的管理指令 |
9 | 跟kernel有关的文档 |
上述的表格内容可以使用『man man』来更详细的取得说明。通过这张表格的说明, 未来你如果使用man page在察看某些数据时,就会知道该指令/文件所代表的基本意义是什么了。
[student@station10-101 ~]$ man man MAN(1) Manual pager utils MAN(1) NAME man - an interface to the on-line reference manuals SYNOPSIS man [-C file] [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale] [-m ...... DESCRIPTION ...... The table below shows the section numbers of the manual followed by the types of pages they contain. 1 Executable programs or shell commands 2 System calls (functions provided by the kernel) 3 Library calls (functions within program libraries) 4 Special files (usually found in /dev) 5 File formats and conventions eg /etc/passwd 6 Games 7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) 8 System administration commands (usually only for root) 9 Kernel routines [Non standard] ...... OPTIONS ...... Main modes of operation -f, --whatis Equivalent to whatis. Display a short description from the manual page, if available. See whatis(1) for details. -k, --apropos Equivalent to apropos. Search the short manual page descriptions for keywords and display any matches. See apropos(1) for details. ......文档说明中, -f 类似 whatis 指令,显示较为完整的查找,而 -k 则是与 apropos 指令相似,列出的是关键字的内容。 两者都是在查找有没有某个 man page 的意思。底下我们来查查看这两个差别在哪里:
[student@station10-101 ~]$ man -f passwd passwd: nothing appropriate. [student@station10-101 ~]$ man -k passwd passwd: nothing appropriate.见鬼!没东西!这是因为我们的 man 还没有建置数据库的问题所致。处理方案很简单,通过 root 运行 mandb 即可!
[student@station10-101 ~]$ su - 密码: [root@station10-101 ~]# mandb ...... Checking for stray cats under /usr/local/share/man... Checking for stray cats under /var/cache/man/local... 112 man subdirectories contained newer manual pages. 7466 manual pages were added. 0 stray cats were added. 0 old database entries were purged. [root@station10-101 ~]# exit logout [student@station10-101 ~]$下达指令前,请注意你的身份为何啊!
[student@station10-101 ~]$ man -f passwd openssl-passwd (1ssl) - compute password hashes passwd (1) - update user's authentication tokens passwd (5) - password file # 只有左侧是完整的 passwd 的项目,才会列出来 [student@station10-101 ~]$ man -k passwd chpasswd (8) - 成批更新用户的口令 chgpasswd (8) - update group passwords in batch mode fgetpwent_r (3) - get passwd file entry reentrantly getpwent_r (3) - get passwd file entry reentrantly gpasswd (1) - administer /etc/group and /etc/gshadow grub2-mkpasswd-pbkdf2 (1) - Generate a PBKDF2 password hash. lpasswd (1) - Change group or user password openssl-passwd (1ssl) - compute password hashes pam_localuser (8) - require users to be listed in /etc/passwd passwd (1) - update user's authentication tokens passwd (5) - password file passwd2des (3) - RFS password encryption pwhistory_helper (8) - Helper binary that transfers password hashes from passwd or shad... saslpasswd2 (8) - set a user's sasl password smbpasswd (5) - The Samba encrypted password file sslpasswd (1ssl) - compute password hashes userpasswd (1) - A graphical tool to allow users to change their passwords. vncpasswd (1) - change the VNC password # 无论是左侧还是右侧,只要有 passwd 这个关键字,就列出来!这样就很轻松的可以查找到,passwd(1) 是指令,可以变更密码, passwd(5) 则是文件,意思是密码档。
[student@station10-101 ~]$ man passwd PASSWD(1) User utilities PASSWD(1) NAME passwd - update user's authentication tokens SYNOPSIS passwd [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w warndays] [-i inactivedays] [-S] [--stdin] [username] DESCRIPTION The passwd utility is used to update user's authentication token(s). ......前面几行就告诉你,这是 (1) 的指令说明!如果想要知道 passwd(5) 的话,这样做:
[student@station10-101 ~]$ man 5 passwd PASSWD(5) Linux Programmer's Manual PASSWD(5) NAME passwd - password file DESCRIPTION The /etc/passwd file is a text file that describes user login accounts for the system. It should have read permission allowed for all users (many utilities, like ls(1) use it to map user IDs to usernames), but write access only for the superuser. ......如此就可以查找两个相同文件名,但不同位置与功能的说明文档了!
从前几小节的练习中,有时候我们会发现几件事情:(1)指令输出的数据量常常很大,一个屏幕装不下,连使用[shift]+[pageup] 都没有办法全部看完; (2)在 man bc 时,找那个 pi= 的项目中,范例提到在文本界面下,可以通过某些方式不要进入 bc 去算 pi !
尤其是第 2 个项目,里面就谈到那个 | 的符号,这个符号我们称作『管线 (pipe) 』! 它的目的是『将前一个指令输出的数据,交由后面的指令来处理』的意思~我们来谈谈该指令的意义:
[student@localhost ~]$ echo "scale=10; 4*a(1)" | bc -l
如果你将上面的指令分成两部份来看,第一部份先运行『echo "scale=10; 4*a(1)"』,就可以发现从屏幕上输出『 scale=10; 4*a(1) 』的字样,echo 这个指令很单纯的将后续的数据当成文本消息输出到屏幕上。 这些数据之后被带入到 bc 指令中,亦即直接在 bc 的环境中进行 scale=10; 4*a(1) 的运算之意。
有两个指令很常使用于大量数据输出时的片段展示,那就是 more 与 less。more 会一页一页翻动,但是无法向前回去查找之前的画面。 至于 less 就是 man page 的操作环境。
除了使用 | less 的功能加上斜线 (/) 找到关键字的方法之外,我们也可以通过 grep 来取得关键字! 以上头的例题来看,如果要使用 ll /etc/ 找出 passwd 的关键字『那一行』的话,可以简单的这样做:
[student@localhost ~]$ ll /etc/ | grep 'passwd'
在 Linux 的系统下,总是会需要用到文件管理的情况发生,包括创建目录与文件、拷贝与移动文件、删除文件与目录等等。 另外,读者也应该要知道在 Linux 的系统下,那些目录是正规系统会存在的,以及该目录又应该要放置哪些数据等等。
所有的 Linux distribions 理论上都应该要遵循当初 Linux 开发时所规范的各项标准,其中之一就是文件系统的阶层标准 (Filesystem Hierarchy Standard, FHS)。基本上 FHS 只是一个基本建议值,详细的数据还是保有让各个 distribution 自由设计的权力! 无论如何,FHS 还是规范了根目录与 /usr, /var 这三个目录内应该要放置的数据就是了。
另外,根据文件是否经常变动,以及文件运作的情况,大致上有几个比较重要的目录分类:
CentOS 7 以后 (包含 CentOS 8) 的系统目录规范跟以前的版本差异挺大的,详细的数据还请参考相关文档,底下仅就个别目录中应该要放置的数据做个基本的解释。 请自行『 ll / 』对照下表的相关目录说明。
目录名称 | 应放置文件内容(一定要知道的内容) |
/bin /sbin |
/bin 主要放置一般用户可操作的指令 /sbin 主要放置系统管理员可操作的指令 这两个数据目前都是链接档,分别链接到 /usr/bin, /usr/sbin 当中 |
/boot | 与开机有关的文件,包括内核文件 / 开机管理程序与设置档 |
/dev | 是 device 的缩写,放置设备档,包括硬盘档、键盘鼠标终端机文件等 |
/etc | 一堆系统设置档,包括帐号、密码与各式服务软件的设置档大多在此目录内 |
/home /root |
/home 是一般帐号的家目录缺省放置位置 /root 则是系统管理员的家目录了! |
/lib /lib64 |
系统函数库与内核函数库,其中 /lib 包含内核驱动程序,而其他软件的函数库若为 64 比特,则使用 /lib64 目录内的函数库文件。 这两个目录目前也都是链接到 /usr/lib, /usr/lib64 内。 |
/proc | 将内存内的数据做成文件类型,放置于这个目录下,连同某些内核参数也能手动调整 |
/sys | 跟 /proc 类似,只是比较针对硬件相关的参数方面。 |
/usr | 是 usr 不是 user 喔!是 unix software resource 的缩写,与 Unix 程序有关。从 CentOS 7 开始, 系统相关的所有软件、服务等,均放置在这个目录中了!因此不能与根目录分离。 |
/var | 是一些变动数据,系统运作过程中的服务数据、暂存数据、登录数据等等。 |
/tmp | 一些用户操作过程中会激活的暂存盘,例如 X 软件相关的数据等等。 |
Linux 是由工程师开发的,许多的目录也沿用 Unix 的规范,Unix 也是工程师开发的,所以许多的目录命名通常就与该目录要放置的数据有点相关性。 例如 bin, sbin 就类似 binary, system binary (二进位程序、系统管理二进位程序) 来结合这样~
目录名称 | 应放置文件内容(以后用到就知道了) |
/media /mnt |
/media 主要是系统上临时挂载使用的设备(如随插即用 USB)之惯用目录 /mnt 主要是用户或管理员自行暂时手动挂载的目录 |
/opt | /opt 是 optional 的意思,通常是第三方协力厂商所开发的软件放置处 |
/run | 系统进行服务软件运作管理的功能,CentOS 7以后,这个目录也放在内存当中了! |
/srv | 通常是给各类服务 (service) 放置数据使用的目录 |
另外,在 Linux 环境下,所有的目录都是根目录 (/) 衍生出来的,从根目录开始撰写的文件名也就被称为『绝对路径』。 而磁盘规划方面,若需要了解磁盘与目录树的搭配,可以使用 df (display filesystem) 的软件来查阅:
[student@localhost ~]$ df
文件系统 1K-区段 已用 可用 已用% 挂载点
devtmpfs 918108 0 918108 0% /dev
tmpfs 936140 0 936140 0% /dev/shm
tmpfs 936140 9388 926752 2% /run
tmpfs 936140 0 936140 0% /sys/fs/cgroup
/dev/mapper/centos-root 10475520 4359596 6115924 42% /
/dev/vda2 1998672 149448 1727984 8% /boot
/dev/mapper/centos-home 3135488 70240 3065248 3% /home
tmpfs 187228 16 187212 1% /run/user/42
tmpfs 187228 4 187224 1% /run/user/0
tmpfs 187228 4 187224 1% /run/user/1000
上表中最左侧为文件系统,最右侧则是挂载点。挂载点有点类似 windows 系统的 C:, D:, E: 等磁盘槽的意思。 在 Linux 底下所有的文件都是从目录树分出来,因此文件系统也需要跟目录结合在一起。以上表来说,『当你进入 /boot 这个目录时, 就可以看到 /dev/vda2 这个设备的内容』之意。
此外,系统也已经将内存仿真成文件系统,提供用户将暂存数据放置于高速的内存内。只是这些数据在关机后就会消失。 这些目录包括很重要的 /dev/shm (上表)。
缺省的情况下,用户取得 shell 的环境时,通常就是在自己的『家目录』,例如 windows 文件总管打开后, 出现在画面中的,通常是『我的文档夹』之类的环境。若要变更『工作目录』,例如变更工作目录到 /var/spool/mail 去,可以这样做:
[student@localhost ~]$ ls Desktop Downloads Music Public text1.txt Documents group Pictures Templates Videos [student@localhost ~]$ cd /var/spool/mail [student@localhost mail]$ ls rpc student
如上所示,一开始读者会在 student 家目录下,因此单纯使用 ls 时,会列出工作目录 (家目录) 底下的数据,亦即是一堆家目录应该会存在的目录名称。 而当读者操作『 cd /var/spool/mail 』之后,工作目录会变成该目录,所以提示字符里面也将 ~ 变成了 mail 了。因此使用 ls 所列出的工作目录下的数据, 就会有不一样的文件名出现。读者在操作指令时,要特别注意『工作目录』才行。而列出目前工作目录的方法为使用 pwd:
[student@localhost mail]$ pwd
/var/spool/mail
[student@localhost mail]$
读者操作系统时,不要只看提示字符下的文件名,最好能够查阅实际的目录较佳。如下案例:
[student@localhost mail]$ cd /etc [student@localhost etc]$ pwd /etc [student@localhost etc]$ cd /usr/local/etc [student@localhost etc]$ pwd /usr/local/etc [student@localhost etc]$
操作者可发现,自从进到 /etc 之后,提示字符内的目录位置一直是『 etc 』,然而使用 pwd 就能够发现两者的差异。 这在系统管理时非常的重要,若去错目录,会导致文件修订的错误!
除了根目录与家目录之外,Linux 上有一些比较特别的目录需要记忆:
目录名称 | 目录意义 |
/ | 根目录,从根目录写起的文件名只会存在一个 |
~ | 用户的家目录,不同用户的家目录均不相同 |
. | 一个小数点,代表的是『本目录』,亦即目前的工作目录之意 |
.. | 两个小数点,代表的是『上一层目录』 |
- | 一个减号,代表『上一次的工作目录』之意 |
操作者应该要注意,根据文件名写法的不同,也可将所谓的路径(path)定义为绝对路径(absolute)与相对路径(relative)。 这两种文件名/路径的写法依据是这样的:
由本章的说明,读者可以清楚 /etc 与 /boot 为两个相当重要的目录,其中 /etc 更是需要备份的所在。 若读者使用 student 的身份来暂时进行文件管理行为时,例如将 /etc 完整备份时,可以如何进行?
[student@localhost ~]$ cd /dev/shm [student@localhost shm]$ pwd /dev/shm
[student@localhost shm]$ mkdir backup [student@localhost shm]$ ll 总计 0 drwxrwxr-x. 2 student student 40 3月 4 13:14 backup
[student@localhost shm]$ cd backup [student@localhost backup]$ pwd /dev/shm/backup
[student@localhost backup]$ cp --help 用法:cp [选项]... [-T] 来源 目的地 或:cp [选项]... 来源... 目录 <==这个是最常用的指令串! 或:cp [选项]... -t 目录 来源... Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY. Mandatory arguments to long options are mandatory for short options too. -a, --archive same as -dR --preserve=all <==尝试完整保留权限 --attributes-only don't copy the file data, just the attributes --backup[=CONTROL] make a backup of each existing destination file ...... -R, -r, --recursive copy directories recursively <==拷贝目录 ...... # 开始尝试将 /etc 文件名拷贝到本目录 (.) 底下 [student@localhost backup]$ cp /etc . cp: -r not specified; omitting directory '/etc'因为 cp 会自动忽略目录的拷贝,因此需要如下的指令来拷贝目录才行
# 刚刚 cp --help 有看到 -r 可以拷贝目录,因此加上这个选项功能来处理目录拷贝: [student@localhost backup]$ cp -r /etc . cp: 无法打开 ‘/etc/crypttab’ 来读取数据: 拒绝不符权限的操作 cp: 无法访问 ‘/etc/pki/CA/private’: 拒绝不符权限的操作 cp: 无法访问 ‘/etc/pki/rsyslog’: 拒绝不符权限的操作 ....... [student@localhost backup]$ ll 总计 0 drwxr-xr-x. 135 student student 5020 3月 4 13:21 etc因为系统很多保密的文件是不许被一般用户所读取的,因此 student 许多文件无法顺利拷贝也是正确的!操作者无须担心。
[student@localhost backup]$ cp -r /etc . 2> /dev/null [student@localhost backup]$ ll -d /etc ./etc drwxr-xr-x. 135 student student 5020 3月 4 13:22 ./etc drwxr-xr-x. 135 root root 8192 3月 3 17:54 /etc
通过上面的练习,最终我们知道其实 student 身份拷贝的 /dev/shm/backup/etc 是没有完整的备份的!因为两者的容量大小、 内容文件、权限都不相同之故。至于相关的指令功能、选项功能等等,请自由 man cp、 man mkdir 来预先了解。
另外,在一些错误消息要丢弃的环境中,也可以在指令的最后面加上 2> /dev/null 来将错误的数据导向垃圾桶 (/dev/null)。
作业硬盘一般操作说明:
作业当中,某些部份可能为简答题~若为简答题时,请将答案写入 /home/student/ans.txt 当中,并写好正确题号,方便老师订正答案。 请注意,文件名写错将无法上传!
作业结果传输:请以 root 的身分运行 vbird_book_check_unit 指令上传作业结果。 正常运行完毕的结果应会出现【XXXXXX_aa:bb:cc:dd:ee:ff_unitNN】字样。若需要查阅自己上传数据的时间, 请在操作系统上面使用浏览器查找: http://192.168.251.254 检查相对应的课程文件。 相关流程请参考: vbird_book_check_unit