mrbone / mrbone.github.io

0 stars 0 forks source link

Learn UNIX in 10 minutes #66

Closed mrbone closed 6 years ago

mrbone commented 6 years ago

Learn UNIX in 10 minutes

mrbone commented 6 years ago

67

mrbone commented 6 years ago

Unix 常用命令

[TOC]

目录命令

ls -l    list a directory in long ( detailed ) format

   for example:
$ ls -l 
drwxr-xr-x    4 cliff    user        1024 Jun 18 09:40 WAITRON_EARNINGS
-rw-r--r--    1 cliff    user      767392 Jun  6 14:28 scanlib.tar.gz
^ ^  ^  ^     ^   ^       ^           ^      ^    ^      ^
| |  |  |     |   |       |           |      |    |      |  
| |  |  |     | owner   group       size   date  time    name 
| |  |  |     number of links to file or directory contents
| |  |  permissions for world
| |  permissions for members of group
| permissions for owner of file: r = read, w = write, x = execute -=no permission
type of file: - = normal file, d=directory, l = symbolic link, and others...

ls -a       List the current directory including hidden files. Hidden files start with "." 
ls -ld *    List all the file and directory names in the current directory. Without the "d" 
        option, ls would list the contents of any sub-directory of the current. With the "d"
        option, ls just lists them like regular files. 

更改权限

chmod 755 file

必须是 file 的所有者或者 root 用户才能执行此操作。

显示和编辑文件

cat filename      以 ascii 编码打印文件内容到屏幕. 
more filename     同 cat 但会分屏: ENTER = one line down 
                  SPACEBAR = page down  q=quit
less filename     和 more 命令一样, 但可以用  PageUp 向上滚动. Not on all systems. 
vi filename       Edit a file using the vi editor. All UNIX systems will have vi in some form. 

指令重定向

>>> 可以将输出重定向到一个文件中,主要的区别有以下几种:

Pipe

| 将前一个命令的输出作为下一个命令的输入。

命令替换

pipe 类似,只是执行顺序反向。

cat `find . -name aaa.txt`

此命令会将当前目录和子目录下的所有名为 aaa.txt 的文件内容打印到屏幕上。

查找文件 find

find search_path -name filename 

find . -name aaa.txt    找出当前和子目录下名为 aaa.txt 的文件. 
find . -name \*.txt     找出当前和子目录下扩展名为 txt 的文件. 
find / -name vimrc      找出系统中所有名为 vimrc 的文件. 
find /usr/local/games -name "*xpilot*"       
                        找出/usr/local/games 目录和子目录下名字包含有 xpilot 的文件. 

查找文件内容 grep

grep string filename    打印出文件中包含 ‘string’ 的所有行内容

grep -v string filename 打印出文件中 **不** 包含 ‘string’ 的所有行内容

获取文件对应行的内容 head / tail


head -1 filename        打印出 filename 的第一行内容

tail -1 filename        打印出 filename 的最后一行内容

更改文件中的内容 sed

sed -e 's/find/replace/g' file1 > file2   将 file1 中 find 替换为 replace 并且将替换后的内容写入到 file2 (file1内容不变)

sed -e 's:/group/boehnke::g' file1 > file2   remove '/group/boehnke' from file1

排序命令 sort

sort markers > sorted.markers   根据字母顺序排序 markers 文件,并把排序后的内容输入到 sorted.markers 文件中.
sort markers | uniq > uniq.markers   同上,但是会将 markers 内容输入到 uniq.markers 并且移除掉重复内容.

磁带归档 tar

tar是tape archive(磁带归档)的缩写,最初设计用于将打包到磁带上,现在我们大都使用它来实现备份某个分区或者某些重要的目录。

tar 命令是读写归档文件(一个树形目录下的所有文件总和)的标准方式。

stuff.tar 和 staff.tar.gz 分别代表一个归档文件和一个文件被分别压缩的归档文件。

当接受到一个 tar 文件时,我们需要使用 tar 命令解包,同样的当我们需要发送一个文件夹的所有文件给他人时,我们同样需要用 tar 命令打包。

通过一个简单的文件压缩比对 gzip 和 bzip2 的压缩大小区别。
原始大小

ll test package.json
-rw-r--r--@ 1 root  staff   1.3K Dec 19 20:38 package.json

test:
total 16
-rw-r--r--@ 1 root  staff   6.0K Dec 19 20:21 agents.js

直接打包

tar cvf bundle.tar test package.json
ll bundle.tar
-rw-r--r--  1 root  staff    14K Dec 23 01:04 bundle.tar

用 gzip 压缩

tar cvzf bundle.tar.gz test package.json
ll bundle.tar.gz
-rw-r--r--  1 root  staff   1.7K Dec 23 01:07 bundle.tar.gz

用 bzip2 压缩

tar cvjf bundle.bz2 test package.json
ll bundle.bz2
-rw-r--r--  1 root  staff   2.0K Dec 23 01:09 bundle.bz2

压缩命令 compress/gzip/bzip2

以上3个命令会将文件直接压缩,并更改后缀名,后缀名分别对应 .Z/.gz/.bz2

gzip 是适用范围最广的,并且 gzip 的压缩率高于 compress 命令,缺点是不是所有系统都预装了 gzip 命令。

bzip2 命令的在(大多数情况下)压缩率要优于 gzip 但是压缩解压的时间也要多余 gzip 命令。

我们用一个原始大小是 1.3K 的文件对比各个压缩比。
首先是 compress

ll package.json
-rw-r--r--  1 root  staff   1.3K Dec 19 20:38 package.json

compress package.json

ll package.json.Z
-rw-r--r--  1 root  staff   738B Dec 19 20:38 package.json.Z

接着是 gzip

uncompress package.json.Z && gzip package.json

ll package.json.gz
-rw-r--r--  1 root  staff   568B Dec 19 20:38 package.json.gz

最后是 bzip2

gunzip package.json.gz && bzip2 package.json

ll package.json.bz2
-rw-r--r--  1 root  staff   582B Dec 19 20:38 package.json.bz2

(bzip2 还要大一点点?)

vim 简单上手

略 #67