Red hat Linux 第二章


Red hat Linux 学习

第二章

从命令行管理(Manage)文件

目的(Objective)

确定(识别 identity)Linux 系统上重要目录的作用(目的 purpose)!
使用绝对路径(absolute path)和相对路径(reative path)指定文件名
学会在命令行使用 复制,移动和 删除文件和目录
用带参数的命令匹配一个或多个文件

文件系统的层次结构(hierarchy)

640?tp=webp


/***大家看一下配图***/
然后看解释:
/ 根目录 (比如一个树的根 , 说明很重要 ,系统总文件夹,所有的东西都在其下)
其下有一些主目录
/bin (系统命令 文件) /boot(系统启动文件) /dev(磁盘文件)
/etc(设备文件) /home(存放的用户) /root (超级用户的空间)
/sbin (放置一些系统管理常用的程序) /run(和mnt 差不多,系统文件挂载的目录) /tmp (让一般使用者暂时存放档案的地方) /usr (用户可以使用的一些文件,包括用户命令) /var (所有服务的登录文件或错误讯息档案,其下有一些帮助文件)/mnt (这是软盘与光盘预设挂载点的地方)
/*/* 这里说的不够详细 ,大家可以参考 http://blog.csdn.net/yigelangmandeshiren/article/details/7096599 博客 了解更多,不过我相信随着你学习的深入,你会慢慢理解这些文件的*/*/

也可以对比一下 最下面的 英文原版(Important Red Hat Enterprise Linux directories 重要的红帽企业Linux 目录), 相信多了解一些东西,只会让你在更优秀

通过文件名定位(locate)文件

1. 绝对路径和相对路径
绝对路径(absolute path),文件在系统下的绝对位置
相对路径(reative path),文件在当前(current)目录的相对位置
(先根据前面的目录的分层结构自己理解,然后看一下这个博客写的和你想的有什么出入?
---------------------------
///***如果还不理解,在微信下 回复 微社区 ,会推送一个网址,点击进入,进去提问,相信很快就会有答案,***//
---------------------------
当然你也可以网上查阅资料,)
2.导航路径(Navigating path)
查看自己当前的位置 用【pwd】 命令  用【ls】命令 当前目录下文件
用【cd】 命令 切换目录 用【touch】命令 + 文件名 ,新建一个文件(如果不指定路径(绝对/相对路径) 默认在当前目录)
【cd ..】 返回上一级目录 【cd .】 当前目录 【cd ~】 切换至家目录
还有命令 【cp / mv / rm / mkdir / rmdir 】
********************************************
『记住 第一章 如何获取帮助,大家可以回顾一下,返回微信会话窗口,点右上角,然后点击查看历史消息,可以查看昨天的 信息,章节』
********************************************
使用路径名展开(Expansion)匹配(Match)的文件名

模式匹配:
*        可以代替  任意数量字符
?       可以代替  任意单个字符
~       切换到家目录
~USERNAME  切换到某个用户的家目录
{} 可以放很多东西 ,用 逗号 或者 点隔开 下面有示例!
// ~+        
//~-
-        切换到上次所在的目录
[abc..]   指定一些文件abc...
[!abc..]  除过abc...之外的内容[^abc...] 两个一样
//[[:alpha:]]
// [[:lower:]]
// [[:upper:]]
// [[:alnum:]]
// [[:punct:]]
// [[:digit:]]
// [[:space:]]
  Note

示例1
mkdir glob ; cd glob
touch alfa able blue cables
ls [会看到 alfa able blue cables]
ls a* [会看到 alfa able]
ls [ac]* 【也会看到全部】
ls ????    
ls ????? 自己体验

示例2

[student@desktopX glob]$ ls ~/glob
able alfa baker bravo cast charlie delta dog easy echo
[student@desktopX glob]$ echo ~/glob
/home/student/glob
[student@desktopX glob]$ echo {Sunday,Monday,Tuesday,Wednesday}.log
Sunday.log Monday.log Tuesday.log Wednesday.log
[student@desktopX glob]$ echo file{1..3}.txt
file1.txt file2.txt file3.txt
[student@desktopX glob]$ echo file{a..c}.txt
filea.txt fileb.txt filec.txt
[student@desktopX glob]$ echo file{a,b}{1,2}.txt
filea1.txt filea2.txt fileb1.txt fileb2.txt
[student@desktopX glob]$ echo file{a{1,2},b,c}.txt
filea1.txt filea2.txt fileb.txt filec.txt

示例 3:

[student@desktopX glob]$ echo Today is `date +%A`.
Today is Wednesday.
[student@desktopX glob]$ echo The time is $(date +%M) minutes past $(date +%l%p).
The time is 26 minutes past 11AM.
[student@desktopX glob]$ host=$(hostname); echo $host
desktopX
[student@desktopX glob]$ echo "***** hostname is ${host} *****"
***** hostname is desktopX *****
[student@desktopX glob]$ echo Your username variable is \$USER.
Your username variable is $USER.
[student@desktopX glob]$ echo "Will variable $host evaluate to $
(hostname)?"
Will variable desktopX evaluate to desktopX?
[student@desktopX glob]$ echo 'Will variable $host evaluate to $(hostname)?'
Will variable $host evaluate to $(hostname)?

LAB 1
首先 创建一个空目录(为了实现这个实验),当我们要创建多个文件时,用
命令行时很轻松的。
1. 12个文件 (内容时 两个季度 每季度6个情节)形如“tv_seasonX_episodeY.ogg” X 是切换的数据! 使用一个命令完成这个任务
2.一个作家写了一个秘密小说,你的下一个章节将要被编辑出版。共创建8个文件 “mystery_chapterX.odf“ X 代表数字,使用一个命令完成任务
3.在Videos下创建两个目录,“season1 , season2 "使用一个命令完成这个任务
4.用两个命令分别将tv_seasonX_seasonY.ogg 文件移动到新建的对应的 season 下

5.在Documents目录下建立一个目录“my_bestseller",在其下建立一个chapters目录
6.在“my_bestseller”下使用一个命令创
建“plot_change,vacation,editor"
7.使用家目录的快捷方式指定源文件,移动所有的章节(mystery_chapterX.odf) 到chapters目录中。(你现在在哪?(当前位置?使用最简单的语法指定目标目录!))
8.前两个章节需要给编辑,不要改变这些章节,把这两个章节只给编辑(editor)目录,在chapters目录下使用相对路径的方法实现。(把你的当前目录切换到chapters目录下实现这个lab)
9.  7.8章节需要在假期时写,移动这两个文件到 vacations 目录下
使用一个命令不使用通配符
10.用一个命令,改变工作目录到season2下, 把第一个情节链接到 vacation下,链接命令自己上网查! 【命令是 ln ,具体实现自己通过系统查看帮助,第一章有,不会就去复习吧,】
11.用一个命令,改变工作目录到 vocation 下, 然后显示改目录下的文件,用快捷键切换回 上一个目录season2 下,将情节2 也链接到vacation 下,然后用快捷键切换到vacation 目录下,(这个题有点考察细节,请认真查看基础知识,绝对没有超范围哟!)
12.小说的第五章节和第六章节需要修改,复制这两个文件到plot_change目录下,移出一个目录到 vacation 父目录,用一个命令
13.跟踪变化(这个实验要仔细查看命令的帮助,下面有,不会请上微社区提问,当然,也可以百度,)改变一个文件包含改变的日期时间,去 plot_change 目录下,复制第五个章节,在文件尾 加上改变的秒数,再次复制第五章节,让其文件名后有用户的信息“$USER"
14.删除 plot_change 这个目录,这个目录不能被删除,因为正在运行(打开),退出,尝试使用rm 删除不使用recursive参数,将失败,使用rmdir 将成功!自己做一下!

--------------------------------------------------------

THAT‘S ALL    


我的和原版还是有些出入的,因为加入了自己的想法,希望大家可以看一下原版,自己理解一下!

下面是英文原版:



Red Hat System Administration IUNIT 2
Managing Files Form The Command Line
www.westos.orgObjectives
identify the purpose for important directorys on a
linux system
Specify files using absolute and reative path
names
Create copy,move and remove files and
directories using command-line utilties
Match one or more file names using shell
expansion as argument to shell commands
The file system hierarchy
Important Red Hat Enterprise Linux directories

/usr






/mnt

divice temporary mount point

A world-writable space for temporary files.
Files which are more than 10 days old are
deleted from this directory automatically.
Another temporary directory exists,
/var/tmp, in which files that have not
been accessed, changed, or modified in
more than 30 days are deleted
automatically
/boot


Home directory for the administrative
superuser, root
/tmp
system data directory
Runtime data for processes started since the
last boot. This includes process ID files and lock
files,among other things. The contents of this
directory are recreated on reboot.
Home directories where regular users
store their personal data and configuration
files
/root
Configuration files specific to this system
/run



/var


/home
/etc


Installed software, shared libraries, include files,
and static read-only program data. Important
subdirectories include:
[/usr/bin]:
User commands.
[/usr/sbin]:
Systemadministration commands.
[/usr/local]:
Locally customized software

Files needed in order to start the boot process
/dev

Contains special device files which are
used by the system to access hardware
www.westos.orgLocating Files by Name
Absolute paths and relative paths
– Absolute paths:
The true path in the system file
– relative paths:
the name about relative to the current directory
Navigating paths
– Notes: please look at the example on next page
www.westos.orgExample 1
[student@desktopX ~]$ pwd
/home/student
[student@desktopX ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
[student@desktopX ~]$ cd Videos
[student@desktopX Videos]$ pwd
/home/student/Videos
[student@desktopX Videos]$ cd /home/student/Documents
[student@desktopX Documents]$ pwd
/home/student/Documents
[student@desktopX Documents]$ cd
[student@desktopX ~]$ pwd
/home/student
[student@desktopX ~]$ touch Videos/blockbuster1.ogg
[student@desktopX ~]$ touch Videos/blockbuster2.ogg
[student@desktopX ~]$ touch Documents/thesis_chapter1.odf
[student@desktopX ~]$ touch Documents/thesis_chapter2.odf
www.westos.orgExample 2
[student@desktopX ~]$ ls -l
total 15
drwxr-xr-x. 2 student student 4096 Feb 7 14:02 Desktop
drwxr-xr-x. 2 student student 4096 Jan 9 15:00 Documents
... ...
drwxr-xr-x. 2 student student 4096 Jan 9 15:00 Videos
[student@desktopX ~]$ ls -a
total 15
drwx------. 16 student student 4096 Feb 8 16:15 . drwxr-xr-x. 6 root root 4096 Feb 8
16:13 .. -rw-------. 1 student student 22664 Feb 8 00:37 .bash_history
-rw-r--r--. 1 student student 18 Jul 9 2013 .bash_logout
... ...
drwxr-xr-x. 11 student student 4096 Feb 6 13:07 .gnome2
drwxr-xr-x. 2 student student 4096 Jan 9 15:00 Videos
[student@desktopX ~]$ ls -R
.:
Desktop Documents Downloads Music Pictures Public Templates Videos
./Desktop:
./Documents:
thesis_chapter1.odf thesis_chapter2.odf
... ...
www.westos.orgExample 3-1
[student@desktopX ~]$ cd Videos
[student@desktopX Videos]$ pwd
/home/student/Videos
[student@desktopX Videos]$ cd /home/student/Documents
[student@desktopX Documents]$ pwd
/home/student/Documents
[student@desktopX Documents]$ cd -
[student@desktopX Videos]$ pwd
/home/student/Videos
[student@desktopX Videos]$ cd -
[student@desktopX Documents]$ pwd
[student@desktopX Documents]$ cd -
[student@desktopX Videos]$ pwd
/home/student/Videos
[student@desktopX Videos]$ cd
www.westos.orgExample 3-2
[student@desktopX Videos]$ pwd
/home/student/Videos
[student@desktopX Videos]$ cd .
[student@desktopX Videos]$ pwd
/home/student/Videos
[student@desktopX Videos]$ cd ..
[student@desktopX ~]$ pwd
/home/student
[student@desktopX ~]$ cd ..
[student@desktopX home]$ pwd
/home
[student@desktopX home]$ cd ..
[student@desktopX /]$ pwd
/
[student@desktopX /]$ cd
[student@desktopX ~]$ pwd
/home/student
www.westos.orgManaging Files Using Command-Line Tools

Command-line file management
www.westos.orgMatching File Names Using Path Name Expansion
Pattern matching
www.westos.orgExample 1
[student@desktopX ~]$ mkdir glob; cd glob
[student@desktopX glob]$ touch alfa bravo charlie delta echo able baker cast
dog easy
[student@desktopX glob]$ ls
able alfa baker bravo cast charlie delta dog easy echo
[student@desktopX glob]$ ls a*
able alfa
[student@desktopX glob]$ ls *a*
able alfa baker bravo cast charlie delta easy
[student@desktopX glob]$ ls [ac]*
able alfa cast charlie
[student@desktopX glob]$ ls ????
able alfa cast easy echo
[student@desktopX glob]$ ls ?????
baker bravo delta
www.westos.orgExample 2
[student@desktopX glob]$ ls ~/glob
able alfa baker bravo cast charlie delta dog easy echo
[student@desktopX glob]$ echo ~/glob
/home/student/glob
[student@desktopX glob]$ echo {Sunday,Monday,Tuesday,Wednesday}.log
Sunday.log Monday.log Tuesday.log Wednesday.log
[student@desktopX glob]$ echo file{1..3}.txt
file1.txt file2.txt file3.txt
[student@desktopX glob]$ echo file{a..c}.txt
filea.txt fileb.txt filec.txt
[student@desktopX glob]$ echo file{a,b}{1,2}.txt
filea1.txt filea2.txt fileb1.txt fileb2.txt
[student@desktopX glob]$ echo file{a{1,2},b,c}.txt
filea1.txt filea2.txt fileb.txt filec.txt
www.westos.orgExample 3
[student@desktopX glob]$ echo Today is `date +%A`.
Today is Wednesday.
[student@desktopX glob]$ echo The time is $(date +%M) minutes past $(date +%l%p).
The time is 26 minutes past 11AM.
[student@desktopX glob]$ host=$(hostname); echo $host
desktopX
[student@desktopX glob]$ echo "***** hostname is ${host} *****"
***** hostname is desktopX *****
[student@desktopX glob]$ echo Your username variable is \$USER.
Your username variable is $USER.
[student@desktopX glob]$ echo "Will variable $host evaluate to $
(hostname)?"
Will variable desktopX evaluate to desktopX?
[student@desktopX glob]$ echo 'Will variable $host evaluate to $(hostname)?'
Will variable $host evaluate to $(hostname)?
www.westos.orgLab-1
<Lab1>
To begin, create sets of empty practice files to use in this lab. If an intended
shell expansion shortcut is not immediately recognized, students are
expected to use the solution to learn and practice. Use shell tab completion
to locate file path names easily.
Create a total of 12 files with names "tv_seasonX_episodeY.ogg". Replace X
with the season number and Y with that season's episode, for two seasons of
six episodes each.
<Lab2>
As the author of a successful series of mystery novels, your next bestseller's
chapters are being edited for publishing. Create a total of eight files with
names "mystery_chapterX.odf". Replace X with the numbers 1 through 8
<Lab3>
To organize the TV episodes, create two subdirectories named "season1" and
"season2" under the existing "Videos" directory. Use one command.
www.westos.orgLab-2
<Lab4>
Move the appropriate TV episodes into the season subdirectories. Use only
two commands,specifying destinations using relative syntax.
<Lab5>
To organize the mystery book chapters, create a two-level directory hierarchy
with one command.Create "my_bestseller" under the existing "Documents"
directory, and chapters beneath the new "my_bestseller" directory
<Lab6>
Using one command, create three more subdirectories directly under the
"my_bestseller" directory.Name these subdirectories "editor", "plot_change",
"and vacation". The create parent option is not needed since the
"my_bestseller" parent directory already exists
<Lab7>
Change to the "chapters" directory. Using the home directory shortcut to
specify the source files, move all book chapters into the "chapters" directory,
which is now your current directory. What is the simplest syntax to specify the
destination directory?
www.westos.orgLab-3
<Lab8>
The first two chapters are sent to the editor for review. To remember to not modify these
chapters during the review, move those two chapters only to the "editor" directory. Use
relative syntax starting from the "chapters" subdirectory
<Lab9>
Chapters 7 and 8 will be written while on vacation. Move the files form
"chapters" to "vacation". Use one command without wildcard characters.
<Lab10>
With one command, change the working directory to the season 2 TV episodes
location, then link (not copy) the first episode of the season to the "vacation"
directory.
<Lab11>
With one command, change the working directory to "vacation", then list its
files. Episode 2 is also needed. Return to the "season2" directory using the
previous working directory shortcut. This will succeed if the last directory
change was accomplished with one command. Link the episode 2 file into
"vacation". Return to "vacation" using the shortcut again
www.westos.orgLab-4
<Lab12>
Chapters 5 and 6 may need a plot change. To prevent these changes from
modifying original files, copy, not link, both files into "plot_change". Move up
one directory to "vacation's" parent directory, then use one command from
there.
<Lab13>
To track changes, save each version as a new file name to include the date.
Change to the "plot_change" directory. Copy "mystery_chapter5.odf",
appending the current timestamp (as the number of seconds since the epoch)
to ensure a unique file name. See the solution for syntax. Also make a copy
appending the current user ($USER) to the file name.
<Lab14>
The plot changes were not successful. Delete the "plot_change" directory.
First, delete all of the files in the "plot_change" directory. Change directory up
one level because the directory cannot be deleted while it is the working
directory. Try to delete the directory using the "rm" command without the
recursive option. This attempt should fail. Now use the "rmdir" command,
which will succeed.
www.westos.orgLab-5
When the "vacation" is over, the vacation directory is no longer needed. Delete
it using the rm command with the recursive option. Confirm that the original
files still remain after their linked versions have been removed.
When finished, return to the home directory
www.westos. When finished, return to the home directory


That's all


欢迎评论,提出观点。 转载请说明出处,谢谢~


如果大家想了解更多请关注微信公众号“IT那些事”(ID: haoyuwl)