Linux文件操作实战
下面我们将要做些真正的文件操作,让我们先建立一个安全地带, 来玩一下文件操作命令。
首先,我们需要一个工作目录。
在我们的家目录下创建一个叫做“playground”的目录。
mkdir 命令被用来创建目录。
首先确定我们在我们的家目录下,来创建 playground 目录, 然后创建这个新目录:
yu@ubuntu:~$ cd
yu@ubuntu:~$ pwd
/home/yu
yu@ubuntu:~$ mkdir playground
yu@ubuntu:~$ ls
a.out Documents examples.desktop Pictures Public Videos
Desktop Downloads Music playground Templates
在 playground 目录下创建一对目录 ,分别叫做 “dir1” 和 “dir2”。
更改我们的当前工作目录到 playground,然后 执行 mkdir 命令:
yu@ubuntu:~$ cd playground
yu@ubuntu:~/playground$ pwd
/home/yu/playground
yu@ubuntu:~/playground$ mkdir dir1 dir2
yu@ubuntu:~/playground$ ls
dir1 dir2
注意到 mkdir 命令可以接受多个参数,它允许我们用一个命令来创建这两个目录。
使用 cp 命令,我们从 /etc 目录复制 passwd 文件到当前工作目录下:
yu@ubuntu:~/playground$ cp /etc/passwd .
yu@ubuntu:~/playground$ ls -l
total 12
drwxrwxr-x 2 yu yu 4096 Oct 26 03:59 dir1
drwxrwxr-x 2 yu yu 4096 Oct 26 03:59 dir2
-rw-r--r-- 1 yu yu 1656 Oct 26 04:03 passwd
注意:我们怎样使用当前工作目录的快捷方式,命令末尾的单个圆点。
重复操作复制命令,使用”-v”选项,看一个它的作用:
yu@ubuntu:~/playground$ cp -v /etc/passwd .
`/etc/passwd' -> `./passwd'
cp 命令再一次执行了复制操作,但是这次显示了一条简洁的信息,指明它 进行了什么操作。
注意,cp 没有警告,就重写了第一次复制的文件。
为了得到警示信息,在命令中包含”-i”选项:
yu@ubuntu:~/playground$ cp -i /etc/passwd .
cp: overwrite `./passwd'? y
响应命令提示信息,输入”y”,文件就会被重写,其它的字符(例如,”n”)会导致 cp 命令不理会文件。
现在,”passwd” 这个名字,看起来不怎么有趣,所以我们给它改个名字:
yu@ubuntu:~/playground$ mv passwd fun
yu@ubuntu:~/playground$ ls -l
total 12
drwxrwxr-x 2 yu yu 4096 Oct 26 03:59 dir1
drwxrwxr-x 2 yu yu 4096 Oct 26 03:59 dir2
-rw-r--r-- 1 yu yu 1656 Oct 26 04:08 fun
让我们来传送 fun 文件,通过移动重命名的文件到各个子目录, 然后再把它移回到当前目录
移动到dir1目录:
yu@ubuntu:~/playground$ mv fun dir1
yu@ubuntu:~/playground$ ls
dir1 dir2
yu@ubuntu:~/playground$ cd dir1
yu@ubuntu:~/playground/dir1$ ls
fun
再把 fun 文件从 dir1 移到目录 dir2:
yu@ubuntu:~/playground/dir1$ cd ..
yu@ubuntu:~/playground$ mv dir1/fun dir2
yu@ubuntu:~/playground$ cd dir2
yu@ubuntu:~/playground/dir2$ ls
fun
最后,再把 fun 文件带回到当前工作目录:
yu@ubuntu:~/playground/dir2$ cd ..
yu@ubuntu:~/playground$ mv dir2/fun .
yu@ubuntu:~/playground$ ls
dir1 dir2 fun
现在,我们试着创建链接。首先是硬链接。我们创建一些关联我们 数据文件的链接:
yu@ubuntu:~/playground$ ln fun fun-hard
yu@ubuntu:~/playground$ ln fun dir1/fun-hard
yu@ubuntu:~/playground$ ln fun dir2/fun-hard
所以现在,我们有四个文件”fun”的实例。看一下目录 playground 中的内容:
yu@ubuntu:~/playground$ ls -l
total 16
drwxrwxr-x 2 yu yu 4096 Oct 26 04:25 dir1
drwxrwxr-x 2 yu yu 4096 Oct 26 04:25 dir2
-rw-r--r-- 4 yu yu 1656 Oct 26 04:08 fun
-rw-r--r-- 4 yu yu 1656 Oct 26 04:08 fun-hard
注意到一件事,列表中,文件 fun 和 fun-hard 的第二个字段是”4”,这个数字 是文件”fun”的硬链接数目。
你要记得一个文件至少有一个硬链接,因为文件名就是由链接创建的。
那么,我们怎样知道实际上 fun 和 fun-hard 是一样的文件呢?
当我们创建文件硬链接的时候,实际上是为文件创建了额外的名字部分, 并且这些名字都关系到相同的数据部分。
这时系统会分配一连串的磁盘给所谓的索引节点,然后索引节点与文 件名字部分相关联。
因此每一个硬链接都关系到一个具体的包含文件内容的索引节点。
ls 命令有一种方法,来展示(文件索引节点)的信息。在命令中加上”-i”选项:
yu@ubuntu:~/playground$ ls -li
total 16
952134 drwxrwxr-x 2 yu yu 4096 Oct 26 04:25 dir1
960506 drwxrwxr-x 2 yu yu 4096 Oct 26 04:25 dir2
960507 -rw-r--r-- 4 yu yu 1656 Oct 26 04:08 fun
960507 -rw-r--r-- 4 yu yu 1656 Oct 26 04:08 fun-hard
第一字段表示文件索引节点号,正如我们所见到的, fun 和 fun-hard 共享一样的索引节点号,这就证实这两个文件是一样的文件。
建立符号链接的目的是为了克服硬链接的两个缺点:
1、硬链接不能跨越物理设备。
2、 硬链接不能关联目录,只能是文件。
符号链接是文件的特殊类型,它包含一个指向 目标文件或目录的文本指针。
符号链接的建立过程相似于创建硬链接:
yu@ubuntu:~/playground$ ln -s fun fun-sym
yu@ubuntu:~/playground$ ln -s ../fun dir1/fun-sym
yu@ubuntu:~/playground$ ln -s ../fun dir2/fun-sym
第一个实例相当直接,在 ln 命令中,简单地加上”-s”选项就可以创建一个符号链接, 而不是一个硬链接。
记住,当我们创建一个符号链接 的时候,会建立一个目标文件在哪里和符号链接有关联的文本描述。
如果我们看看 ls 命令的输出结果,比较容易理解。
yu@ubuntu:~/playground$ ls -l dir1
total 4
-rw-r--r-- 4 yu yu 1656 Oct 26 04:08 fun-hard
lrwxrwxrwx 1 yu yu 6 Oct 26 04:54 fun-sym -> ../fun
目录 dir1 中,fun-sym 的列表说明了它是一个符号链接,通过在第一字段中的首字符”l” 可知,并且它还指向”../fun”,也是正确的。
相对于 fun-sym 的存储位置,fun 在它的 上一个目录。
同时注意,符号链接文件的长度是6,这是字符串”../fun”所包含的字符数, 而不是符号链接所指向的文件长度。
当建立符号链接时,你既可以使用绝对路径名:
ln -s /home/yu/playground/fun dir1/fun-sym
也可用相对路径名,正如前面例题所展示的。
使用相对路径名更令人满意, 因为它允许一个包含符号链接的目录重命名或移动,而不会破坏链接。
除了普通文件,符号链接也能关联目录:
yu@ubuntu:~/playground$ ln -s dir1 dir1-sym
yu@ubuntu:~/playground$ ls -l
total 16
drwxrwxr-x 2 yu yu 4096 Oct 26 04:54 dir1
lrwxrwxrwx 1 yu yu 4 Oct 26 04:58 dir1-sym -> dir1
drwxrwxr-x 2 yu yu 4096 Oct 26 04:54 dir2
-rw-r--r-- 4 yu yu 1656 Oct 26 04:08 fun
-rw-r--r-- 4 yu yu 1656 Oct 26 04:08 fun-hard
lrwxrwxrwx 1 yu yu 3 Oct 26 04:53 fun-sym -> fun
移动文件和目录
rm 命令被用来删除文件和目录。
首先,删除一个硬链接:
yu@ubuntu:~/playground$ rm fun-hard
yu@ubuntu:~/playground$ ls -l
total 12
drwxrwxr-x 2 yu yu 4096 Oct 26 04:54 dir1
lrwxrwxrwx 1 yu yu 4 Oct 26 04:58 dir1-sym -> dir1
drwxrwxr-x 2 yu yu 4096 Oct 26 04:54 dir2
-rw-r--r-- 3 yu yu 1656 Oct 26 04:08 fun
lrwxrwxrwx 1 yu yu 3 Oct 26 04:53 fun-sym -> fun
文件 fun-hard 消失了,文件 fun 的链接数从4减到3。
下一步,我们会删除文件 fun,仅为了娱乐,我们会包含”-i” 选项,看一个它的作用:
yu@ubuntu:~/playground$ rm -i fun
rm: remove regular file `fun'? y
yu@ubuntu:~/playground$ ls -l
total 8
drwxrwxr-x 2 yu yu 4096 Oct 26 04:54 dir1
lrwxrwxrwx 1 yu yu 4 Oct 26 04:58 dir1-sym -> dir1
drwxrwxr-x 2 yu yu 4096 Oct 26 04:54 dir2
lrwxrwxrwx 1 yu yu 3 Oct 26 04:53 fun-sym -> fun
注意,fun-sym 发生了 什么事(显示为红色)?
因为它是一个符号链接,指向已经不存在的文件,链接已经坏了。
如果我们试着使用 损坏的链接,会看到以下情况:
yu@ubuntu:~/playground$ less fun-sym
fun-sym: No such file or directory
删除符号链接:
yu@ubuntu:~/playground$ rm fun-sym dir1-sym
yu@ubuntu:~/playground$ ls -l
total 8
drwxrwxr-x 2 yu yu 4096 Oct 26 04:54 dir1
drwxrwxr-x 2 yu yu 4096 Oct 26 04:54 dir2
对于符号链接,有一点值得记住,执行的大多数文件操作是针对链接的对象,而不是链接本身。
而 rm 命令是个特例。当你删除链接的时候,删除链接本身,而不是链接的对象。
最后,用 rm 命令加上选项(-r),来删除目录 playground, 和目录下的所有内容,包括子目录:
yu@ubuntu:~$ rm -r playground
yu@ubuntu:~$ ls -l
total 44
-rw-rw-r-- 1 yu yu 0 Oct 25 04:44 a.out
drwxr-xr-x 2 yu yu 4096 Oct 25 00:41 Desktop
drwxr-xr-x 2 yu yu 4096 Oct 25 00:41 Documents
drwxr-xr-x 2 yu yu 4096 Oct 25 00:41 Downloads
-rw-r--r-- 1 yu yu 8445 Oct 25 00:17 examples.desktop
drwxr-xr-x 2 yu yu 4096 Oct 25 00:41 Music
drwxr-xr-x 2 yu yu 4096 Oct 25 00:41 Pictures
drwxr-xr-x 2 yu yu 4096 Oct 25 00:41 Public
drwxr-xr-x 2 yu yu 4096 Oct 25 00:41 Templates
drwxr-xr-x 2 yu yu 4096 Oct 25 00:41 Videos
恢复实战前状态。
知识来源:http://billie66.github.io/TLCL/book/zh/chap05.html