前幾篇介紹了 Linux 的檔案系統,了解了檔案系統後接著就要知道如何管理硬碟了。
df 指令用於查看以掛載的硬碟空間占用情況,例如 : 總容量、使用量、剩餘容量等等。
df [option] [file]
option :
/proc
等檔案系統。file 可以指定要列出哪個目錄下的硬碟占用情況,若不指定則為當前目錄。
範例
列出所有的檔案系統,可以看到 /dev/sda1
即為硬碟。dev 即為 device,存放了外部系統的文件,詳情可以參考 Linux (二) - 檔案系統架構
此外若是沒有指定容量的顯示格式,預設使用 KB,所以可以看到沒有指定容量和指定 -k
參數的輸出結果是一樣的。
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
overlay 61255652 14209244 43905076 25% /
tmpfs 65536 0 65536 0% /dev
tmpfs 1651424 0 1651424 0% /sys/fs/cgroup
shm 65536 0 65536 0% /dev/shm
/dev/sda1 61255652 14209244 43905076 25% /etc/hosts
tmpfs 1651424 0 1651424 0% /proc/acpi
tmpfs 1651424 0 1651424 0% /sys/firmware
$ df -k
Filesystem 1K-blocks Used Available Use% Mounted on
overlay 61255652 14209244 43905076 25% /
tmpfs 65536 0 65536 0% /dev
tmpfs 1651424 0 1651424 0% /sys/fs/cgroup
shm 65536 0 65536 0% /dev/shm
/dev/sda1 61255652 14209244 43905076 25% /etc/hosts
tmpfs 1651424 0 1651424 0% /proc/acpi
tmpfs 1651424 0 1651424 0% /sys/firmware
加上 -a
參數後會列出更完整的的檔案系統,包含系統持有的 /proc
等檔案系統。如下 :
$ df -a
Filesystem 1K-blocks Used Available Use% Mounted on
overlay 61255652 14209244 43905076 25% /
proc 0 0 0 - /proc
tmpfs 65536 0 65536 0% /dev
devpts 0 0 0 - /dev/pts
sysfs 0 0 0 - /sys
tmpfs 1651424 0 1651424 0% /sys/fs/cgroup
cpuset 0 0 0 - /sys/fs/cgroup/cpuset
cpu 0 0 0 - /sys/fs/cgroup/cpu
cpuacct 0 0 0 - /sys/fs/cgroup/cpuacct
blkio 0 0 0 - /sys/fs/cgroup/blkio
memory 0 0 0 - /sys/fs/cgroup/memory
devices 0 0 0 - /sys/fs/cgroup/devices
freezer 0 0 0 - /sys/fs/cgroup/freezer
net_cls 0 0 0 - /sys/fs/cgroup/net_cls
perf_event 0 0 0 - /sys/fs/cgroup/perf_event
net_prio 0 0 0 - /sys/fs/cgroup/net_prio
hugetlb 0 0 0 - /sys/fs/cgroup/hugetlb
pids 0 0 0 - /sys/fs/cgroup/pids
rdma 0 0 0 - /sys/fs/cgroup/rdma
cgroup 0 0 0 - /sys/fs/cgroup/systemd
mqueue 0 0 0 - /dev/mqueue
shm 65536 0 65536 0% /dev/shm
/dev/sda1 61255652 14209244 43905076 25% /etc/resolv.conf
/dev/sda1 61255652 14209244 43905076 25% /etc/hostname
/dev/sda1 61255652 14209244 43905076 25% /etc/hosts
devpts 0 0 0 - /dev/console
proc 0 0 0 - /proc/bus
proc 0 0 0 - /proc/fs
proc 0 0 0 - /proc/irq
proc 0 0 0 - /proc/sys
proc 0 0 0 - /proc/sysrq-trigger
tmpfs 1651424 0 1651424 0% /proc/acpi
tmpfs 65536 0 65536 0% /proc/kcore
tmpfs 65536 0 65536 0% /proc/keys
tmpfs 65536 0 65536 0% /proc/timer_list
tmpfs 65536 0 65536 0% /proc/sched_debug
tmpfs 1651424 0 1651424 0% /sys/firmware
加上 -m
參數指定以 MB 的格式顯示容量。
$ df -m
Filesystem 1M-blocks Used Available Use% Mounted on
overlay 59820 13877 42877 25% /
tmpfs 64 0 64 0% /dev
tmpfs 1613 0 1613 0% /sys/fs/cgroup
shm 64 0 64 0% /dev/shm
/dev/sda1 59820 13877 42877 25% /etc/hosts
tmpfs 1613 0 1613 0% /proc/acpi
tmpfs 1613 0 1613 0% /sys/firmware
加上 -h
參數讓系統自動決定容量顯示的格式,可以看到有 GB,也有 MB。再看到如果是加上 -H
參數可以看到 Size 略有不同,因為 -h
是用 1024
進位,而 -H
是以 1000
進位。
$ df -h
Filesystem Size Used Avail Use% Mounted on
overlay 59G 14G 42G 25% /
tmpfs 64M 0 64M 0% /dev
tmpfs 1.6G 0 1.6G 0% /sys/fs/cgroup
shm 64M 0 64M 0% /dev/shm
/dev/sda1 59G 14G 42G 25% /etc/hosts
tmpfs 1.6G 0 1.6G 0% /proc/acpi
tmpfs 1.6G 0 1.6G 0% /sys/firmware
$ df -H
Filesystem Size Used Avail Use% Mounted on
overlay 63G 15G 45G 25% /
tmpfs 68M 0 68M 0% /dev
tmpfs 1.7G 0 1.7G 0% /sys/fs/cgroup
shm 68M 0 68M 0% /dev/shm
/dev/sda1 63G 15G 45G 25% /etc/hosts
tmpfs 1.7G 0 1.7G 0% /proc/acpi
tmpfs 1.7G 0 1.7G 0% /sys/firmware
這裡再指定要列出 etc
這個目錄下的硬碟使用情況。
$ df -h etc
Filesystem Size Used Avail Use% Mounted on
overlay 59G 14G 42G 25% /
加上 -T
會顯示出檔案系統的類型,例如下方的範例,tmpfs 是 Ram disk,ext4 (fourth extended filesystem) 是硬碟的格式。
$ df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
overlay overlay 61255652 14209244 43905076 25% /
tmpfs tmpfs 65536 0 65536 0% /dev
tmpfs tmpfs 1651424 0 1651424 0% /sys/fs/cgroup
shm tmpfs 65536 0 65536 0% /dev/shm
/dev/sda1 ext4 61255652 14209244 43905076 25% /etc/hosts
tmpfs tmpfs 1651424 0 1651424 0% /proc/acpi
tmpfs tmpfs 1651424 0 1651424 0% /sys/firmware
加上 -i
參數會顯示出 inode 的使用數量。
$ df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
overlay 3907584 440219 3467365 12% /
tmpfs 412856 17 412839 1% /dev
tmpfs 412856 15 412841 1% /sys/fs/cgroup
shm 412856 1 412855 1% /dev/shm
/dev/sda1 3907584 440219 3467365 12% /etc/hosts
tmpfs 412856 1 412855 1% /proc/acpi
tmpfs 412856 1 412855 1% /sys/firmware
du 會列出當前目錄下每個目錄的容量大小。
du [option] [file]
option :
範例
下面這個範例可以看到沒有加任何參數的情況會列出當前目錄下每個目錄的容量大小,前面顯示的就是容量大小,預設是 KB,所以會和加了 -k
參數的結果相同。
$ du
4 ./testDir2/testDir1/testDir2
4 ./testDir2/testDir1/testDir3
12 ./testDir2/testDir1
16 ./testDir2
4 ./mvDirTest2/mvDirTest1
8 ./mvDirTest2
20 ./tony
4 ./testDir4/testDir1/testDir2
4 ./testDir4/testDir1/testDir3
12 ./testDir4/testDir1
16 ./testDir4
4 ./testDir3/testDir1/testDir2
4 ./testDir3/testDir1/testDir3
12 ./testDir3/testDir1
16 ./testDir3
4 ./test/test2
8 ./test
108 .
$ du -k
4 ./testDir2/testDir1/testDir2
4 ./testDir2/testDir1/testDir3
12 ./testDir2/testDir1
16 ./testDir2
4 ./mvDirTest2/mvDirTest1
8 ./mvDirTest2
20 ./tony
4 ./testDir4/testDir1/testDir2
4 ./testDir4/testDir1/testDir3
12 ./testDir4/testDir1
16 ./testDir4
4 ./testDir3/testDir1/testDir2
4 ./testDir3/testDir1/testDir3
12 ./testDir3/testDir1
16 ./testDir3
4 ./test/test2
8 ./test
108 .
加上 -a
參數後可以看到列出了更詳細的目錄大小,連檔案也都列出來了。
$ du -a
0 ./t2
4 ./testDir2/testDir1/testDir2
0 ./testDir2/testDir1/test2.txt
0 ./testDir2/testDir1/testDir3/test3.txt
4 ./testDir2/testDir1/testDir3
0 ./testDir2/testDir1/test1.txt
12 ./testDir2/testDir1
16 ./testDir2
0 ./mvDirTest2/mvDirTest1/testmv
4 ./mvDirTest2/mvDirTest1
8 ./mvDirTest2
4 ./textfile3
4 ./tony/.bash_logout
4 ./tony/.bashrc
4 ./tony/.bash_history
4 ./tony/.bash_profile
20 ./tony
0 ./testDir4/testDir1/testDir2/test1.txt
4 ./testDir4/testDir1/testDir2
0 ./testDir4/testDir1/test2.txt
0 ./testDir4/testDir1/testDir3/test3.txt
4 ./testDir4/testDir1/testDir3
0 ./testDir4/testDir1/test1.txt
12 ./testDir4/testDir1
16 ./testDir4
4 ./textfile1
4 ./testDir3/testDir1/testDir2
0 ./testDir3/testDir1/test2.txt
0 ./testDir3/testDir1/testDir3/test3.txt
4 ./testDir3/testDir1/testDir3
0 ./testDir3/testDir1/test1.txt
12 ./testDir3/testDir1
16 ./testDir3
4 ./testcat
4 ./test/test2
8 ./test
0 ./testmv21
0 ./testmv3
0 ./t1.txt
4 ./file1
4 ./textfile2
108 .
加上 -h
參數可以看到因為容量大小都很小所以還是選擇以 KB 格式顯示,不過比較不一樣的是多了 K
的單位顯示。
$ du -h
4.0K ./testDir2/testDir1/testDir2
4.0K ./testDir2/testDir1/testDir3
12K ./testDir2/testDir1
16K ./testDir2
4.0K ./mvDirTest2/mvDirTest1
8.0K ./mvDirTest2
20K ./tony
4.0K ./testDir4/testDir1/testDir2
4.0K ./testDir4/testDir1/testDir3
12K ./testDir4/testDir1
16K ./testDir4
4.0K ./testDir3/testDir1/testDir2
4.0K ./testDir3/testDir1/testDir3
12K ./testDir3/testDir1
16K ./testDir3
4.0K ./test/test2
8.0K ./test
108K .
加上 -s
參數後可以看到僅列出了一行,.
也就代表當前目錄下的總容量大小。
$ du -s
108 .
加上 -S
參數後可以看到在當前目錄 (.
) 下的總容量是 24
,跟 -s
參數列出的 108
不同,因為 -s
會巢狀的計算出整個目錄下的容量,而 -S
只有計算當前目錄下這一層的容量。
$ du -S
4 ./testDir2/testDir1/testDir2
4 ./testDir2/testDir1/testDir3
4 ./testDir2/testDir1
4 ./testDir2
4 ./mvDirTest2/mvDirTest1
4 ./mvDirTest2
20 ./tony
4 ./testDir4/testDir1/testDir2
4 ./testDir4/testDir1/testDir3
4 ./testDir4/testDir1
4 ./testDir4
4 ./testDir3/testDir1/testDir2
4 ./testDir3/testDir1/testDir3
4 ./testDir3/testDir1
4 ./testDir3
4 ./test/test2
4 ./test
24 .
加上 -m
參數後因為目錄下的容量都不到 MB,所以為了顯示出有容量被使用就都顯示為 1
。
$ du -m
1 ./testDir2/testDir1/testDir2
1 ./testDir2/testDir1/testDir3
1 ./testDir2/testDir1
1 ./testDir2
1 ./mvDirTest2/mvDirTest1
1 ./mvDirTest2
1 ./tony
1 ./testDir4/testDir1/testDir2
1 ./testDir4/testDir1/testDir3
1 ./testDir4/testDir1
1 ./testDir4
1 ./testDir3/testDir1/testDir2
1 ./testDir3/testDir1/testDir3
1 ./testDir3/testDir1
1 ./testDir3
1 ./test/test2
1 ./test
1 .
本篇介紹了幾個基本的指令來查看硬碟的相關資訊,透過 df
和 du
指令就可以來查看目前硬碟的狀況。
[1] Linux 硬碟管理
[2] Linux 硬碟與硬體管裡
Linux
DFS&BFSfrom collections import deque
Sep 10, 2023Heroku offers a student program in partnership with GitHub. Students can access Heroku's resources by getting the GitHub Studnet Developer Pack. It provides a credit of $13 USD per month for 12 months. Please follow the instructions below step by step to enable it. 1. GitHub Student Developer Pack Click here to apply the GitHub Studnet Developer Pack from GitHub Education. An email account from the school, i.e., with .edu, is required to verify your student identity. 2. Heroku for GitHub Students Offer Register a Heroku account before enrolling in the Heroku Students Offer. After logging into Heroku, please click here to apply for the Studnets Offer.
May 4, 2023act is a tool to run github action locally. By using act, we can get feedback from CI faster. No need to commit/push any changes to github for checking your code is fulfilling the CI requirements or not. Installation MacOS Using homebrew to install act brew install act Install Docker
Dec 5, 2022電腦是由硬體和軟體所構成的,而主要負責運算的部分是作業系統的核心 - Kernel。當使用者下了命令之後,Kernel 就會接收這個命令並且再交由 CPU 進行處理。那 Kernel 是如何接收這些命令的呢 ? 可以看到下面這張圖,使用者和 Kernel 會靠著 Shell 作為一個使用者的介面來進行溝通,也就是說使用者下達了命令後,Shell 會將這些命令轉成 Kernel 可以理解的程式碼,再傳送給 Kernel 好讓 Kernel 可以正確地控制硬體工作。  BASH (Bourne Again SHell) BASH 是 Linux 預設的 Shell,雖然 Shell 有很多種,但是 BASH 會被作為 Linux 預設的 Shell 是因為他的功能非常強大,下面列出幾個主要的優點 : 命令紀錄 (history)
Jul 3, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up