Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

我的 Linux 常用命令 #5

Open
SuperTapir opened this issue Apr 5, 2020 · 0 comments
Open

我的 Linux 常用命令 #5

SuperTapir opened this issue Apr 5, 2020 · 0 comments
Labels

Comments

@SuperTapir
Copy link
Owner

SuperTapir commented Apr 5, 2020

作为一个程序员,不可避免的,要跟 Linux 打交道,而跟 Linux 很多场景下最高效的方式就是命令行了,这里总结了我日常中比较常用的,以便今后查找

操作文件和目录

符号链接

符号链接类似 Windows 中的快捷方式,通过利用符号链接,我在项目里可以将一些程序内部的配置提到项目目录外面进行修改和管理

$ touch a.txt
$ ll
total 0
-rw-r--r--  1 kilo  staff     0B Apr  5 23:22 a.txt
$ ln -s a.txt a.link.txt
$ ll
total 0
lrwxr-xr-x  1 kilo  staff     5B Apr  5 23:23 a.link.txt -> a.txt
-rw-r--r--  1 kilo  staff     0B Apr  5 23:22 a.txt

ln 命令的 -s 指的就是创建 create symbolic links instead of hard links,不加就意味着创建 硬链接。

硬连接是 指向文件的指针,而符号链接是 指向文件路径的指针

由于硬连接有诸如不能关联目录,不能关联所在文件系统之外的文件的局限性,所以一般使用符号链接而不是硬连接

重定向

不得不承认,这可能是 Linux 命令行里最酷的东西

  • >> 追加标准输出
  • 2> 重定向标准错误
  • &> 重定向标准输出和标准错误
  • 2> /dev/null 丢弃(忽略)错误输出
$ ll / ./err >> stdout.txt 2>> stderr.txt
$ ll
total 16
-rw-r--r--  1 kilo  staff    37B Apr  5 23:48 stderr.txt
-rw-r--r--  1 kilo  staff   947B Apr  5 23:48 stdout.txt

查看磁盘状态

df

列出文件系统的整体磁盘使用量

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        40G   11G   28G  28% /
devtmpfs        3.9G     0  3.9G   0% /dev
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           3.9G  612K  3.9G   1% /run
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
tmpfs           783M     0  783M   0% /run/user/1001

du

检查某目录的磁盘空间使用量,并且按照从大到小排序

$ du -s ./* | sort -nr
389928	./node_modules
560	./yarn.lock
48	./dist
24	./src
24	./build
16	./env
8	./tsconfig.json
8	./package.json
8	./index.html
8	./babel.config.js

du 的 -s 代表 only report total for each argument,也就是计算目录的总大小

sort 的 -n 代表 compare according to string numerical value,也就是通过数字排序而不是字典序。-r 代表 reverse the result of comparisons,也就是逆序

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant