Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

The „du“ Command Line

The „du“ (Disk Usage) command line is a standard command under Unix and Linux. It is used to
list the disk space used by files on a machine and crucial for disk space management on unix and
linux systems. Several paramaters enable users to format and filter the results.

Using du is a good starting point when trying to clean up unused disk space under Unix.

Many smaller machines run Microsoft Windows. There is a wide range of graphical disk space
managers for Windows, but few will enable you to scan Linux and Unix servers without having
to resort to Samba. The Enterprise Edition of SpaceObServer will scan Unix and Linux servers
via SSH.

Using du

To analyze disk usage in a specific directory tree, simply enter the command line.
# du

Adding /* will show all directories and files at the top level of the file system:
# du -s /*

You can also specify which folder shall be scanned:


# du -s /var

There are several ways to filter the information and gain the insights required to proceed.

Parameters
A wide range of parameters can be used to refine the results. Many flags can be used in
combination. This is a (by no means complete) selection of useful arguments.
#du -s

Display the disk space used by all files in the selected directory by adding -s. You cannot limit
the folder depths while using this argument.
r# du -s /*

#du -h
Adding the parameter -h shows the results in a human-readable format: It adds size information
in Byte, Kilobyte, Megabyte, Gigabyte, Terabyte, and Petabyte.

It is a useful argument to remember and can be attached to any other parameter.

#du -c
Use the -c argument to sum up the used disk space in the last line of the results.
#du -ch /var

Combine -h and -s for Detailed and easy-to-read Information


-hs will show you all files in the folder and present the results in human-readable format:

# du -hs *

The disk space manager TreeSize Professional provides similar information in the „Details“
view.

Sort Results
Add | sort to the command line to sort the output. | sort -rh sorts recursively and displays the
biggest sizes on top.
# du -s /* | sort -rh

See the Top10


Limit the output to the first ten lines with the | head command and see the ten largest files or
folders.
# du -sh /* | sort -rh | head

Exclude File Types


If you want to exclude certain file types from the equation, use the –exclude argument in
combination with other flags. In this example, du will not count .BIN files.
# du -sh /var

# du -sh --exclude="*.bin" /var

Useful “df” Commands to Check Disk Space in Linux


Check File System Disk Space Usage
The “df” command displays the information of device name, total blocks, total disk space, used
disk space, available disk space and mount points on a file system.

# df

Display Information of all File System Disk Space Usage

The same as above, but it also displays information of dummy file systems along with all the file
system disk usage and their memory utilization.

# df -a

Show Disk Space Usage in Human Readable Format


Have you noticed that above commands displays information in bytes, which is not readable yet
all, because we are in a habit of reading the sizes in megabytes, gigabytes etc. as it makes very
easy to understand and remember.

The df command provides an option to display sizes in Human Readable formats by using ‘-h’
(prints the results in human readable format (e.g., 1K 2M 3G)).

# df -h

Display Information of /home File System


To see the information of only device /home file system in human readable format use the
following command.
# df -hT /home

Display Information of File System in Bytes


To display all file system information and usage in 1024-byte blocks, use the option ‘-k‘ (e.g. –
block-size=1K) as follows.

# df -k

Display Information of File System in MB


To display information of all file system usage in MB (Mega Byte) use the option as ‘-m‘.

# df -m

Display Information of File System in GB


To display information of all file system statistics in GB (Gigabyte) use the option as ‘df -h‘.

# df -h

Display File System Inodes


Using ‘-i‘ switch will display the information of number of used inodes and their percentage for
the file system.
# df -i

Display File System Type


If you notice all the above commands output, you will see there is no file system type mentioned
in the results. To check the file system type of your system use the option ‘T‘. It will display file
system type along with other information.
# df -T

Include Certain File System Type


If you want to display certain file system type use the ‘-t‘ option. For example, the following
command will only display ext3 file system.
# df -t ext3
Exclude Certain File System Type
If you want to display file system type that doesn’t belongs to ext3 type use the option as ‘-x‘.
For example, the following command will only display other file systems types other than ext3.
# df -x ext3

View all Disk Partitions in Linux


The following basic command list all existing disk partition on your system. The ‘-l‘ argument
stand for (listing all partitions) is used with fdisk command to view all available partitions on
Linux. The partitions are displayed by their device’s names. For example: /dev/sda, /dev/sdb or
/dev/sdc.

# fdisk -l

View Specific Disk Partition in Linux


To view all partitions of specific hard disk use the option ‘-l‘ with device name. For example, the
following command will display all disk partitions of device /dev/sda. If you’ve different device
names, simple write device name as /dev/sdb or /dev/sdc.
# fdisk -l /dev/sda

Check all Available fdisk Commands

If you would like to view all commands which are available for fdisk. Simply use the following
command by mentioning the hard disk name such as /dev/sda as shown below. The following
command will give you output similar to below.

# fdisk /dev/sda

Type ‘m‘ to see the list of all available commands of fdisk which can be operated on /dev/sda
hard disk. After, I enter ‘m‘ on the screen, you will see the all available options for fdisk that you
can be used on the /dev/sda device.

# fdisk /dev/sda

Print all Partition Table in Linux


To print all partition table of hard disk, you must be on command mode of specific hard disk say
/dev/sda.

# fdisk /dev/sda

From the command mode, enter ‘p‘ instead of ‘m‘ as we did earlier. As I enter ‘p‘, it will print
the specific /dev/sda partition table.

Command (m for help): p

How to Delete a Partition in Linux


If you would like to delete a specific partition (i.e /dev/sda9) from the specific hard disk such as
/dev/sda. You must be in fdisk command mode to do this.

# fdisk /dev/sda

Next, enter ‘d‘ to delete any given partition name from the system. As I enter ‘d‘, it will prompt
me to enter partition number that I want to delete from /dev/sda hard disk. Suppose I enter
number ‘4‘ here, then it will delete partition number ‘4‘ (i.e. /dev/sda4) disk and shows free
space in partition table. Enter ‘w‘ to write table to disk and exit after making new alterations to
partition table. The new changes would only take place after next reboot of system. This can be
easily understood from the below output.

How to Create a New Partition in Linux

If you’ve free space left on one of your device say /dev/sda and would like to create a new
partition under it. Then you must be in fdisk command mode of /dev/sda. Type the following
command to enter into command mode of specific hard disk.
# fdisk /dev/sda
After entering in command mode, now press “n” command to create a new partition
under /dev/sda with specific size. This can be demonstrated with the help of following
given output.

While creating a new partition, it will ask you two options ‘extended‘ or ‘primary‘ partition
creation. Press ‘e‘ for extended partition and ‘p‘ for primary partition. Then it will ask you to
enter following two inputs.

First cylinder number of the partition to be create.


Last cylinder number of the partition to be created (Last cylinder, +cylinders or +size).
You can enter the size of cylinder by adding “+5000M” in last cylinder. Here, ‘+‘ means
addition and 5000M means size of new partition (i.e 5000MB). Please keep in mind that after
creating a new partition, you should run ‘w‘ command to alter and save new changes to partition
table and finally reboot your system to verify newly created partition.

Command (m for help): w


The partition table has been altered!

Calling ioctl() to re-read partition table.

How to Format a Partition in Linux

After the new partition is created, don’t skip to format the newly created partition using
‘mkfs‘ command. Type the following command in the terminal to format a partition.
Here /dev/sda4 is my newly created partition.

[root@tecmint ~]# mkfs.ext4 /dev/sda4

How to Check Size of a Partition in Linux


After formatting new partition, check the size of that partition using flag ‘s‘ (displays size
in blocks) with fdisk command. This way you can check size of any specific device.

[root@tecmint ~]# fdisk -s /dev/sda2

5194304

How to Fix Partition Table Order

If you’ve deleted a logical partition and again recreated it, you might notice ‘partition out of
order‘ problem or error message like ‘Partition table entries are not in disk order‘.
For example, when three logical partitions such as (sda4, sda5 and sda6) are deleted, and new
partition created, you might expect the new partition name would be sda4. But, the system would
create it as sda5. This happens because of, after the partition are deleted, sda7 partition had been
moved as sda4 and free space shift to the end.

To fix such partition order problems, and assign sda4 to the newly created partition, issue the ‘x‘
to enter an extra functionality section and then enter ‘f‘ expert command to fix the order of
partition table as shown below.

[root@tecmint ~]# fdisk /dev/sda

Command (m for help): x

Expert command (m for help): f


Done.

Expert command (m for help): w


The partition table has been altered!

Calling ioctl() to re-read partition table.

After, running ‘f‘ command, don’t forget to run ‘w‘ command to save and exit from fdisk
command mode. Once it fixed partition table order, you will no longer get error messages.

How to Disable Boot Flag (*) of a Partition

By default, fdisk command shows the boot flag (i.e. ‘*‘) symbol on each partition. If you want to
enable or disable boot flag on a specific partition, do the following steps.
[root@tecmint ~]# fdisk /dev/sda

Press ‘p‘ command to view the current partition table, you see there is a boot flag (asterisk (*)
symbol in orange color) on /dev/sda1 disk as shown below.

[root@tecmint ~]# fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 637.8 GB, 637802643456 bytes

255 heads, 63 sectors/track, 77541 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sda1 * 1 13 104391 83 Linux


/dev/sda2 14 2624 20972857+ 83 Linux
/dev/sda3 2625 4582 15727635 83 Linux
/dev/sda4 4583 77541 586043167+ 5 Extended
/dev/sda5 4583 5887 10482381 83 Linux
/dev/sda6 5888 7192 10482381 83 Linux
/dev/sda7 7193 7845 5245191 83 Linux
/dev/sda8 7846 8367 4192933+ 82 Linux swap / Solaris
/dev/sda9 8368 77541 555640123+ 8e Linux LVM
Next enter command ‘a‘ to disable boot flag, then enter partition number ‘1‘ as (i.e. /dev/sda1) in
my case. This will disable boot flag on the partition /dev/sda1. This will remove the asterisk (*)
flag.

Command (m for help): a


Partition number (1-9): 1

Command (m for help): p

Disk /dev/sda: 637.8 GB, 637802643456 bytes


255 heads, 63 sectors/track, 77541 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System


/dev/sda1 1 13 104391 83 Linux
/dev/sda2 14 2624 20972857+ 83 Linux
/dev/sda3 2625 4582 15727635 83 Linux
/dev/sda4 4583 77541 586043167+ 5 Extended
/dev/sda5 4583 5887 10482381 83 Linux
/dev/sda6 5888 7192 10482381 83 Linux
/dev/sda7 7193 7845 5245191 83 Linux
/dev/sda8 7846 8367 4192933+ 82 Linux swap / Solaris
/dev/sda9 8368 77541 555640123+ 8e Linux LVM

Command (m for help):

You might also like