本文的经典应用场景:
1.查找占用磁盘存储空间最大的目录/文件
2.关于【磁盘分区】的相关概念和实操,详见另一博文:[Linux]磁盘分区 - 博客园/千千寰宇

fdisk := "Partition table manipulator(操纵器) for Linux" := Linux的磁盘分区工具
-l 【查看磁盘所有分区情况】;列出指定设备的分区表信息并退出。 如果没有给出设备,那么使用那些在 /proc/partitions (如果存在)提到的.
-b sector size (512, 1024, 2048 or 4096)
-c switch off DOS-compatible mode
-h print help
-u give sizes in sectors instead of cylinders;以扇区数而不是以柱面数的形式显示分区表中各分区的信息.
-v print version;打印 fdisk 的版本信息并退出
-C specify the number of cylinders
-H specify the number of heads
-S specify the number of sectors per track;分区 将分区的 大小 (单位为块)信息输出到标准输出

  • 选择分区磁盘:进入磁盘,分区操作(创建、删除)准备
fdisk /dev/sda
  • 在当前磁盘上建立扩展分区
fdisk /ext
  • 查看磁盘所有分区情况
[root@test ~]# fdisk -l

Disk /dev/vda: 34.4 GB, 34359738368 bytes, 67108864 sectors 【硬盘vda的总存储空间: 34.4GB】
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos 【硬盘标签类型: dos:= 硬盘采用MBR分区表; gpt := 硬盘采用GPT分区表】
Disk identifier: 0x0009e5d4

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    67108830    33553391+  83  Linux

/////////////////////////////////////////////////////////////////

[root@test1 ~]# fdisk -l

Disk /dev/loop0: 3916 MB, 3916431360 bytes
64 heads, 32 sectors/track, 3735 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x47840dcd

      Device Boot      Start         End      Blocks   Id  System
/dev/loop0p1   *           1        3735     3824640   17  Hidden HPFS/NTFS

Disk /dev/sda: 536.9 GB, 536870912000 bytes 【注: 硬盘sda总空间: 536.9GB】
255 heads, 63 sectors/track, 65270 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a3902

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64       65271   523774976   8e  Linux LVM

Disk /dev/mapper/VolGroup-LogVol01: 519.2 GB, 519162560512 bytes
255 heads, 63 sectors/track, 63117 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/VolGroup-LogVol00: 17.2 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

du

du := disk usage

  • 查看 目标目录下 占用空间的大小

方法1

du -h --max-depth=1 <targetDir>

方法2

step1 切换到目标路径

cd <目标目录路径>

step2 执行du命令

[root@govern /]# du -sh *
(或者 du -sh * <targetDir>)
5.9M	bin
25M	boot
260M	data
8.0K	dedia
160K	dev
30M	etc
435G	home
176M	lib
38M	lib64
16K	lost+found
3.7G	media
4.0K	mnt
4.2G	opt
du: 无法访问"proc/209022/task/209022/fd/4": 没有那个文件或目录
du: 无法访问"proc/209022/task/209022/fdinfo/4": 没有那个文件或目录
du: 无法访问"proc/209022/fd/4": 没有那个文件或目录
du: 无法访问"proc/209022/fdinfo/4": 没有那个文件或目录
du: 无法访问"proc/967628/task/969740/fd/69": 没有那个文件或目录
0	proc
2.8G	root
16M	sbin
137M	sefon
4.0K	selinux
4.0K	srv
0	sys
1.2M	tmp
1.7G	usr
7.0G	var
du: 无法访问"/proc/209022/task/209022/fd/4": 没有那个文件或目录
du: 无法访问"/proc/209022/task/209022/fdinfo/4": 没有那个文件或目录
du: 无法访问"/proc/209022/fd/4": 没有那个文件或目录
du: 无法访问"/proc/209022/fdinfo/4": 没有那个文件或目录
451G	/

ls

  • 查找占用存储空间超过阈值的文件(及其路径)
[root@xxx ~]# find /home/sdc -type f -size +800M
/home/myTomcat/logs/catalina.out

补充: 此命令(切换到目标路径下) / du -sh *亦可

  • 查看指定文件的大小
[root@xxx ~]#ls -l --block-size='G' /home/myTomcat/logs/catalina.out
-rw-r----- 1 sdc sdc 431G 10月 17 12:03 /home/myTomcat/logs/catalina.out

[root@xxx ~]#ls -l --block-size='M' /home/myTomcat/logs/catalina.out
-rw-r----- 1 sdc sdc 440590M 10月 17 12:03 /home/myTomcat/logs/catalina.out


[root@xxx ~]#ls -l --block-size='K' /home/myTomcat/logs/catalina.out
-rw-r----- 1 sdc sdc 451163616K 10月 17 12:03 /home/myTomcat/logs/catalina.out


[root@xxx ~]# ls -lh
total 565M
-rwxr-xr-x 1 billing_dx BILLING 1.1M Mar 10 18:56 AcctMgrService
-rw-r--r-- 1 billing_dx BILLING  364 Mar 10 18:59 AcctMgrService.conf
-rw------- 1 billing_dx BILLING  11G Mar 13 18:37 core.22836
-rw-r--r-- 1 billing_dx BILLING  410 Mar 13 17:38 dll_info.conf
-rwxr-xr-x 1 billing_dx BILLING  39 Mar 10 18:57 go

补充: 此命令(切换到目标路径下) / du -sh *亦可

df

[root@localhost opt]# df -hT
文件系统       类型      容量  已用  可用 已用% 挂载点
/dev/vda2      xfs       1.0T   26G  999G    3% /
devtmpfs       devtmpfs   32G     0   32G    0% /dev
tmpfs          tmpfs      32G     0   32G    0% /dev/shm
tmpfs          tmpfs      32G   18M   32G    1% /run
tmpfs          tmpfs      32G     0   32G    0% /sys/fs/cgroup
/dev/vda3      xfs       200G   11G  190G    6% /home
/dev/vda6      xfs       300G   33M  300G    1% /ruanjian
/dev/vda1      xfs       197M  133M   65M   68% /boot
tmpfs          tmpfs     6.3G   12K  6.3G    1% /run/user/42
tmpfs          tmpfs     6.3G     0  6.3G    0% /run/user/1000

df -h /iso/CentOS7-1804.iso # 【IMPORTANT】查看指定文件(or目录)的挂载路径 / 查看指定的文件目录属于磁盘分区(挂载点)
      Filesystem             Size  Used Avail Use% Mounted on
      /dev/mapper/cl-root    15T    38G   15T   1% /

df –h # 查看已挂载设备的信息
	Filesystem           Size  Used Avail Use% Mounted on
	/dev/mapper/cl-root   15T   14G   15T   1% /
	devtmpfs              32G     0   32G   0% /dev
	tmpfs                 32G     0   32G   0% /dev/shm
	tmpfs                 32G  9.4M   32G   1% /run
	tmpfs                 32G     0   32G   0% /sys/fs/cgroup
	/dev/sda2           1016M  131M  886M  13% /boot
	/dev/sda1            200M  9.5M  191M   5% /boot/efi
	tmpfs                6.3G     0  6.3G   0% /run/user/0
	/dev/loop0           4.1G  4.1G     0 100% /var/www/html/repo

ll /var/www/html/repo # 查看挂载的目录中是否有内容
	total 654
	-rw-r--r--. 1 root root     14 Dec  5  2016 CentOS_BuildTag
	drwxr-xr-x. 3 root root   2048 Dec  5  2016 EFI
	-rw-r--r--. 1 root root    215 Dec 10  2015 EULA
	-rw-r--r--. 1 root root  18009 Dec 10  2015 GPL
	drwxr-xr-x. 3 root root   2048 Dec  5  2016 images
	drwxr-xr-x. 2 root root   2048 Dec  5  2016 isolinux
	drwxr-xr-x. 2 root root   2048 Dec  5  2016 LiveOS
	drwxrwxr-x. 2 root root 630784 Dec  5  2016 Packages
	drwxrwxr-x. 2 root root   4096 Dec  5  2016 repodata
	-rw-r--r--. 1 root root   1690 Dec 10  2015 RPM-GPG-KEY-CentOS-7
	-rw-r--r--. 1 root root   1690 Dec 10  2015 RPM-GPG-KEY-CentOS-Testing-7
	-r--r--r--. 1 root root   2883 Dec  5  2016 TRANS.TBL

X 推荐文献

相关文章: