Disk Related Commands-1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Disk related commands

du: Disk Usage


The“du” (Disk Usage) is a standard Unix/Linux command, used to check the
information of disk usage of files and directories on a machine. The du command has
many parameter options that can be used to get the results in many formats. The du
command also displays the files and directory sizes in a recursively manner.

Syntax:

du [OPTION]... [FILE]...

Options:
-a, In addition to the default output, include information for each non-directory entry
-k, show sizes as multiples of 1024 bytes, not 512-byte
-s, report only the sum of the usage in the current directory, not for each file

Example : 1 Disk usage of a Directory , when du command is executed without any options
$ du Pictures
12072 Pictures

In the above example , 'Pictures' is a directory and when du command is executed , it displays
disk usage figures in Kilobytes (default block size) .

Example : 2 Display sizes in human readable format (e.g., 1

K 250M , 2G)
$ du -h Pictures
12M Pictures
Example : 3 Display disk usage counts for all the files

This can be done using -a option.

$ du -a /var/log/httpd/
As shown in the above example , that disk usage of individual files (inside the directory
'/var/log/httpd') is displayed in the output.

dd command:

The command ‘dd’ is one of the very powerful utility which can be used in a variety
of ways. This tool is mainly used for copying and converting data, hence it stands for
‘data duplicator’.

This tool can be used for.

• Backing up and restoring an entire hard drive or a partition

• Copy regions of raw device files like backing up MBR(master boot record)

• Converting data formats like ASCII to EBCDIC

• Converting lowercase to uppercase and vice versa

• Creating files with fixed size

Syntax of ‘dd’ command:

dd if=<source file name> of=<target file name> [Options]

Example : 1 Backup entire hard drive to another drive.

dd if=/dev/sda of=/dev/sdb bs=4096 conv=noerror,sync

Here, ‘if’ stands for input file, ‘of’ stands for output file and ‘bs’ stands for the block size
(number of bytes to be read/write at a time). The conversion parameter ‘noerror’ allows the
tool to continue to copy the data eventhough it encounter any errors. The sync option allows
to use synchronized I/O.

The above command will copy all the data from the disk /dev/sda to /dev/sdb. ‘dd’ doesn’t
know anything about the filesystem or partitions- it will just copy everything from /dev/sda to
/dev/sdb. So, this will clone the disk with the same data on same partition.

Example : 2 Creating a disk image

dd if=/dev/sda of=/tmp/sdadisk.img

Backing up a disk to an image will be faster than copying the exact data. Also, disk image
make the restoration much easier.

Example : 3 Creating a compressed disk image

dd if=/dev/sda | gzip >/tmp/sdadisk.img.gz


Example : 4 Restoring hard disk image

dd if=/tmp/sdadisk.img of=/dev/sda

df command
df displays the amount of disk space available on the file system in terms of disk
blocks. The command displays the capacity of each file system, the space in use, the
free space and the number of free files.

Syntax of df command:
df [OPTION]... [FILE]...

Example : 1 We can use the df command without any option or file system in order to obtain
information about all the file systems installed on our machines.
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/loop0 18761008 15246876 2554440 86% /
none 4 0 4 0% /sys/fs/cgroup
udev 493812 4 493808 1% /dev
tmpfs 100672 1364 99308 2% /run
none 5120 0 5120 0% /run/lock
none 503352 1764 501588 1% /run/shm
none 102400 20 102380 1% /run/user
/dev/sda3 174766076 164417964 10348112 95% /host

Example : 2 In order to easily remember the size of the partitions, we use the –h option to
display the size of the partition in human readable form

$ df -h
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/loop0 18761008 15246924 2554392 86% /

Example : 3 To know the amount of space available in a particular partition, we can specify
that while giving the df command. Such as in order to know the amount of free disk in the
root directory, we need to give the following command:

$ df /

1. fdisk: Dividing the hard disk into one or more logical disks is called partitions. The
partitions or the divisions of the disk are described in the partition table found in
sector 0 of the disk. fdisk creates both partitions as well as file systems. The fdisk
command is used to cerate, delete and activate partitions.

You might also like