The disk is a very important hardware resource in Linux system, how to manage them effectively is directly related to performance of the whole system. If you have some experiences on the Linux disk management and should know that “df”, “du” and “fdisk” these three commands: df is used to check the file system disk usage,

du checks a disk space usage, and fdisk is used to disk partition. These three tools are commonly used in Linux disk management, here will share with you how to use them.
1. df
df command can get how much space is occupied and how much availabe space is left, it cal also display the usage information for both inode and disk blocks.
[root@pxe /]# df –help
Usage: df [OPTION]… [FILE]…
Show information about the file system on which each FILE resides,
or all file systems by default.

Mandatory arguments to long options are mandatory for short options too.
-a, –all include dummy file systems
-B, –block-size=SIZE use SIZE-byte blocks
-h, –human-readable print sizes in human readable format (e.g., 1K 234M 2G)
-H, –si likewise, but use powers of 1000 not 1024
-i, –inodes list inode information instead of block usage
-k like –block-size=1K
-l, –local limit listing to local file systems
–no-sync do not invoke sync before getting usage info (default)
-P, –portability use the POSIX output format
–sync invoke sync before getting usage info
-t, –type=TYPE limit listing to file systems of type TYPE
-T, –print-type print file system type
-x, –exclude-type=TYPE limit listing to file systems not of type TYPE
-v (ignored)
–help display this help and exit
–version output version information and exit

SIZE may be (or may be an integer optionally followed by) one of following:
kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

Report bugs to <bug-coreutils@gnu.org>.

[root@pxe /]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 296281516 64214432 216774008 23% /
/dev/sda1 497829 29624 442503 7% /boot
tmpfs 2021856 0 2021856 0% /dev/shm

Sample outputs:
Display Each File System Disk Space Usage
1 [root@devops ~]# df
2 Filesystem 1K-blocks Used Available Use% Mounted on
3 /dev/xvda1 20641404 1165572 18427308 6% /
4 tmpfs 250416 0 250416 0% /dev/shm
5 /dev/xvdb1 10317828 2392496 7401216 25% /media/
• The first column of Filesystem is the device file path name of file system (usually the hard disk partitions);
• The second column of Inodes is the amount of inode of data block.
• The third and fourth columns are the amount of used and available data block number.you may feel strange, why the total block number for third and fourth columns is not equal to the amount in the second column. because each partition left little space for the system administrator by default. Even the ordinary user space is full, there are still have spaces so that administrator can login to solve the problem.
2. du
du is the abbreviation of “disk usage”, this command will progressively into each subdirectory of the specified directory and display how many blocks were occupied. If not given a specified directory, then will show the statistics of the current directory.
du options:
[root@pxe /]# du –help
Usage: du [OPTION]… [FILE]…
or: du [OPTION]… –files0-from=F
Summarize disk usage of each FILE, recursively for directories.

Mandatory arguments to long options are mandatory for short options too.
-a, –all write counts for all files, not just directories
–apparent-size print apparent sizes, rather than disk usage; although
the apparent size is usually smaller, it may be
larger due to holes in (`sparse’) files, internal
fragmentation, indirect blocks, and the like
-B, –block-size=SIZE use SIZE-byte blocks
-b, –bytes equivalent to `–apparent-size –block-size=1′
-c, –total produce a grand total
-D, –dereference-args dereference FILEs that are symbolic links
–files0-from=F summarize disk usage of the NUL-terminated file
names specified in file F
-H like –si, but also evokes a warning; will soon
change to be equivalent to –dereference-args (-D)
-h, –human-readable print sizes in human readable format (e.g., 1K 234M 2G)
–si like -h, but use powers of 1000 not 1024
-k like –block-size=1K
-l, –count-links count sizes many times if hard linked
-m like –block-size=1M
-L, –dereference dereference all symbolic links
-P, –no-dereference don’t follow any symbolic links (this is the default)
-0, –null end each output line with 0 byte rather than newline
-S, –separate-dirs do not include size of subdirectories
-s, –summarize display only a total for each argument
-x, –one-file-system skip directories on different file systems
-X FILE, –exclude-from=FILE Exclude files that match any pattern in FILE.
–exclude=PATTERN Exclude files that match PATTERN.
–max-depth=N print the total for a directory (or file, with –all)
only if it is N or fewer levels below the command
line argument; –max-depth=0 is the same as
–summarize
–time show time of the last modification of any file in the
directory, or any of its subdirectories
–time=WORD show time as WORD instead of modification time:
atime, access, use, ctime or status
–time-style=STYLE show times using style STYLE:
full-iso, long-iso, iso, +FORMAT
FORMAT is interpreted like `date’
–help display this help and exit
–version output version information and exit

SIZE may be (or may be an integer optionally followed by) one of following:
kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

Report bugs to <bug-coreutils@gnu.org>.

Sample output:
Check The Disk Space Usage For /pe directory.
Report bugs to <bug-coreutils@gnu.org>.
[root@pxe /]# du -skh /pe
31G /pe

Display The Disk Usage For All Of File And Directory

[root@pxe boot]# du -h /tftpboot/
561M /tftpboot/Boot
14M /tftpboot/cent5
52K /tftpboot/Fonts
34M /tftpboot/cent6
34M /tftpboot/rhat6
15M /tftpboot/rhat5
52K /tftpboot/winpe/Fonts
3.1M /tftpboot/winpe/boot
4.0K /tftpboot/winpe/pe
2.7G /tftpboot/winpe
12K /tftpboot/pxelinux.cfg
3.9G /tftpboot/

3. fdisk
fdisk can be used to manage disk partitions, you can create a new paritition, delete an existing partition…
the below let us see how to use fdisk command:
Warning:if you don’t know what you are doing, pls don’t delete paritition or modify partition, or you may will lost your data in disk!!

[root@localhost ~]# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 514048+ 83 Linux
/dev/sda2 65 1084 8193150 82 Linux swap / Solaris
/dev/sda3 1085 5221 33230452+ 83 Linux

Disk /dev/sdb: 17.1 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdb doesn’t contain a valid partition table

Display All Fdisk Command Option Using “m” Command

[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklab el
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won’t be recoverable.

The number of cylinders for this disk is set to 2088.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition’s system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Command (m for help):

Create A New Disk Partition Using “n” Command

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2088, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-2088, default 2088):
Using default value 2088

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

Run fdisk –l to see the new partition

[root@localhost ~]# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 514048+ 83 Linux
/dev/sda2 65 1084 8193150 82 Linux swap / Solaris
/dev/sda3 1085 5221 33230452+ 83 Linux

Disk /dev/sdb: 17.1 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 2088 16771828+ 83 Linux

Delete the partition you just created.

[root@localhost ~]# fdisk /dev/sdb

The number of cylinders for this disk is set to 2088.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): d
Selected partition 1

Command (m for help): 1
1: unknown command
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition’s system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 514048+ 83 Linux
/dev/sda2 65 1084 8193150 82 Linux swap / Solaris
/dev/sda3 1085 5221 33230452+ 83 Linux

Disk /dev/sdb: 17.1 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

Leave a Reply

Your email address will not be published. Required fields are marked *