分享 提高生产力的 10 个终端命令

xiaoronglv · June 25, 2014 · Last by gihnius replied at July 02, 2014 · 5905 hits

以下是上周末翻译的一片文章。

用命令行的人有福了,因为终端是他们的;
用命令的人有福了,因为他们必脱离鼠标。

以下是终端中常用的几个命令行,顺手整理了一下,美其名曰「提高生产力的十个终端命令」,希望对同学们有用。

1 Man

对某条命令不熟怎么办?
忘记参数怎么办?

使用 man 可以非常方便查看每条命令的文档。

$ man touch

Man

2 Touch

touch 本用于修改文件的 access 和 modification 时间,但程序猿们都喜欢用 touch 命令去创建一个空的文件。

$ touch shared/production.log $ touch one.txt two.txt three.txt

Touch

假如你想创建并编辑一个新文件,别用 touch,建议使用其他命令:

# textmate $ mate index.html

# vim $ mvim index.html

# sublime text $ subl index.html

3 Cat && Less

cat 和 less 都可在终端直接输出文件内容。我特别喜欢用来查看密钥,阅读只有十几行的小文本文件。

$ cat shop_list.rb $ cat readme.md

cat

但 cat 会输出文件所有的内容,假如你要查看的是服务器的日志,成百上万条记录瞬间刷爆你的屏幕,你就死定了!

此时建议你用 less 命令

$ less production.log

他只会给你返回一屏的信息,你可以通过 jkbf 等快捷键来查阅该文件。

4 Tail

动态显示某个文件的最后几行,用来看日志是最合适不过的了。

$ tail production.log

tail

5 Curl

curl 最常见的应用场景有这么几个:

  1. 查看网页源码

    curl www.sina.com

    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

    301 Moved Permanently

    Moved Permanently

    The document has moved here.

  2. 加上 -o 参数可以下载文件

    curl -o [文件名] www.sina.com

  3. 显示 HTTP response 的 head 信息

  4. 提交表单数据。

  5. ....

curl 的用途太多,我就不一一赘述了,它是 web 程序员必备命令,

阮一峰在《curl 网站开发指南》中对该命令有更详尽的介绍,大家可以自行翻阅。

6 Gzip & Gunzip

在 Windows 中,压缩和打包归一个软件负责,不要用这种思维方式来理解 Linux 的压缩程序。

gzip 仅仅用于压缩文件,它才不管打包呢。Unix 的哲学是 Do one thing well

# 压缩文本文件 => hello.txt.gz $ gzip hello.txt

# 压缩 tar 包 => hello.tar.gz $ gzip hello.tar.gz

# 解压缩 => hello.txt $ gunzip hello.txt.gz

# 解压缩 => hello.tar $ gunzip hello.tar.gz

7 Tar

Tar 命令用于打包/解包

想把 one.txt,two.txt 打包为一个文件?

# -c : create, 打包(与之相对的是 -x,解包) # -f : 打包后的文件名 $ tar cf archive.tar one.txt two.txt

想打包一个文件夹?

# -c : create, 打包 # -z : 打包后顺便用 gzip 压缩 # -p : 保存相应的权限信息 # -v : 把文件按打包顺序一一列出来 # -f : 打包后的文件名 $ tar czpvf archive.tar.gz document

gzip 命令和 tar 是截然不同的。

what's the difference between tar and gzip

8 History

可以通过 history 拉出来所有敲过的命令的历史记录。

$ history

History

9 Chmod

chmod (change mode) 用于修改文件的权限。

$ chmod 644 authorized_keys

对权限系统不甚了解的同学可以去拜读 Rei 的大作《Linux 用户权限》。

Read More

  1. 10 Terminal Commands That Will Boost Your Productivity

  2. curl 网站开发指南

  3. Rei: Linux 用户权限

就想到九个,还差一个命令,怎么办?

1 Floor has deleted

…………这些都是基本操作吧。。。

:thumbsup:

#1 楼 @winnie

我刚才试了一下,电脑变得飞快,今天下午可以不用上班了。 😄

@xiaoronglv 哈哈,我删除掉了,等下真的有人没睡醒就出大问题了。

怎能少了grep,用 ruby 的好多都不喜欢用 IDE 吧,那如何再一个项目中快速查找去某一个变量名称在项目里哪儿出现过呢?比如要查找signed_in?函数,直接

grep 'signed_in?' * -R

既然说到搜索代码了,那我就来推荐下 the silver searcher: https://github.com/ggreer/the_silver_searcher

ag -Q 'signed_in?'

详情可参见一年前的这个帖子:https://ruby-china.org/topics/8728


最近新出了一个 The Platinum Searcher: https://github.com/monochromegane/the_platinum_searcher

#2 楼 @winnie rm rf / #make computer faster 😄

说好的第 10 个呢?

我觉得第 10 应该是 zsh 的插件 z,绝对是 lofe saver

lofe→life

提到 tail 命令,还说用来看日志,却不说最好用的tail -f

我最长用的:感叹号 + 命令前几个字母+tab. 还可以配合 history 使用,例如: history|grep rake 9970 rake db:migrate 9972 rake db:migrate

然后!+9970+tab.

这跟“生产力”有半毛钱关系!?

You need to Sign in before reply, if you don't have an account, please Sign up first.