10 Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories

The Linux “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.

This article explains 10 useful “du” commands with their examples, that might helps you to find out the sizes of files and directories in Linux. The information provided in this article are taken from the man pages of du command.

Read Also:

  1. How to maipulate the shell output in php

1. To find out the disk usage summary of a /home/sourabh directory tree and each of its sub directories. Enter the command as:

[root@sourabh]# du /home/sourabh

40 /home/sourabh/downloads
4 /home/sourabh/.mozilla/plugins
4 /home/sourabh/.mozilla/extensions
12 /home/sourabh/.mozilla
12 /home/sourabh/.ssh
689112 /home/sourabh/Ubuntu-12.10
689360 /home/sourabh

The output of the above command displays the number of disk blocks in the /home/sourabh directory along with its sub-directories.

2. Using “-h” option with “du” command provides results in “Human Readable Format“. Means you can see sizes in Bytes, Kilobytes, Megabytes, Gigabytes etc.

[root@sourabh]# du -h /home/sourabh

40K /home/sourabh/downloads
4.0K /home/sourabh/.mozilla/plugins
4.0K /home/sourabh/.mozilla/extensions
12K /home/sourabh/.mozilla
12K /home/sourabh/.ssh
673M /home/sourabh/Ubuntu-12.10
674M /home/sourabh

3. To get the summary of a grand total disk usage size of an directory use the option “-s” as follows.

[root@sourabh]# du -sh /home/sourabh

674M /home/sourabh

4. Using “-a” flag with “du” command displays the disk usage of all the files and directories.

[root@sourabh]# du -a /home/sourabh

4 /home/sourabh/.bash_logout
12 /home/sourabh/downloads/uploadprogress-1.0.3.1.tgz
24 /home/sourabh/downloads/Phpfiles-org.tar.bz2
40 /home/sourabh/downloads
12 /home/sourabh/uploadprogress-1.0.3.1.tgz
4 /home/sourabh/.mozilla/plugins
4 /home/sourabh/.mozilla/extensions
12 /home/sourabh/.mozilla
4 /home/sourabh/.bashrc
689108 /home/sourabh/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
689112 /home/sourabh/Ubuntu-12.10
689360 /home/sourabh

5. Using “-a” flag along with “-h” displays disk usage of all files and folders in human readeable format. The below output is more easy to understand as it shows the files in Kilobytes, Megabytes etc.

[root@sourabh]# du -ah /home/sourabh

4.0K /home/sourabh/.bash_logout
12K /home/sourabh/downloads/uploadprogress-1.0.3.1.tgz
24K /home/sourabh/downloads/Phpfiles-org.tar.bz2
40K /home/sourabh/downloads
12K /home/sourabh/uploadprogress-1.0.3.1.tgz
4.0K /home/sourabh/.mozilla/plugins
4.0K /home/sourabh/.mozilla/extensions
12K /home/sourabh/.mozilla
4.0K /home/sourabh/.bashrc
673M /home/sourabh/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
673M /home/sourabh/Ubuntu-12.10
674M /home/sourabh

6. Find out the disk usage of a directory tree with its subtress in Kilobyte blcoks. Use the “-k” (displays size in 1024 bytes units).

[root@sourabh]# du -k /home/sourabh
40 /home/sourabh/downloads
4 /home/sourabh/.mozilla/plugins
4 /home/sourabh/.mozilla/extensions
12 /home/sourabh/.mozilla
12 /home/sourabh/.ssh
689112 /home/sourabh/Ubuntu-12.10
689360 /home/sourabh

7. To get the summary of disk usage of directory tree along with its subtrees in Megabytes (MB) only. Use the option “-mh” as follows. The “-m” flag counts the blocks in MB units and “-h” stands for human readable format.

[root@sourabh]# du -mh /home/sourabh

40K /home/sourabh/downloads
4.0K /home/sourabh/.mozilla/plugins
4.0K /home/sourabh/.mozilla/extensions
12K /home/sourabh/.mozilla
12K /home/sourabh/.ssh
673M /home/sourabh/Ubuntu-12.10
674M /home/sourabh

8. The “-c” flag provides a grand total usage disk space at the last line. If your directory taken 674MB space, then the last last two line of the output would be.

[root@sourabh]# du -ch /home/sourabh

40K /home/sourabh/downloads
4.0K /home/sourabh/.mozilla/plugins
4.0K /home/sourabh/.mozilla/extensions
12K /home/sourabh/.mozilla
12K /home/sourabh/.ssh
673M /home/sourabh/Ubuntu-12.10
674M /home/sourabh
674M total

9. The below command calculates and displays the disk usage of all files and directories, but excludes the files that matches given pattern. The below command excludes the “.txt” files while calculating the total size of diretory. So, this way you can exclude any file formats by using flag “-–exclude“. See the output there is no txt files entry.

[root@sourabh]# du -ah --exclude="*.txt" /home/sourabh

4.0K /home/sourabh/.bash_logout
12K /home/sourabh/downloads/uploadprogress-1.0.3.1.tgz
24K /home/sourabh/downloads/Phpfiles-org.tar.bz2
40K /home/sourabh/downloads
12K /home/sourabh/uploadprogress-1.0.3.1.tgz
4.0K /home/sourabh/.bash_history
4.0K /home/sourabh/.bash_profile
4.0K /home/sourabh/.mozilla/plugins
4.0K /home/sourabh/.mozilla/extensions
12K /home/sourabh/.mozilla
4.0K /home/sourabh/.bashrc
24K /home/sourabh/Phpfiles-org.tar.bz2
4.0K /home/sourabh/geoipupdate.sh
4.0K /home/sourabh/.zshrc
120K /home/sourabh/goaccess-0.4.2.tar.gz.1
673M /home/sourabh/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
673M /home/sourabh/Ubuntu-12.10
674M /home/sourabh

10. Display the disk usage based on modification of time, use the flag “–time” as shown below.

[root@sourabh]# du -ha --time /home/sourabh

4.0K 2012-10-12 22:32 /home/sourabh/.bash_logout
12K 2013-01-19 18:48 /home/sourabh/downloads/uploadprogress-1.0.3.1.tgz
24K 2013-01-19 18:48 /home/sourabh/downloads/Phpfiles-org.tar.bz2
40K 2013-01-19 18:48 /home/sourabh/downloads
12K 2013-01-19 18:32 /home/sourabh/uploadprogress-1.0.3.1.tgz
4.0K 2012-10-13 00:11 /home/sourabh/.bash_history
4.0K 2012-10-12 22:32 /home/sourabh/.bash_profile
0 2013-01-19 18:32 /home/sourabh/xyz.txt
0 2013-01-19 18:32 /home/sourabh/abc.txt
4.0K 2012-10-12 22:32 /home/sourabh/.mozilla/plugins
4.0K 2012-10-12 22:32 /home/sourabh/.mozilla/extensions
12K 2012-10-12 22:32 /home/sourabh/.mozilla
4.0K 2012-10-12 22:32 /home/sourabh/.bashrc
24K 2013-01-19 18:32 /home/sourabh/Phpfiles-org.tar.bz2
4.0K 2013-01-19 18:32 /home/sourabh/geoipupdate.sh
4.0K 2012-10-12 22:32 /home/sourabh/.zshrc
120K 2013-01-19 18:32 /home/sourabh/goaccess-0.4.2.tar.gz.1
673M 2013-01-19 18:51 /home/sourabh/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
673M 2013-01-19 18:51 /home/sourabh/Ubuntu-12.10
674M 2013-01-19 18:52 /home/sourabh
Advertisements

Code snippet: Restrict access to a page in PHP based on remote hosts IP’s

<?php
//get the remote host IP
$szRemoteIP = $_SERVER['REMOTE_ADDR'];
//this is the array holding the allowed IP's list;
$arrAllowedIPs = array("192.168.0.1",
                       "192.168.2.3",
                       "192.168.63.22",
                       "127.0.0.1",
                       "10.10.192.102");
if (!in_array($szRemoteIP, $arrAllowedIPs))
{
    //if IP's is not in the list, deny access
    echo "You don't have the permissions to access this area from $szRemoteIP";
    exit(0);
}
//page code goes here
?>

How to maipulate the shell output in php

To get disk usage of server , need to execute shell command << df -h -T >>

I used <?php shell_exec(‘df -h -T’); ?>

To parse disk usage shell command to get memory usage of different directories , use code as follows :-

$output = shell_exec(‘df -h -T’);
$drives = split(“[\r|\n]”, trim($output));
echo “<pre>”;
print_r($drives);
echo “</pre>”;

Setting up LAMP Stack on your UBUNTU System

This is to help people set up LAMP on their desktops. Ubuntu server edition comes with LAMP but desktop users must download all the necessary files. This tutorial owes its credits to Ubuntu Community Docs and lots of people around the world who enlightened me about LAMP and Ubuntu. Let’s start.We first need some important packages. These are:

apache2 php5-mysql libapache2-mod-php5 mysql-server

You can get these via apt-get or from your Synaptics Package Manager. For getting via apt-get you must have an active internet connection. Type this command in the terminal:

sudo apt-get install apache2 php5-mysql libapache2-mod-php5 mysql-server

If you are in the Packages Manager, just search the packages and select them. This is the easiest method.

You may want to configure your LAMP, but before starting it is advisable to change the installation defaults. The default root user of MySQL has no password or a blank string as password. This should be changed. It is always advisable to create another user with full privileges as that as root. Use this alternate user instead as root. Root is the superuser.

Go to terminal: Type

mysql -u root

At MySQL console type

SET PASSWORD FOR ‘root’@’localhost’ = PASSWORD(‘yourpassword’);

Just put your password instead of yourpassword within single quotes as shown above.

After setting the password if you want to login as root,just enter this at the terminal:

mysql -u root -p

Create a new user and give all the privliges to it as explained above:

grant all privileges on *.* to ‘yourusername’@’localhost’

identified by ‘yourpassword’ with grant option;

Replace yourusername with the username you want and yourpassword with a proper password. If you want this user to deny Grant Option just remove WITH GRANT OPTION from the above statement.

Run, Stop, Test, And Restart Apache:

Start: sudo /usr/sbin/apache2ctl start

Stop: sudo /usr/sbin/apache2ctl stop

Test: sudo /usr/sbin/apache2ctl test

Retart: sudo /usr/sbin/apache2ctl restart

Running PHP Applications on Linux

Overview:

I usually find that lots of poeple find it quite difficult to install any application made in PHP and MySQL on their Linux boxes. They find it a bit confusing how to configure the settings. How to install the LAMP stack. Where to put the PHP applications code. I was quite inspired to write to this after having I saw people facing problems on Ubuntu Forums. This is the one which I dealt with.

Sometimes spoon-feeding becomes important,but only for the first time.

Scenario:

A newbie who knows just the basics of Linux. He/She wants to run a PHP application like wordpress / phpBB / osCommerce on their computer.

The distro is assumed to be Ubuntu, though it doesn’t change much for others too.

Lets Start:

Step 1: Getting the LAMP stack on your system

You first need to install LAMP on your box, Installation is quite simple. Just get the following packages using the command on your terminal ( Applications> Accessories>Terminal)

sudo apt-get install apache2 php5-mysql libapache2-mod-php5 mysql-server

While installing MySQL, it might ask for set up the root password,if it doesn’t ask, then continue.

For more information check this post here .

Step 2: Download the PHP application

You need to download the PHP application which you want to run over the webserver. Examples are WordPress, phpBB, osCommerce, MediaWiki etc

Get them and let it be in the folder where you have downloaded it.

Step 3: Moving these software to the Apache’s root directory

Apache’s root directory is /var/www (/var/www/html). This is owned by the user www-data which is a minimal user created just for running apache and web servers. A normal user cant write to this directory.

Now here is the solution of how to copy the data to this directory. This method can be used for any directory which the normal user does not have write access. The concept behind the method is to start an instance of nautilus ( explorer) with root (superuser) privileges. For this follow the isntructions

* Press Alt + F2

* A Run daialog box appears, over there type gksudo nautilus /var/www

* You will be asked for your password as you are elevating your privileges

* Nautilus opens up,now copy the tarball/zip file from the download location to /var/www

* Untar the archive by Right Click > Extract Archive

You may have the package in the folder say /var/www/wordpress. We are supposing wordpress as an example.

Step 4: Setting up username and accounts in database

Now you need to have a create a new database for the software you intent to work on. For this open MySQL from the terminal. If you had been asked to set up the password at time of MySQL installation then use this command

mysql -u root -p

and then enter the root password which you set up

if you were not asked to set up root password, then use this command at terminal

mysql -u root

After this is done you will be dropped to mysql prompt. Create a new database using this:

create database foo;

type quit to exit.

Step 5: Change the data in the config file.

Hunt for file which may have name such as config,settings etc etc. One you locate it,open to find the area for entering database settings. The settings should be:

Hostname: localhost

Username: root

Password: <the password you set up>

Database name: foo (or whatever you created)

Step 6: Access it via browser

Now point your browser to http://localhost/wordpress

You may get the basic installation thing or whatever stuff you need.

Additional Stuffs:

You can be asked to make some locations writable. There may be instances like the software cant write to a specific folder or file. It can be used for cache or any such temporary stuff. Then you need to change the permissions. Simply give write permissions to the user www-root. However this situation may be unlikely. e..g we have to change the permissions of folder /var/www/wordpress/cache

sudo chmod -cR u+w /var/www/wordpress/cache