Closed mrbone closed 6 years ago
[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
的所有者设置 rwx = 7 = 111(权限) 权限 file
当前所属组用户设置 xr = 5 = 101 权限必须是 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.
>
和 >>
可以将输出重定向到一个文件中,主要的区别有以下几种:
grep string filename > newfile
将 filename 文件中检索出带 string
的行内容写入到 newfile
文件中,如果该文件不存在则新建。grep string filename >! newfile
和上面命令一样,唯一区别是如果 newfile
存在则会强制复写,而上面的命令会失败(mac 下用第一条命令也直接复写了)。grep string filename >> newfile
和之前的命令一样,只是内容是 append 到 newfile 中的。|
将前一个命令的输出作为下一个命令的输入。
和 pipe
类似,只是执行顺序反向。
cat `find . -name aaa.txt`
此命令会将当前目录和子目录下的所有名为 aaa.txt
的文件内容打印到屏幕上。
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 string filename 打印出文件中包含 ‘string’ 的所有行内容
grep -v string filename 打印出文件中 **不** 包含 ‘string’ 的所有行内容
head -1 filename 打印出 filename 的第一行内容
tail -1 filename 打印出 filename 的最后一行内容
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 markers > sorted.markers 根据字母顺序排序 markers 文件,并把排序后的内容输入到 sorted.markers 文件中.
sort markers | uniq > uniq.markers 同上,但是会将 markers 内容输入到 uniq.markers 并且移除掉重复内容.
tar是tape archive(磁带归档)的缩写,最初设计用于将打包到磁带上,现在我们大都使用它来实现备份某个分区或者某些重要的目录。
tar
命令是读写归档文件(一个树形目录下的所有文件总和)的标准方式。
stuff.tar 和 staff.tar.gz 分别代表一个归档文件和一个文件被分别压缩的归档文件。
当接受到一个 tar 文件时,我们需要使用 tar
命令解包,同样的当我们需要发送一个文件夹的所有文件给他人时,我们同样需要用 tar
命令打包。
tar cvf archive.tar file1 [file2...]
tar cvf archive.tgz file1 [file2...]
tar xvf archive.tar
tar cvzf archive.tgz
tar xvjf archive.bz2
tar cvjf archive.tar file1 [file2...]
通过一个简单的文件压缩比对 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 还要大一点点?)
略 #67
Learn UNIX in 10 minutes