17个有用的CLI命令,作为前端工程师,你需要知道一下

Tree

它在显示文件之间的目录关系方面做得很好,这真的很酷。

commands
├── a.js
├── b.js
├── c.js
├── copy-apps
│   └── fe-apps
│       └── a.js
├── fe-apps
│   └── a.js
├── test.log
└── xxx
    └── yyy

在此之前,您需要安装命令树。

brew install tree

然后只需在文件目录中执行tree即可。

wc

wc 是 word count 的缩写,常用于文件统计。它可以统计字数、行数、字符数、字节数等。

我经常用它来计算文件中的代码行数。

du

打印出一个目录的文件大小信息。我们使用它的频率较低,但它是一个非常值得学习的命令。

  • du -h:打印出适合人类阅读的信息。
  • du -a:列出目录中文件大小的信息;
  • du -s:只显示总大小,不显示具体信息。
➜  commands git:(master) ✗ du
0  ./xxx/yyy
0  ./xxx
0  ./fe-apps
0  ./copy-apps/fe-apps
0  ./copy-apps
0  ./.git/objects/pack
0  ./.git/objects/info
0  ./.git/objects
8  ./.git/info
104  ./.git/hooks
0  ./.git/refs/heads
0  ./.git/refs/tags
0  ./.git/refs
136  ./.git
168  .

➜  commands git:(master) ✗ du -h
  0B  ./xxx/yyy
  0B  ./xxx
  0B  ./fe-apps
  0B  ./copy-apps/fe-apps
  0B  ./copy-apps
  0B  ./.git/objects/pack
  0B  ./.git/objects/info
  0B  ./.git/objects
4.0K  ./.git/info
 52K  ./.git/hooks
  0B  ./.git/refs/heads
  0B  ./.git/refs/tags
  0B  ./.git/refs
 68K  ./.git
 84K  .
➜  commands git:(master) ✗ du -ha
4.0K  ./a.js
  0B  ./xxx/yyy
  0B  ./xxx
  0B  ./fe-apps/a.js
  0B  ./fe-apps
4.0K  ./test.log
  0B  ./copy-apps/fe-apps/a.js
  0B  ./copy-apps/fe-apps
  0B  ./copy-apps
4.0K  ./c.js
4.0K  ./.git/config
  0B  ./.git/objects/pack
  0B  ./.git/objects/info
  0B  ./.git/objects
4.0K  ./.git/HEAD
4.0K  ./.git/info/exclude
4.0K  ./.git/info
4.0K  ./.git/description
4.0K  ./.git/hooks/commit-msg.sample
8.0K  ./.git/hooks/pre-rebase.sample
4.0K  ./.git/hooks/pre-commit.sample
4.0K  ./.git/hooks/applypatch-msg.sample
4.0K  ./.git/hooks/fsmonitor-watchman.sample
4.0K  ./.git/hooks/pre-receive.sample
4.0K  ./.git/hooks/prepare-commit-msg.sample
4.0K  ./.git/hooks/post-update.sample
4.0K  ./.git/hooks/pre-merge-commit.sample
4.0K  ./.git/hooks/pre-applypatch.sample
4.0K  ./.git/hooks/pre-push.sample
4.0K  ./.git/hooks/update.sample
 52K  ./.git/hooks
  0B  ./.git/refs/heads
  0B  ./.git/refs/tags
  0B  ./.git/refs
 68K  ./.git
4.0K  ./b.js
 84K  .
du -sh

alias

alias命令用于设置命令的别名。如果您仅键入别名,将列出所有当前别名设置。

让我们尝试为 git status 设置一个别名

alias gs="git status"

值得注意的是:如果你想让gs命令永久存在,你应该在.profile或.zshrc中设置它。

grep

我们经常需要查找服务器上日志文件的内容,grep将是我们得心应手的帮手。

有一个日志文件test.log。它包含以下内容:

const a = 1
const b = 2
const c = 3

console.log(a + b + c)

如何突出显示包含 a 字符的位置?这很容易,不是吗?

grep a test.log

cat

cat 的主要用途是查看文件的内容并将其打印在屏幕上。

但它至少还有一些其他用途。

1.清除a.js的内容

➜  commands git:(master) ✗ cat a.js // There are two lines of code in a.js
const a = 'fatfish'

console.log(a)%
➜  commands git:(master) ✗ cat /dev/null > a.js // clear the contents of a.js
➜  commands git:(master) ✗ cat a.js // The content in a.js is cleared.
➜  commands git:(master) ✗

2.将a.js的内容复制到b.js

➜  commands git:(master) ✗ cat a.js
const name = 'fatfish'
console.log(name)
➜  commands git:(master) ✗ cat b.js // No content in b.js
➜  commands git:(master) ✗ cat a.js > b.js // Copy the contents of a.js to b.js
➜  commands git:(master) ✗ cat b.js // The content in b.js is the same as in a.js
const name = 'fatfish'
console.log(name)
➜  commands git:(master) ✗ cat a.js
const name = 'fatfish'
console.log(name)

3.将a.js的内容添加到c.js的最后一个字符


➜  commands git:(master) ✗ cat a.js
const name = 'fatfish'
console.log(name)%
➜  commands git:(master) ✗ cat c.js
const age = 100
console.log(age)
➜  commands git:(master) ✗ cat a.js >> c.js
➜  commands git:(master) ✗ cat c.js
const age = 100
console.log(age)const name = 'fatfish'
console.log(name)