RSS Subscription
Linux Howtos & Tutorials

Enter your email:

Delivered by


NOTE:New tutorials are from LinuxCareer.com

Poll

Do you own or wish to have iPhone?
 


Linux eBooks FREE Download
A guide to programming Linux kernel modules
Introduction to Linux - A Hands on Guide
A Newbie's Getting Started Guide to Linux

Linux from Scratch - Create Your Own Linux System - Free eBook

Linux: The Hacking Solution (v.3.0)

SQLite 3 with PHP Essential Training – Free Video Training Tutorials

This guide will introduce you to the world of GNU/Linux

The GNU/Linux Advanced Administration

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)

Advanced Bash-Scripting Guide

Set up, maintain, and secure a small office email server

Partner Linux Sites:
How-To.LinuxCareer.com
Jobs.LinuxCareer.com
TuxMachines
Monsterb
LinuxBloggers
AdamsInfo
LinuxScrew
All For Linux

List all directories and sort by size

Article Index
1. List all directories and files with relevant size
2. List all directories and sort by size
3. List TOP 10 by size directories
4. List all directories and sort by size bash script

Although days of limited hard drive memory space seem to be over as hard drives to hold data of great size are today available for affordable price. However, if you feel that you do not need a hard drive with of greater capacity and yet your hard drive is full this may be the indication that you have lots of junk data stored on your system. This is exactly my case and I do need to clean up regularly. I prefer to delete data according the size so starting with the biggest directory in my home directory is the first step.


Here are couple bash commands I use for my data clean up routine:

1. List all directories and files with relevant size

This command will list only files and directories in your current working directory. Remove -s option to see a recursive output. -h stands for a human readable output.

$ du -sh *

2. List all directories and sort by size

The previous command is not very useful as it does not sort and prints many unnecessary files which we are not concerned about. The following command prints all directories in current working directory, includes the relevant size in MB and sorts from biggest to smallest.

$ du -m --max-depth 1 | sort -rn

3. List TOP 10 by size directories

Our clean up data day my end by cleaning up first 10 biggest directories we find. So we may only be interested in first the biggest directories. To find which are those lets use a previous command and include head:

$ du -m --max-depth 1 | sort -rn | head -11

4. List all directories and sort by size bash script

Using the command above we can create a bash script to make our life easier. The following bash script will accept 2 arguments. First argument will be a directory name in which we will start our search and the second argument will by a number of directories the script should output.

#!/bin/bash 
 
if [ $# != 2 ]; then 
	echo "Incorrect number of arguments !" >&2 
	echo "USAGE: sortdirbysize [DIRECTORY] <first n directories>" 
fi 
du --block-size=1M --max-depth 1 $1 | sort -rn | head -$2  

Example:

$ ./sort-dir-by-size.sh /home/linux 15

Share this linux post:

Submit List all directories and sort by size in Delicious Submit List all directories and sort by size in Digg Submit List all directories and sort by size in FaceBook Submit List all directories and sort by size in Google Bookmarks Submit List all directories and sort by size in Stumbleupon Submit List all directories and sort by size in Technorati Submit List all directories and sort by size in Twitter
 
Comments for this page are closed !!!
Please visit our new Linux Forum for additional help or discussion.


Linux eBooks FREE Download