码迷,mamicode.com
首页 > 其他好文 > 详细

学习 Unix 常用命令

时间:2017-06-01 18:37:58      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:input   roc   ons   效果   参数   lan   复杂   nat   nbsp   

第一个是 man 命令,作用是:"Display system documentation",我猜测英文是 manual 的缩写。通过这个命令,我们能了解接下来要学习的命令的文档。

ls, pwd, cd 的基本用法我觉得不需要额外说明。

cp。

cp命令的复杂性源于文件系统的复杂性。常遇到的情况有:如何处理软链接文件?

如果目标文件存在,怎么办?如果目标文件存在,但是打不开,怎么处理?

source file 是一个文件夹的话,要复制多少,是复制整个文件夹,还是部分?

 

这些都能通过 cp 的参数来解决。

对于已存在的目标文件

-f 在权限范围内,强制删除然后新建并复制过去;

-i 提示用户,并等待用户选择

-n 不覆盖已存在文件,并且不会提示

软链接

-L    If the -R option is specified, all symbolic links are followed.

这个解释少了半截。搜索了一下,完整的意思是这样““Follows the links in the source to find the file to copy instead of copying the links ””

 

-P    If the -R option is specified, no symbolic links are followed.  This is the default.

默认情况下,只复制软链接而不复制它所对应的文件

源文件为文件夹

-R    If source_file designates a directory, cp copies the directory and

           the entire subtree connected at that point.  If the source_file

           ends in a /, the contents of the directory are copied rather than

           the directory itself.  This option also causes symbolic links to be

           copied, rather than indirected through, and for cp to create spe-

           cial files rather than copying them as normal files.  Created

           directories have the same mode as the corresponding source direc-

           tory, unmodified by the process‘ umask.

 

           In -R mode, cp will continue copying even if errors are detected.

 

           Note that cp copies hard-linked files as separate files.  If you

           need to preserve hard links, consider using tar(1), cpio(1), or

           pax(1) instead.

这里有一个比较隐蔽的选项,如果源文件是文件夹,并且它的路径以 ‘/‘ 结尾,就复制该文件夹下的所有文件过去(不放在该文件夹中)而不是复制该文件夹过去。

我们做一下测试

mkdir source_dir target_dir
$ touch source_dir/file1 source_dir/file2
$ ls source_dir
file1	file2
$ ls target_dir
$ cp -R source_dir target_dir/
$ ls target_dir/
source_dir
$ cp -R source_dir/ target_dir/
$ ls target_dir/
file1		file2		source_dir

这些参数可以组合使用,组合使用后也有问题,如果参数的作用冲突了,你还要弄清冲突后的效果。这里我暂时不深入了。

rm

rm 与 cp 类似,也存在文件夹、软链接的问题。

不带参数的 rm 只能删除非目录类型的文件。

删除文件夹

-d          Attempt to remove directories as well as other types of files.

尝试了一下,发现不能移除非空文件夹

如果要移除非空文件夹,使用 -R

软链接

The rm utility removes symbolic links, not the files referenced by the links.

mkdir

创建文件夹。

默认情况下,如果要创建的文件夹的中间路径有不存在的,那么会报错;

如果有 -p 选项,那么会自动创建中间不存在的文件夹;

ln

man ln 的说明看的有点糊涂,于是找到了 ln (Unix) 上的解释

通过链接文件,不同的文件名可以指向同一个文件。

ln 可以创建两种类型的链接文件:

  1. 符号链接,也称软链接,这是指向另一个不同路径文件的一个符号路径。
  2. 硬链接,这是一个存储了链接建立时它所指向文件的实际数据的文件副本。

从以下命令示例可看出两种链接文件的区别:

$ echo ‘文件内容‘ > oringinal.file
$ ln oringinal.file hardlink.file
$ ln -s oringinal.file softlink.file
$ cat softlink.file
文件内容
$ rm oringinal.file
$ cat softlink.file
cat: softlink.file: 没有那个文件或目录
$ cat hardlink.file
文件内容

原始文件被删除后,符号链接将失效,访问软链接时,会提示找不到文件,但硬链接文件还在,而且还保存有原始文件的内容。

硬链接与 cp 有什么不一样?  

要理解这个,先要理解 inodes。这里 http://teaching.idallen.com/cst8207/12f/notes/460_links_and_inodes.html 讲的很详细。看完就就会理解下面的结论。

Thus, on Unix, a file can have many names, even across different directories. You may use any of the several names of a file to find the inode for the file. Unix calls these names "pointers" or "links" to the file.

 

Note that the "rm" command does not delete a file - it only deletes a name-inode map for a file. Only when all the name-inode maps are gone does the actual file data space get reclaimed.

 读完上面那篇文章,我们能够知道,如果更改硬链接的文件,那么原始文件、软链接的文件都会变化,因为它们都指向相同的 inodes。但是复制出的文件就不会变化,因为是不同的 inodes 了。代码如下:

$ echo ‘file content‘ > original.file
$ ln original.file hardlink.file
$ ln -s original.file softlink.file
$ cp original.file copied_original.file
$ rm original.file
$ ln hardlink.file original.file
# 更改文件
$ vim original.file
# 更改后的文件内容
$ cat original.file
file content
updated!!!
$ cat hardlink.file
file content
updated!!!
$ cat softlink.file
file content
updated!!!
# 使用 cp 复制出来的没有变化
$ cat copied_original.file
file content

  

mv

移动文件(夹)。可以看成是 cp 与 rm 的结合

rm -f destination_path && cp -pRP source_file destination && rm -rf source_file    
cp -pRP

从源文件复制过去,保留各种属性信息,复制软链接而不是它的源文件,如果是文件夹,复制整个文件夹过去。

touch

改变文件的访问和修改时间。如果文件不存在,那么创建这个文件,文件权限为默认权限。

cat

cat -- concatenate and print files

     The command:

 

           cat file1 file2 > file3

 

     will sequentially print the contents of file1 and file2 to the file file3, truncating file3 if it already exists.  See the manual page for your shell (i.e., sh(1)) for more

     information on redirection.

 

     The command:

 

           cat file1 - file2 - file3

 

     will print the contents of file1, print data it receives from the standard input until it receives an EOF (`^D‘) character, print the contents of file2, read and output contents

     of the standard input again, then finally output the contents of file3.  Note that if the standard input referred to a file, the second dash on the command-line would have no

     effect, since the entire contents of the file would have already been read and printed by cat when it encountered the first `-‘ operand.

cat file1 file2 > file3 会将 file1, file2 的内容联合起来,写入到 file3。file3 的内容会丢失。

cat file1 - file2 - file3 会打印输入的,直到 EOF 为止。不知道这个的作用是什么。

 

学习 Unix 常用命令

标签:input   roc   ons   效果   参数   lan   复杂   nat   nbsp   

原文地址:http://www.cnblogs.com/jay54520/p/6930145.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!