PHP and Web Technologies Freak
This article explains 2 simple commands that most people want to know when they are using Linux. You can find the size of a directory and find the amount of free space, which is on your computer. The command you use to specify the directory size is “du”. And, to the free space you can “df.”
All information in this article can be found in the man pages for du and df. In case you get bored reading the man pages, and you want your work quickly, then this article is for you.
‘du’ – find the size of a directory
$ duEnter the above at the command prompt, you can view a list of directories in the current directory with their sizes. The last line of output gives you the size of the current directory including subdirectories. The size given the size of the files and directories in the current directory and all its subdirectories. Note that by default are the size in kilobytes.
$ du / home / david
The above command would be the directory, size of the directory / home / david
$du -h
This command gives you a better outcome than standard. The option “-h” stands for the human-readable format. So that the size of the files and directories are this time with a suffix ‘K’ if its kilobytes and “M” if its Megabytes and ‘G’, if its gigabytes.
$du -ah
This command would be in his performance, not just the directories, but also all the files in the current directory. Note that “you” always counts all files and directories, with the final size in the last line. But the “-a” shows the file name along with the directory name in the output. ‘-h’ is re-readable form.
$du -c
This gives you a large sum in the last line of output. So, if your directory is 30MB the last 2 lines of output would be
30M. 30M total
In the first line will be the default last line of the “du” output, indicating the size of the list and a further display of the same size, followed by the string “Total”. This is useful if you run this command with the command grep only the final size of a directory, as shown below.
$ du -ch | grep total
This would have only one line in the output shows the size of the current directory including all subdirectories.
Note: If you are not familiar with the lines (around the above command possible) refer to Article No. 24 Grep is one of the most important commands in Unix. See Rule No. 25 to learn more about grep.
$du -s
This shows a summary of the directory size. It is the easiest way to know the size of the current directory.
$du -S
This would increase the size of the current directory without the size of the subdirectories in that directory. So it basically shows the total size of all files in the current directory.
$du --exculde = mp3
The above command would be the size of the current directory with all subdirectories, but it would make all files with the given pattern in their filenames. Thus in the above case, if it happens, all MP3 files in the current directory or its subdirectories, their size is not taken into account when calculating the total list size.
‘df’ – finding the disk free space / disk usage
$dfEnter the above is a table consisting of 6 columns. All columns are very easy to understand. Remember that the “size”, “Used” and “free” columns use kilobytes as a unit. The “Use% column shows the use as a percentage, which is also very useful.
$df -h
Shows the same performance as the previous one, but the command “-h” shows human-readable format. Therefore, instead of kilobytes as the unit of output would have been “M” for megabytes and “G” for gigabytes.
The majority of users are not on the other parameters, which are to “df”. So I will not discuss.
I will again show you an example that I am on my computer. I have actually saved this as a script called “use” because I think it often.
Example:
I have my Linux installation on / dev/hda1 is mounted and I have my Windows partitions as well (by default every time Linux boots). So ‘df’ default shows me the disk usage of my Linux and Windows partitions. And I’m only in the hard disk using the Linux partitions. That is what I use:
$df-h | grep / dev/hda1 | cut-c 41-43
This command displays the following on my computer
45%
Basically, this command “df” which uses all the disk partitions and then extracts the lines with / dev/hda1, because I am only in this. Then cut the characters from the 41st to 43 Column, since the columns, the use in%, that is what I want.