Linux 指令筆記

本篇為 Linux 指令的筆記。

常用指令

看 Linux 相關資訊

看 Ubuntu 版本,使用 cat /etc/lsb-release

1
2
3
4
5
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.2 LTS"

看 CPU 資訊,使用 lscpucat /proc/cpuinfo

1
2
3
4
5
6
7
8
9
10
$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 46 bits physical, 48 bits virtual
...

$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
...

看 Memory 資訊,使用 free -h

1
2
3
4
$ free -h
total used free shared buff/cache available
Mem: 966Mi 215Mi 154Mi 0.0Ki 596Mi 592Mi
Swap: 0B 0B 0B

-h 是 human-readable 的意思,會將 bytes 轉換成 MiB、GiB 等

看 HDD 資訊,使用 df -h,若要看某層目錄底下的檔案大小,可使用 du -h -d 1 <dir>,其中 -h 同樣是 human-readable,而 -d 1 代表最深深度為 1 層:如此一來就不會列出 nested 很深層的目錄資訊。

1
2
3
4
5
6
7
8
9
10
11
12
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 29G 3.1G 26G 11% /
tmpfs 484M 0 484M 0% /dev/shm
...

$ sudo du -h -d 1 /
60K /root
864K /run
36M /home
...

看 Logs 最後幾行

例如看 docker-compose 的 logs,可使用

1
$ docker-compose logs | tail -n 5

-n 是 number 的意思,表示要顯示最末的幾行,也可用 --lines

圖片來源

  1. Unsplash