作为前端开发工程师,我们需要知道哪些命令?如果您熟悉这些命令,它们将大大提高您的工作效率。
1.tree 朋友们,你们知道如何像下面这样列出一个目录的文件结构吗?
它很好地显示了文件之间的目录关系,这真的很酷。
复制 commands
├── a .js
├── b .js
├── c .js
├── copy - apps
│ └── fe - apps
│ └── a .js
├── fe - apps
│ └── a .js
├── test .log
└── xxx
└── yyy
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 在此之前,您需要安装命令tree。
然后只需在文件目录中执行tree命令。
2.wc wc是word count的缩写,常用于文件统计。它可以统计字数、行数、字符数、字节数等。
我经常用它来统计文件中的代码行数。
3.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 .
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 复制 ➜ commands git :(master ) ✗ du - h
0 B ./ xxx / yyy
0 B ./ xxx
0 B ./ fe - apps
0 B ./ copy - apps / fe - apps
0 B ./ copy - apps
0 B ./ .git / objects / pack
0 B ./ .git / objects / info
0 B ./ .git / objects
4.0 K ./ .git / info
52 K ./ .git / hooks
0 B ./ .git / refs / heads
0 B ./ .git / refs / tags
0 B ./ .git / refs
68 K ./ .git
84 K .
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.
复制 ➜ commands git :(master ) ✗ du - ha
4.0 K ./ a .js
0 B ./ xxx / yyy
0 B ./ xxx
0 B ./ fe - apps / a .js
0 B ./ fe - apps
4.0 K ./ test .log
0 B ./ copy - apps / fe - apps / a .js
0 B ./ copy - apps / fe - apps
0 B ./ copy - apps
4.0 K ./ c .js
4.0 K ./ .git / config
0 B ./ .git / objects / pack
0 B ./ .git / objects / info
0 B ./ .git / objects
4.0 K ./ .git / HEAD
4.0 K ./ .git / info / exclude
4.0 K ./ .git / info
4.0 K ./ .git / description
4.0 K ./ .git / hooks / commit - msg .sample
8.0 K ./ .git / hooks / pre - rebase .sample
4.0 K ./ .git / hooks / pre - commit .sample
4.0 K ./ .git / hooks / applypatch - msg .sample
4.0 K ./ .git / hooks / fsmonitor - watchman .sample
4.0 K ./ .git / hooks / pre - receive .sample
4.0 K ./ .git / hooks / prepare - commit - msg .sample
4.0 K ./ .git / hooks / post - update .sample
4.0 K ./ .git / hooks / pre - merge - commit .sample
4.0 K ./ .git / hooks / pre - applypatch .sample
4.0 K ./ .git / hooks / pre - push .sample
4.0 K ./ .git / hooks / update .sample
52 K ./ .git / hooks
0 B ./ .git / refs / heads
0 B ./ .git / refs / tags
0 B ./ .git / refs
68 K ./ .git
4.0 K ./ b .js
84 K .
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38.
4. alias alias 命令用于设置命令的别名。如果您只键入 alias,将列出所有当前的别名设置。
让我们尝试为 git status 设置一个别名
值得注意的是:如果你希望 gs 命令是永久的,你应该在 .profile 或 .zshrc 中设置它。
5. grep 我们经常需要查找服务器上日志文件的内容,grep 将是我们得心应手的帮手。
有一个日志文件test.log。它包含以下内容:
复制 const a = 1
const b = 2
const c = 3
console .log (a + b + c )
如何突出显示包含 a 字符的位置?这很容易,不是吗?
6.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 )
7. clear 有时,我们需要在终端中进行一些操作,以至于屏幕上的内容足以让我们感到厌烦。
如何清除它们?我们需要逐行删除它们吗?
8.cp cp 命令用于复制文件或目录。
cp -f:当要复制的文件覆盖已有的目标文件时,不会有提示信息。
cp -r:如果复制的文件是目录文件,则复制该目录下的所有子目录和文件。
复制 ➜ commands git :(master ) ✗ ls - R
a .js b .js copy - apps fe - apps
./ copy - apps :
./ fe - apps :
// 1. copy a file
➜ commands git :(master ) ✗ cp a .js fe - apps
➜ commands git :(master ) ✗ ls - R
a .js b .js copy - apps fe - apps
./ copy - apps :
./ fe - apps :
a .js
➜ commands git :(master ) ✗ cp fe - apps copy - apps
cp : fe - apps is a directory (not copied ).
// 2. copy a directory
➜ commands git :(master ) ✗ cp - rf fe - apps copy - apps
➜ commands git :(master ) ✗ ls - R
a .js b .js copy - apps fe - apps
./ copy - apps :
fe - apps
./ copy - apps / fe - apps :
a .js
./ fe - apps :
a .js
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23.
9. cd 这篇文章一定是没有技术含量的,因为cd真的没什么好写的,作为开发者,谁不熟悉呢?
也许你是对的,但我只是想说 cd - 可以回到你上次访问的目录。我认为这是一个好技巧。
10. ls 这是一个使用频率很高的命令,用来显示文件目录的内容列表。
它可以至少以 3 种方式使用。
ls -a:显示所有文件和目录(包括以.开头的目录) ls -A:显示所有文件和目录(不包括以.目录开头的目录) ls -R:显示所有文件和目录,如果目录中有文件,则按顺序列出
11. rm 它用于删除文件或目录。
rm -i: 将目录下的文件一个一个删除,删除前会询问是否删除文件。
rm -r:将指定目录及其子目录下的所有文件一起处理(注意:不删除文件。)
rm -f:用于强制删除文件或目录。
12.tail
我想你一定也有在服务器上查看日志内容的经历,tail绝对是个好帮手。
tail -f filename 会在屏幕上显示filename尾部的内容,当它的内容发生变化时,你会在屏幕上看到最新的内容。
13.MV 有时我们想更改文件或目录的名称,或者将其移动到另一个地方,这时我们可以使用 mv 命令。
1.修改文件名
复制 ➜ commands git :(master ) ✗ ls
a .js
➜ commands git :(master ) ✗ mv a .js xxx .js
➜ commands git :(master ) ✗ ls
xxx .js
➜ commands git :(master ) ✗
2.将文件移动到其他目录
复制 ➜ commands git :(master ) ✗ ls - R
a .js fe - apps
./ fe - apps :
xxx .js
➜ commands git :(master ) ✗ mv a .js fe - apps
➜ commands git :(master ) ✗ ls - R
fe - apps
./ fe - apps :
a .js xxx .js
14.touch 我经常使用 touch 命令创建一个新文件,尽管它用于修改文件或目录的时间属性。
15.which 如果要查看命令的具体路径,可以使用 which。
复制 ➜ commands git :(master ) ✗ which node
/ Users / dz0400229 / .nvm / versions / node / v16 .0 .0 / bin / node
➜ commands git :(master ) ✗ which npm
/ Users / dz0400229 / .nvm / versions / node / v16 .0 .0 / bin / npm
➜ commands git :(master ) ✗ which npx
/ Users / dz0400229 / .nvm / versions / node / v16 .0 .0 / bin / npx
➜ commands git :(master ) ✗
16. mkdir 是的,你以前肯定用过这个命令,没什么好说的!
但是mkdir -p dirname 真的是我们很少用到的东西,它有什么用呢?
复制 ➜ commands git :(master ) ✗ ls
a .js b .js copy - apps fe - apps
➜ commands git :(master ) ✗ mkdir xxx / yyy // You cannot create the yyy directory because the xxx directory does not exist
mkdir : xxx : No such file or directory
➜ commands git :(master ) ✗ mkdir - p xxx / yyy // `-p` will check if the xxx directory already exists, and create it if it doesn't.
➜ commands git :(master ) ✗ ls
a .js b .js copy - apps fe - apps xxx
➜ commands git :(master ) ✗ ls - R
a .js b .js copy - apps fe - apps xxx
./ copy - apps :
fe - apps
./ copy - apps / fe - apps :
a .js
./ fe - apps :
a .js
./ xxx :
yyy
./ xxx / yyy :
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.
17.whoami 显示用户名。
复制 ➜ commands git :(master ) ✗ whoami
dz0400229
总结 到这里,我想与您分享的17关于CLI的实用命令就结束了