如何在Linux上检查磁盘分区和磁盘使用情况 |
您所在的位置:网站首页 › 如何检测硬盘是否正常使用了 › 如何在Linux上检查磁盘分区和磁盘使用情况 |
如何在Linux上检查磁盘分区和磁盘使用情况|
创建和删除分区是Linux中的常规任务。这是因为硬盘、U盘等存储设备在使用前需要以一定的方式进行结构化。例如,在安装系统或创建任何文件之前,文件系统必须存在。同样,文件系统只有在创建分区后才能存在。 磁盘分区可以定义为将磁盘划分为单个或多个逻辑区域的过程。这些分区通常称为分区。分区作为独立的部分工作,其中每个部分都充当自己的硬盘驱动器。创建分区后,磁盘将在分区表中存储有关分区位置和大小的信息。分区表。换句话说,分区表描述了硬盘驱动器分区的布局。通常,有两种分区表标准。这些都是: MBR(主引导记录):它也称为ms-dos。这种分区方案通常出现在较旧的计算机上。它只允许有四个分区,最大磁盘大小为 2 TB。 MBR 还包含一个写入驱动器初始扇区的引导加载程序。GPT(GUID分区表):现在广泛使用。与 MBR 不同,GPT 不包含引导加载程序,最多可以有128 个分区(128 位条目)。它还支持 MBR 部分以实现向后兼容性。磁盘分区至关重要,因为它有助于执行以下操作: 升级硬盘并将新硬盘合并到系统中双启动,用户可以在同一系统上拥有多个操作系统。高效的磁盘管理,每个分区都可以单独管理。使用同一系统处理不同的文件系统确保备份和安全。传统上,要对 MBR 磁盘进行分区,需要使用 fdisk 工具。然而,随着技术的进步,fdisk 被开发来理解 GPT 格式。还有其他几个工具可用于管理 Linux 上的磁盘分区。在本指南中,我们将了解在 Linux 上查看磁盘分区和磁盘使用情况的顶级工具。 1.F盘Fdisk 也称为固定磁盘,是在 Linux 系统上查看磁盘分区和磁盘使用情况时使用最广泛的工具。它可以帮助用户获取有关分区的所有详细信息,还可以用于监视、创建、删除、移动和复制 Linux 系统中的任何分区。 Fdisk 支持 GPT、MBR、Sun、SGI 和 BSD 分区表。 该工具可以通过以下方式使用: 查看所有磁盘分区要查看系统上的所有分区,请使用带有 -l 参数的命令,如下所示: sudo fdisk -l示例输出: 查看有关特定分区的详细信息您还可以通过提供分区名称来查看有关给定分区的详细信息。例如: sudo fdisk -l /dev/sda示例输出: 创建分区Fdisk 提供了许多可用于管理分区的命令。要获取所有可用命令,请指定硬盘名称并按如下所示继续操作: sudo fdisk /dev/sda示例输出: 现在就可以根据需要使用上面的命令来管理分区了。要获取所有分区表,请使用“p”,如图所示。 将打印硬盘中的分区,如上所示。 要创建新分区,请提供磁盘名称并键入“n”。在本演示中,我将使用使用 sudo fdisk -l 标识的辅助硬盘 /dev/sdb sudo fdisk /dev/sda要创建分区,请键入 n 并设置分区类型,如下所示: Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p设置分区号和扇区 Partition number (1-4, default 1): 1 First sector (2048-20971519, default 2048): Press Enter Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971519, default 20971519): Press Enter Created a new partition 1 of type 'Linux' and of size 10 GiB. Command (m for help):为了验证创建,打印分区表; Command (m for help): p Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors Disk model: QEMU HARDDISK Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x6602bb9a Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 20971519 20969472 10G 83 Linux Command (m for help):现在应用更改并退出 Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.删除分区要删除分区,请提供磁盘名称: sudo fdisk /dev/sdb使用“d”删除分区。 Command (m for help): d Selected partition 1 Partition 1 has been deleted. Command (m for help):应用更改并使用“w”退出: Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.2.G盘GDisk 是一个 DOS 命令行实用程序,其操作方式与 fdisk 类似。 GDisk 可以执行 FDisk 可以执行的所有操作,但具有 FDisk 没有的一些功能。其中一些功能包括即时格式化、提高性能、更好的分区管理等 GDisk执行的基本操作是: 查看磁盘分区用于查看给定磁盘(例如 /dev/sda)详细信息的命令是: sudo gdisk -l /dev/sda示例输出: 列出所有可用命令要获取可用命令的列表,请使用“?”,如图所示; $ sudo gdisk /dev/sda GPT fdisk (gdisk) version 1.0.5 Partition table scan: ...... Command (? for help): ? b back up GPT data to a file c change a partition's name d delete a partition i show detailed information on a partition l list known partition types n add a new partition o create a new empty GUID partition table (GPT) p print the partition table q quit without saving changes r recovery and transformation options (experts only) s sort partitions t change a partition's type code v verify disk w write table to disk and exit x extra functionality (experts only) ? print this menu Command (? for help):正如所见,这些命令类似于 FDisk 中使用的命令。 创建新的 GPT 分区表要创建新的 GPT 分区表,请提供磁盘名称。例如: sudo gdisk /dev/sdb使用“n”命令创建新分区。 Command (? for help): n Partition number (1-128, default 1): 1 First sector (34-20971486, default = 2048) or {+-}size{KMGTP}: Press Enter Last sector (2048-20971486, default = 20971486) or {+-}size{KMGTP}: Press Enter Current type is 8300 (Linux filesystem) Hex code or GUID (L to show codes, Enter = 8300): Press Enter Changed type of partition to 'Linux filesystem' Command (? for help):验证创建: Command (? for help): p Disk /dev/sdb: 20971520 sectors, 10.0 GiB Model: QEMU HARDDISK Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 6E1BBEF6-A37B-46A2-A08C-1389FADC68B8 Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 20971486 Partitions will be aligned on 2048-sector boundaries Total free space is 2014 sectors (1007.0 KiB) Number Start (sector) End (sector) Size Code Name 1 2048 20971486 10.0 GiB 8300 Linux filesystem Command (? for help):应用更改: Command (? for help): w Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): y OK; writing new GUID partition table (GPT) to /dev/sdb. The operation has completed successfully.删除分区要删除分区,请使用“d”,如图所示; $ sudo gdisk /dev/sdb GPT fdisk (gdisk) version 1.0.5 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR; using GPT. Command (? for help): d Using 1选择分区后,写入更改: Command (? for help): w inal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): y OK; writing new GUID partition table (GPT) to /dev/sdb. The operation has completed successfully.3. 分手Parted 也是显示和操作磁盘分区时广泛使用的命令。这是因为它支持多种分区表格式,例如 MBR 和 GPT。它可用于添加、调整大小、删除分区、重新组织磁盘使用以及将数据复制到分区。 常见的用例是: 列出分区执行此操作的命令是: sudo parted -l示例输出: 创建新分区打开磁盘。例如/dev/sdb sudo parted /dev/sdb示例输出: GNU Parted 3.3 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted)查看所有可用的命令: Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) help align-check TYPE N check partition N for TYPE(min|opt) alignment help [COMMAND] print general help, or help on COMMAND mklabel,mktable LABEL-TYPE create a new disklabel (partition table) mkpart PART-TYPE [FS-TYPE] START END make a partition name NUMBER NAME name partition NUMBER as NAME print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition quit exit program rescue START END rescue a lost partition near START and END resizepart NUMBER END resize partition NUMBER rm NUMBER delete partition NUMBER select DEVICE choose the device to edit disk_set FLAG STATE change the FLAG on selected device disk_toggle [FLAG] toggle the state of FLAG on selected device set NUMBER FLAG STATE change the FLAG on partition NUMBER toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER unit UNIT set the default unit to UNIT version display the version number and copyright information of GNU Parted要创建新分区,您可以使用 GPT 或 MSDOS 分区表。对于 GPT,请按如下所示进行操作: ##For GPT (parted) mklabel gpt ##For MSDOS (parted) mklabel msdos对于本指南,我将使用 MSDOS 分区表,尽管 GPT 的过程类似。 Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue? Yes/No? yes创建分区表后,我们现在将继续创建分区: (parted) mkpart如图所示继续: Partition type? primary/extended? primary File system type? [ext2]? ext4 Start? Start? 1 End? 9090开始值和结束值定义分区的大小(以 MB 为单位)。在本例中,几乎为 10GB。 要验证创建,请使用以下命令: (parted) print Model: ATA QEMU HARDDISK (scsi) Disk /dev/sdb: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 9090MB 9089MB primary ext4 lba (parted) 如何调整分区大小可以使用parted命令缩小和扩展分区。 要缩小分区,请使用以下命令。请记住,缩小分区时数据可能会丢失。 (parted) resizepart Partition number? 1 End? [9090MB]? 5000 Warning: Shrinking a partition can cause data loss, are you sure you want to continue? Yes/No? yes验证更改 Model: ATA QEMU HARDDISK (scsi) Disk /dev/sdb: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 5000MB 4999MB primary (parted)扩展分区: (parted) resizepart Partition number? 1 End? [5000MB]? 9080核实 : (parted) print Model: ATA QEMU HARDDISK (scsi) Disk /dev/sdb: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 9080MB 9079MB primary (parted)删除分区要删除分区,请使用 rm 命令和分区号。例如: (parted) rm Partition number? 1 (parted)核实 : (parted) print Model: ATA QEMU HARDDISK (scsi) Disk /dev/sdb: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags (parted) 4. 杜、NCDUDu 和 ncdu 是著名的跟踪磁盘使用情况的命令。 ncdu 源自NCurses Disk Usage,主要基于 du(在线工具)命令。 du 工具可以使用以下命令启动: $ du 8 ./.npm/_cacache/index-v5/22/e3 60 ./.npm/_cacache/index-v5/22 8 ./.npm/_cacache/index-v5/fa/9f 8 ./.npm/_cacache/index-v5/fa/36 8 ./.npm/_cacache/index-v5/fa/33 28 ./.npm/_cacache/index-v5/fa 8 ./.npm/_cacache/index-v5/99/7b 8 ./.npm/_cacache/index-v5/99/98 20 ./.npm/_cacache/index-v5/99 6444 ./.npm/_cacache/index-v5 39196 ./.npm/_cacache 400 ./.npm/_logs 39600 ./.npm 4 ./.gnupg/private-keys-v1.d 16 ./.gnupg 4 ./Videos 8 ./.ssh 4 ./Downloads ....您可以以更易于理解的格式获取上述信息; $ du -h 36K ./.npm/_cacache/index-v5/a0 8.0K ./.npm/_cacache/index-v5/ec/da 8.0K ./.npm/_cacache/index-v5/ec/08 8.0K ./.npm/_cacache/index-v5/ec/f5 28K ./.npm/_cacache/index-v5/ec 8.0K ./.npm/_cacache/index-v5/5e/e7 8.0K ./.npm/_cacache/index-v5/5e/5b 8.0K ./.npm/_cacache/index-v5/5e/fc 8.0K ./.npm/_cacache/index-v5/5e/d8 8.0K ./.npm/_cacache/index-v5/5e/9d 8.0K ./.npm/_cacache/index-v5/5e/8b 52K ./.npm/_cacache/index-v5/5e 8.0K ./.npm/_cacache/index-v5/a1/4f 8.0K ./.npm/_cacache/index-v5/a1/31 8.0K ./.npm/_cacache/index-v5/a1/75 8.0K ./.npm/_cacache/index-v5/a1/30 8.0K ./.npm/_cacache/index-v5/a1/3d 44K ./.npm/_cacache/index-v5/a1 8.0K ./.npm/_cacache/index-v5/ff/d3 8.0K ./.npm/_cacache/index-v5/ff/44 8.0K ./.npm/_cacache/index-v5/ff/37 8.0K ./.npm/_cacache/index-v5/ff/89 36K ./.npm/_cacache/index-v5/ff ....左侧的值表示文件使用的磁盘空间。 要列出所有文件和目录的大小,请使用命令 -a 或 --all例如: $ du -ah 44K ./frontend/src/types/actions 556K ./frontend/src/types 4.0K ./frontend/src/index.html.ejs 4.0K ./frontend/src/pages/UnAuthorized/index.tsx 8.0K ./frontend/src/pages/UnAuthorized 4.0K ./frontend/src/pages/MySettings/index.tsx 8.0K ./frontend/src/pages/MySettings 4.0K ./frontend/src/pages/MetricApplication/index.tsx 8.0K ./frontend/src/pages/MetricApplication 4.0K ./frontend/src/pages/Metrics/index.tsx 8.0K ./frontend/src/pages/Metrics 4.0K ./frontend/src/pages/TraceDetail/index.tsx 4.0K ./frontend/src/pages/TraceDetail/constants.ts 12K ./frontend/src/pages/TraceDetail 4.0K ./frontend/src/pages/GettingStarted/Section.tsx ....安装后,可以使用以下命令从终端启动 ncdu: ncdu示例输出: 在这里,您可以使用向上和向下箭头键浏览不同的目录。如果按Enter,您将进入所选目录。 要退出,请按“q”。 扫描大型目录时需要相当长的时间。例如,从根目录启动该工具,它将有许多文件需要分析。 使用-o选项,您可以避免准备报告,而是保存所有收集的数据。例如: $ sudo ncdu -o /tmp/dureport /home/ubuntu/signoz/deploy/do...pSort.proj/msgOperation.dict.mrk2 28753 files您现在可以查看该文件: $ head -11 /tmp/dureport [1,1,{"progname":"ncdu","progver":"1.14.1","timestamp":1667043990}, [{"name":"/home/ubuntu","asize":4096,"dsize":4096,"dev":2053,"ino":1446098}, [{"name":"Public","asize":4096,"dsize":4096,"ino":1705159}], [{"name":"Pictures","asize":4096,"dsize":4096,"ino":1705162}], [{"name":"Music","asize":4096,"dsize":4096,"ino":1705161}], {"name":".sudo_as_admin_successful","ino":1443137}, {"name":".bashrc","asize":3771,"dsize":4096,"ino":1450156}, {"name":".profile","asize":807,"dsize":4096,"ino":1450157}, [{"name":"sample-nodejs-app","asize":4096,"dsize":4096,"ino":1453579}, {"name":"index.js","asize":156,"dsize":4096,"ino":1454761}, {"name":"package-lock.json","asize":219289,"dsize":221184,"ino":1454762},要在使用 ncdu 时获得帮助,请使用以下命令; $ ncdu --help ncdu -h,--help This help message -q Quiet mode, refresh interval 2 seconds -v,-V,--version Print version -x Same filesystem -e Enable extended information -r Read only -o FILE Export scanned directory to FILE -f FILE Import scanned directory from FILE -0,-1,-2 UI to use when scanning (0=none,2=full ncurses) --si Use base 10 (SI) prefixes instead of base 2 --exclude PATTERN Exclude files that match PATTERN -X, --exclude-from FILE Exclude files that match any pattern in FILE -L, --follow-symlinks Follow symbolic links (excluding directories) --exclude-caches Exclude directories containing CACHEDIR.TAG --confirm-quit Confirm quitting ncdu --color SCHEME Set color scheme5. dfdf 是一个命令行实用工具,允许 Linux 用户监控其磁盘使用情况。例如,要查看磁盘使用情况摘要,请使用以下命令: $ df Filesystem 1K-blocks Used Available Use% Mounted on udev 1948344 0 1948344 0% /dev tmpfs 396924 1364 395560 1% /run /dev/sda5 40454400 15248488 23118744 40% / tmpfs 1984620 0 1984620 0% /dev/shm tmpfs 5120 0 5120 0% /run/lock tmpfs 1984620 0 1984620 0% /sys/fs/cgroup /dev/loop0 128 128 0 100% /snap/bare/5 /dev/loop2 64768 64768 0 100% /snap/core20/1623 /dev/loop1 63488 63488 0 100% /snap/core20/1518 /dev/loop3 410496 410496 0 100% /snap/gnome-3-38-2004/112 /dev/loop5 354688 354688 0 100% /snap/gnome-3-38-2004/119 /dev/loop7 48128 48128 0 100% /snap/snapd/16292 /dev/loop6 66816 66816 0 100% /snap/gtk-common-themes/1519 /dev/loop8 93952 93952 0 100% /snap/gtk-common-themes/1535 /dev/loop4 49152 49152 0 100% /snap/snapd/17336 /dev/loop9 55552 55552 0 100% /snap/snap-store/558 /dev/loop10 47104 47104 0 100% /snap/snap-store/599 /dev/sda1 523248 4 523244 1% /boot/efi tmpfs 396924 36 396888 1% /run/user/125 tmpfs 396924 8 396916 1% /run/user/1000要以人类可读的格式获取磁盘使用情况,请执行: $ df -Th Filesystem Type Size Used Avail Use% Mounted on udev devtmpfs 1.9G 0 1.9G 0% /dev tmpfs tmpfs 388M 1.4M 387M 1% /run /dev/sda5 ext4 39G 15G 23G 40% / tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/loop0 squashfs 128K 128K 0 100% /snap/bare/5 /dev/loop2 squashfs 64M 64M 0 100% /snap/core20/1623 /dev/loop1 squashfs 62M 62M 0 100% /snap/core20/1518 /dev/loop3 squashfs 401M 401M 0 100% /snap/gnome-3-38-2004/112 /dev/loop5 squashfs 347M 347M 0 100% /snap/gnome-3-38-2004/119 /dev/loop7 squashfs 47M 47M 0 100% /snap/snapd/16292 /dev/loop6 squashfs 66M 66M 0 100% /snap/gtk-common-themes/1519 /dev/loop8 squashfs 92M 92M 0 100% /snap/gtk-common-themes/1535 /dev/loop4 squashfs 48M 48M 0 100% /snap/snapd/17336 /dev/loop9 squashfs 55M 55M 0 100% /snap/snap-store/558 /dev/loop10 squashfs 46M 46M 0 100% /snap/snap-store/599 /dev/sda1 vfat 511M 4.0K 511M 1% /boot/efi tmpfs tmpfs 388M 36K 388M 1% /run/user/125 tmpfs tmpfs 388M 8.0K 388M 1% /run/user/10006. pydfpydf 是一个 Python 命令行实用程序,可替代 df 命令。它的工作方式与 df 类似,只是它用不同的颜色突出显示值 可以如图所示用PIP安装; sudo pip install pydf在 Debian/Ubuntu 上,您可以使用以下命令安装它: sudo apt install pydf -y要使用它,请发出命令: pydf -a示例输出: 使用 -h 标志获取人类可读的输出 其他可用选项可以从命令中获取 pydf --help7. 杜夫duf 工具还为您提供了一种简单的方法来检查系统上的磁盘使用情况和可用空间。它提供了比 du 和 df 更高级的用途,您可以排序、过滤或以其他方式更改显示。 该工具可以从 Github 下载并安装,如图所示。 ##On Debian/Ubuntu VER=$(curl -s https://api.github.com/repos/muesli/duf/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//') wget https://github.com/muesli/duf/releases/download/v${VER}/duf_${VER}_linux_amd64.deb sudo dpkg -i duf_*_linux_amd64.deb ##On Rhel/Rocky/Alma Linux VER=$(curl -s https://api.github.com/repos/muesli/duf/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//') wget https://github.com/muesli/duf/releases/download/v${VER}/duf_${VER}_linux_amd64.rpm sudo rpm -i duf_*_linux_amd64.rpm要使用它,请执行命令: duf示例输出: 获取更详细的信息: duf --all示例输出: 您还可以通过提供路径来限制搜索: 可以使用 –sort 选项对显示的内容进行排序。可用标志为avail、filesystem、inodes、inodes_avail、inodes_usage、inodes_used、mountpoint、size、type、usage或used 。 例如: 8. dfcdfc 是一个漂亮的工具,它使用图形和一点颜色提供了对文件系统和空间使用情况更加人性化的概述。该工具还可以直接替代 df 命令。 该工具可以从源或存储库安装。例如在 Debian/Ubuntu 上: sudo apt-get install dfc要启动该工具,请使用以下命令: dfc示例输出: 获取所有文件系统的详细概述: dfc -a示例输出: 使用文件系统的名称进行过滤: $ dfc -t ext $ dfc -t ext FILESYSTEM (=) USED FREE (-) %USED AVAILABLE TOTAL MOUNTED ON /dev/sda5 [=========-----------] 44.3% 21.5G 38.6G /要获得更多选项,请使用: $ dfc -h Usage: dfc [OPTION(S)] [-c WHEN] [-e FORMAT] [-p FSNAME] [-q SORTBY] [-t FSTYPE] [-u UNIT] Available options: -a print all mounted filesystem -b do not show the graph bar -c choose color mode. Read the manpage for details -d show used size -e export to specified format. Read the manpage for details -f disable auto-adjust mode (force display) -h print this message -i info about inodes -l only show information about locally mounted file systems -m use metric (SI unit) -M do not print "mounted on" -n do not print header -o show mount flags -p filter by file system name. Read the manpage for details -q sort the output. Read the manpage for details -s sum the total usage -t filter by file system type. Read the manpage for details -T show filesystem type -u choose the unit in which to show the values. Read the manpage for details -v print program version -w use a wider bar -W wide filename (un truncate)判决这标志着本指南关于在 Linux 上查看磁盘分区和磁盘使用情况的顶级工具的结束。请记住,还有其他几种可用的工具。如果您使用过任何其他更好的工具,请随时在下面的评论中分享。 查看更多: 如何在 Linux 上调整 XFS/Btrfs 文件系统的大小解决没有工具可用于使用“gpt”调整磁盘大小的问题使用 Cryptsetup 加密 Ubuntu/Debian 磁盘分区在不使用 LVM 的情况下调整 ext2/3/4 和 XFS 根分区的大小 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |