Monday, February 6, 2012

Determine file size with du

The du command ( disk usage ) gather and summarize about how much your disk space being used by your file and the disk space being use by directory in the Linux system The du command can be use to find the size of file and the size of directory in Linux system.

du takes a single argument, specifying a pathname for du to work; if it is not specified, the current directory is used. The SUS mandates for du the following options:
-a, display an entry for each file (and not directory) contained in the current directory
-H, calculate disk usage for link references specified on the command line
-k, show sizes as multiples of 1024 bytes, not 512-byte
-L, calculate disk usage for link references anywhere
-s, report only the sum of the usage in the current directory, not for each file
-x, only traverse files and directories on the device on which the pathname argument is specified.
du is flexible and offers a variety of options. For example, it allows you to determine the size of the current directory and all subdirectories:
$ du -sh

Using du by itself displays the size of every subdirectory under the current directory--it summarizes and reports the data in human-readable format, such as 402 MB instead of 411012 bytes. You can also see the size of every file in every subdirectory by incorporating the "-a" option:
$ du -ah

In addition, you can use du in combination with grep to find a particular file size. If you're searching in a directory and suspect a file is growing extremely large, reduce the output of du to all files and directories 1 GB in size or larger with this command:
$ du -ah|grep -e '[0-9]G'

Examples 

Sum of directories in kilobytes:

 $ du -sk *
 152304  directoryOne
 1856548 directoryTwo
Sum of directories in human-readable format (Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte):
 $ du -sh *
 149M directoryOne
 1.8G directoryTwo
disk usage of all subdirectories and files including hidden files within the current directory (sorted by filesize) :
 $ du -sk .[!.]* *| sort -n
disk usage of all subdirectories and files including hidden files within the current directory (sorted by reverse filesize) :
 $ du -sk .[!.]* *| sort -nr
The weight of directories:
 $ du -d 1 -c -h

More information on du command:
# info du
# man du
# du --help

Thursday, February 2, 2012

node-static for Serving Static Files with Node.js

node-static can be installed via npm with the following command:
npm install node-static
To use it: include the module in your script via the require function, create a server, pass it your preferred options, and tell it to serve files:
var static = require('node-static'),
  http = require('http'),
  util = require('util');
var webroot = './public',
  port = 8181;
var file = new(static.Server)(webroot, {
  cache: 900,
  headers: { 'X-Powered-By': 'node-static' }
});
http.createServer(function(req, res) {
  req.addListener('end', function() {
    file.serve(req, res, function(err, result) {
      if (err) {
        console.error('Error serving %s - %s', req.url, err.message);
        if (err.status === 404 || err.status === 500) {
          file.serveFile(util.format('/%d.html', err.status), err.status, {}, req, res);
        } else {
          res.writeHead(err.status, err.headers);
          res.end();
        }
      } else {
        console.log('%s - %s', req.url, res.message);
      }
    });
  });
}).listen(port);
console.log('node-static running at http://localhost:%d', port);
Here, when we create a new instance of a node-static server, we pass it:
  • the directory we want it to serve files from by way of the `webroot` variable (defaults to serving files from the current directory unless you tell it otherwise)
  • a cache value of 900, so each file served will be cached for 15 minutes (default value for cache is 3600, meaning files are cached for 1 hour)
  • a custom ‘X-Powered-By’ header (I’ve used X-Powered-By solely as an example – you may, or may not, want to disclose the software you’re server is running)
We then use the http module’s createServer function, and add an event handler for the request’s end event where we use our instance of node-static to serve the files.
When we call file.serve, we pass it an optional callback function that let’s us customise how we handle any errors:
  • we check err.status, and serve either 404.html or 500.html accordingly
  • if there’s an error and the status code is something other than 404 or 500, we send the status code and the headers back to the client explicitly – when you pass a callback function to file.serve and there is an error, node-static will not respond to the client by itself.