# Linux (五) - 硬碟管理 前幾篇介紹了 Linux 的檔案系統,了解了檔案系統後接著就要知道如何管理硬碟了。 <!-- more --> ## 常用命令 ### df df 指令用於查看以掛載的硬碟空間占用情況,例如 : 總容量、使用量、剩餘容量等等。 ```bash= df [option] [file] ``` option : * -a : 列出所有的檔案系統,包含系統持有的 `/proc` 等檔案系統。 * -k : 以 KB 的格式顯示。 * -m : 以 MB 的格式顯示。 * -h : 自動以 GB, MB, KB 等格式顯示。 * -H : 以 M=1000K 取代 M=1024K。 * -T : 顯示檔案系統類型。 * -i : 以 [inode](https://www.itread01.com/content/1542281710.html) 的數量顯示。 file 可以指定要列出哪個目錄下的硬碟占用情況,若不指定則為當前目錄。 **範例** 列出所有的檔案系統,可以看到 `/dev/sda1` 即為硬碟。dev 即為 device,存放了外部系統的文件,詳情可以參考 [Linux (二) - 檔案系統架構](https://tienyulin.github.io/linux-filesystem/#dev) 此外若是沒有指定容量的顯示格式,預設使用 KB,所以可以看到沒有指定容量和指定 `-k` 參數的輸出結果是一樣的。 ```bash= $ 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` 等檔案系統。如下 : ```bash= $ 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 的格式顯示容量。 ```bash= $ 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` 進位。 ```bash= $ 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` 這個目錄下的硬碟使用情況。 ```bash= $ df -h etc Filesystem Size Used Avail Use% Mounted on overlay 59G 14G 42G 25% / ``` 加上 `-T` 會顯示出檔案系統的類型,例如下方的範例,tmpfs 是 Ram disk,ext4 (fourth extended filesystem) 是硬碟的格式。 ```bash= $ 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 的使用數量。 ```bash= $ 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 會列出當前目錄下每個目錄的容量大小。 ```bash= du [option] [file] ``` option : * -a : 列出所有檔案與目錄容量大小,預設只會有檔案大小沒有目錄大小。 * -h : 自動以 GB, MB, KB 等格式顯示。 * -s : 只列出所有目錄的總容量,不會全部檔案一一列出。 * -S : 只列出每個目錄下的檔案總容量,但是不包括目錄下的子目錄,也就是只顯示單層目錄的總容量。 * -k : 以 KB 的格式顯示。 * -m : 以 MB 的格式顯示。 **範例** 下面這個範例可以看到沒有加任何參數的情況會列出當前目錄下每個目錄的容量大小,前面顯示的就是容量大小,預設是 KB,所以會和加了 `-k` 參數的結果相同。 ```bash= $ 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` 參數後可以看到列出了更詳細的目錄大小,連檔案也都列出來了。 ```bash= $ 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` 的單位顯示。 ```bash= $ 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` 參數後可以看到僅列出了一行,`.` 也就代表當前目錄下的總容量大小。 ```bash= $ du -s 108 . ``` 加上 `-S` 參數後可以看到在當前目錄 (`.`) 下的總容量是 `24`,跟 `-s` 參數列出的 `108` 不同,因為 `-s` 會巢狀的計算出整個目錄下的容量,而 `-S` 只有計算當前目錄下這一層的容量。 ```bash= $ 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`。 ```bash= $ 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 . ``` ## Summary 本篇介紹了幾個基本的指令來查看硬碟的相關資訊,透過 `df` 和 `du` 指令就可以來查看目前硬碟的狀況。 ## 參考 [1] [Linux 硬碟管理](https://www.runoob.com/linux/linux-filesystem.html) [2] [Linux 硬碟與硬體管裡](http://linux.vbird.org/linux_basic/0230filesystem/0230filesystem.php) ###### tags: `Linux`
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.