Linux 如何通过终端查看硬盘上未分配的可用空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12313384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:17:07  来源:igfitidea点击:

How to view unallocated free space on a hard disk through terminal

linuxterminalfedorahard-drive

提问by AnnGM

I want to view the unallocated free space on my hard disk through terminal. I've burned my brains searching the internet for a possible solution, but all in vain.

我想通过终端查看硬盘上未分配的可用空间。我在互联网上搜索可能的解决方案已经烧了我的大脑,但都是徒劳的。

I used all sorts of commands like df, du, fdisk, parted, etc. It tells me about the disks that are mounted and unmounted, but what about the unallocated space that I've left free?

我使用了各种命令,例如dfdufdiskparted等。它告诉我有关已挂载和未挂载的磁盘的信息,但是我留下的未分配空间呢?

Of course I can view it using the 'Disk Utility' app provided by Fedora, but since I LOVE being in the terminal I'd like to view in it.

当然,我可以使用 Fedora 提供的“磁盘实用程序”应用程序查看它,但由于我喜欢在终端中,我想在其中查看。

Can anyone please help me with a solution?

任何人都可以帮我解决问题吗?

回答by Rastislav Hasicek

If you need to see your partitions and/or filers with available space, mentioned utilities are what you need. You just need to use options.

如果您需要查看具有可用空间的分区和/或文件管理器,提到的实用程序就是您所需要的。你只需要使用选项。

For instance: df -hwill print you those information in "human-readable" form. If you need information only about free space, you could use: df -h | awk '{print $1" "$4}'.

例如:df -h将以“人类可读”的形式向您打印这些信息。如果您只需要有关可用空间的信息,您可以使用:df -h | awk '{print $1" "$4}'

回答by Basile Starynkevitch

You might want to use the fdisk -l /dev/sdacommand to see the partitioning of your sdadisk. The "free space" should be some unused partition (or lack of).

您可能希望使用该fdisk -l /dev/sda命令查看sda磁盘的分区。“可用空间”应该是一些未使用的分区(或缺少)。

回答by Burhan Khalid

Use GNU partedand print freecommand:

使用 GNUpartedprint free命令:

root@sandbox:~# parted
GNU Parted 2.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print free
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 64.4GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  Flags
        32.3kB  1049kB  1016kB            Free Space
 1      1049kB  256MB   255MB   primary   ext2         boot
        256MB   257MB   1048kB            Free Space
 2      257MB   64.4GB  64.2GB  extended
 5      257MB   64.4GB  64.2GB  logical                lvm
        64.4GB  64.4GB  1049kB            Free Space

回答by user2618594

To see in TB:

要在 TB 中查看:

# parted /dev/sda unit TB print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

# parted /dev/sda unit TB print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

To see in GB:

以GB查看:

# parted /dev/sda unit GB print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

# parted /dev/sda unit GB print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

To see in MB:

以 MB 为单位查看:

# parted /dev/sda unit MB print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

# parted /dev/sda unit MB print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

To see in bytes:

以字节为单位查看:

# parted /dev/sda unit B print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

# parted /dev/sda unit B print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

To see in %:

以%查看:

# parted /dev/sda unit '%' print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

# parted /dev/sda unit '%' print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

To see in sectors:

要在部门中查看:

# parted /dev/sda unit s print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

# parted /dev/sda unit s print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

Change /dev/sdato whatever device you are trying to find the information about. If you are using the result in any calculations, make sure to trim the trailing characters.

更改/dev/sda为您尝试查找相关信息的任何设备。如果您在任何计算中使用结果,请确保修剪尾随字符。

回答by Ugitha

Just follow below.

只需按照下面。

  • find out the dev type, whether it is /dev/sda /dev/hda /dev/vda etc.

  • look for vi /etc/fstab and find out the mounted partisions and there UUIDs etc

  • say, your harddisk is labeled as /dev/sda and you know number of /dev/sda under df -hT

  • 找出开发类型,是否是 /dev/sda /dev/hda /dev/vda 等。

  • 查找 vi /etc/fstab 并找出已安装的分区和 UUID 等

  • 假设您的硬盘被标记为 /dev/sda 并且您知道 df -hT 下的 /dev/sda 数量

then you need to find out remaining /dev/sda* right.

那么你需要找出剩余的 /dev/sda* 正确的。

so,

所以,

fdisk -l /dev/sda* will give the ALL /dev/sda* and you will find for example, /dev/sda4 or /dev/sda5

fdisk -l /dev/sda* 将给出所有 /dev/sda* 并且您会发现例如 /dev/sda4 或 /dev/sda5

then find out UUIDs of mounted partisions and those are not listed in /etc/fstab are the ones you can format and mount.

然后找出已挂载分区的 UUID,那些未在 /etc/fstab 中列出的分区是您可以格式化和挂载的分区。

just follow this up. a world to wise is sufficient.

只是按照这个。一个有智慧的世界就足够了。

回答by avinash

While using the disk utility graphically, it shows disk space used by all filesystem and it uses commands in the terminal such as df -H. In other words, it uses powers of 1000, not 1024. (Note: there is difference between -hand -H.)

在以图形方式使用磁盘实用程序时,它会显示所有文件系统使用的磁盘空间,并在终端中使用诸如df -H. 换句话说,它使用 1000 的幂,而不是 1024。(注意:-h和之间存在差异-H。)

While also finding the unallocated space in a hard disk using command line # fdisk /dev/sdawill display the total space and total cylinder value.

同时使用命令行查找硬盘中未分配的空间 # fdisk /dev/sda将显示总空间和总柱面值。

Now check the last cylinder value and subtract it from the total cylinder value. Hence the final value * 1000 gives you the unallocated disk space.

现在检查最后一个气缸值并从总气缸值中减去它。因此,最终值 * 1000 为您提供了未分配的磁盘空间。

Note: the cylinder value shows up in df -Has a power of 1000 or it might also show up using df -h, a power of 1024.

注意:柱面值显示df -H为 1000 的幂,或者也可能使用df -h1024 的幂显示。

回答by nnsense

This is an old question, but I wanted to give my answer as well.

这是一个老问题,但我也想给出我的答案。

Since we're talking about free available space, we should talk about sectors, since no partitioning or sizing of sectors is done.

由于我们谈论的是可用空间,因此我们应该谈论扇区,因为没有对扇区进行分区或调整大小。

For us human beings this doesn't make much sense. To have human-readable information we must translate this number into bytes. So, we have a disk already partitioned and we want to know how much space we may use. I personally don't like the partedsolution because my brain-memory for commands is already taken. There is also cfdisk, which gives you free space. But I think fdisk is the quickest solution: it's plain and simple, with nothing to install: execute fdisk /dev/sdxand then enter vinto the interactive shell. It will gives you the number of sectors still free.

对于我们人类来说,这没有多大意义。为了获得人类可读的信息,我们必须将此数字转换为字节。所以,我们有一个已经分区的磁盘,我们想知道我们可以使用多少空间。我个人不喜欢这个parted解决方案,因为我对命令的大脑记忆已经被占用了。还有cfdisk,它为您提供免费空间。但我认为 fdisk 是最快的解决方案:它简单明了,无需安装:执行fdisk /dev/sdx然后进入v交互式 shell。它将为您提供仍然可用的扇区数。

2004-54-0 [17:03:33][root@minimac:~]$> fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
..
Command (m for help): v
Remaining 1713 unallocated 512-byte sectors

We still have 1713 sectors at 512 bytes each. So, because you love terminal (in 2012, who knows now?) we do echo $(((1713*512)/1024))k, which is 1713 sectors multiplied for 512 bytes (divided by 1024 to have KB), which gives 856k.. not even 900 KB.. and I need another disk..

我们仍然有 1713 个扇区,每个扇区 512 字节。所以,因为你喜欢终端(在 2012 年,谁知道现在?)我们这样做echo $(((1713*512)/1024))k,即 1713 个扇区乘以 512 字节(除以 1024 得到 KB),得到 856k .. 甚至不是 900 KB .. 我需要另一个磁盘..

回答by cwilli

A simple solution to the answer:

一个简单的答案解决方案:

parted /dev/sda

Display the help on unit. Then toggle it to the units you want.

在单元上显示帮助。然后将其切换到您想要的单位。

To show free space on the device, use:

要显示设备上的可用空间,请使用:

print free

回答by mattdm

In addition to all the answers about how to find unpartitionedspace, you may also have space allocated to an LVM volume but not actually in use. You can list physical volumes with the pvdisplayand see which volume groups each physical volume is associated with. If a physical volume isn't associated with anyvolume group, it's safe to reallocate or destroy. Assuming that it it isassociated with a volume group, the next step is to use vgdisplayto show your those. Among other things, this will show if you have any free "physical extents" — blocks of storage you can assign to a logical volume. You can get this in a concise form with vgs:

除了有关如何查找未分区空间的所有答案外,您还可能将空间分配给 LVM 卷,但实际上并未使用。您可以使用 列出物理卷pvdisplay并查看每个物理卷与哪些卷组相关联。如果物理卷未与任何卷组关联,则重新分配或销毁是安全的。假设它,它与卷组关联,下一步是使用vgdisplay显示您的那些。除其他外,这将显示您是否有任何可用的“物理范围”——可以分配给逻辑卷的存储块。您可以通过以下方式以简洁的形式获得vgs

$ sudo vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  fedora   1   3   0 wz--n- 237.46g    0 

... and here you can see I have nothing free. If I did, that last number would be bigger than zero.

......在这里你可以看到我没有任何空闲。如果我这样做,最后一个数字将大于零。

This is important, because that free space is invisibleto du, df, and the like, and also will show up as an allocated partition if you are using fdiskor another partitioning tool.

这很重要,因为该可用空间对、等不可见,并且如果您正在使用或其他分区工具,也会显示为已分配的分区。dudffdisk

回答by Anatoly

when you cut you disk in partitions by fdisk you may be careful so as not to left gaps with free space. So command automatically align partitions and you'll get some gaps between parts. There are many articles in net why need to do so. The reason is that it gives solution with less errors. That was many years ago. Now I don't know is there errors occurs if you do all without any gaps. But first. You may do so if you don't allow alignment you sen begin of the next part=end previous+1. But is first part begins always with 2048 sector. So call expert part you may shift it to 0. But strongly recomended to do so if you plan to boot from this disk. If only for data you'll gain 1 Mb additional disk space. This is an MBR space. If you plan to install OS on this disk you don't use GPT partition type. Also it's more suitable not all OS see GPT parts of disks. But some see them. If you don't sure it use msdos. While format the block size is 4096 bytes(logical) physical one is 512 bytes. I don't do so but you may set block size=512 too. There was many discussion about that. It's lead to disk errors. But you'll give some free disk space too especially when you have many small size files. You disk will fill more compactly. And if you give already partitioned disk with filled them with data and maybe installed OS you maybe want to do so, it was very problems to do. But is possible for Linux. For Windows no... You must save backup and mbr too, write UUID every part then use fdisk and format as setting right UUID and LABEL for every part restore mbr with dd command and if you don't do any wrong all will be work as before but without any gaps.

当您通过 fdisk 将磁盘切成分区时,您可能会小心,以免留下可用空间的间隙。所以命令会自动对齐分区,你会在部分之间得到一些间隙。网上有很多文章为什么需要这样做。原因是它提供了错误较少的解决方案。那是很多年前的事了。现在我不知道如果你在没有任何差距的情况下做所有事情是否会发生错误。但首先。如果您不允许对齐,您可以这样做,您 sen 开始下一部分=结束前一个 + 1。但第一部分总是从 2048 扇区开始。所以调用专家部分你可以把它移到 0。但是如果你打算从这个磁盘启动,强烈建议这样做。如果仅用于数据,您将获得 1 Mb 的额外磁盘空间。这是一个 MBR 空间。如果您打算在此磁盘上安装操作系统,请不要使用 GPT 分区类型。还有它' s 更适合并非所有操作系统都看到磁盘的 GPT 部分。但有些人看到了他们。如果您不确定它使用 msdos。而格式块大小是 4096 字节(逻辑)物理一个是 512 字节。我不这样做,但您也可以设置块大小 = 512。对此有很多讨论。它导致磁盘错误。但是您也会提供一些可用的磁盘空间,尤其是当您有许多小文件时。您的磁盘将填充得更紧凑。如果你给已经分区的磁盘填满了数据,并且可能安装了操作系统,你可能想要这样做,这是非常有问题的。但是对于 Linux 是可能的。对于 Windows 不...您也必须保存备份和 mbr,每个部分都写入 UUID,然后使用 fdisk 和 format 作为设置正确的 UUID 和 LABEL 为每个部分使用 dd 命令恢复 mbr,如果您不这样做'