Blog Spot!


How to change the output color in linux terminal

You can use these ANSI escape codes:

Black        0;30     Dark Gray     1;30
Red          0;31     Light Red     1;31
Green        0;32     Light Green   1;32
Brown/Orange 0;33     Yellow        1;33
Blue         0;34     Light Blue    1;34
Purple       0;35     Light Purple  1;35
Cyan         0;36     Light Cyan    1;36
Light Gray   0;37     White         1;37

And then use them like this in your script:

RED='\033[0;31m'
NC='\033[0m' # No Color
printf "I ${RED}love${NC} Stack Overflow\n"

which prints love in red.

stackoverflow

Added on 15.Jan.2016
Tags: linux terminal bash cmd scripts

How do I compress a Whole Linux or UNIX Directory - tar

It is very easy to compress a Whole Linux/UNIX directory. It is useful to backup files, email all files, or even to send software you have created to friends. Technically, it is called as a compressed archive. GNU tar command is best for this work. It can be use on remote Linux or UNIX server. It does two things for you:

  • Create the archive
  • Compress the archive

You need to use the tar command as follows (syntax of tar command):

tar -zcvf archive-name.tar.gz directory-name

Where:

  • -z Compress archive using gzip program
  • -c Create archive
  • -v Verbose i.e display progress while creating archive
  • -f Archive File name

For example, say you have a directory called /home/jerry/prog and you would like to compress this directory then you can type tar command as follow:

$ tar -zcvf prog-1-jan-2005.tar.gz /home/jerry/prog

Above command will create an archive file called prog-1-jan-2005.tar.gz in current directory. If you wish to restore your archive then you need to use the following command (it will extract all files in current directory):

$ tar -zxvf prog-1-jan-2005.tar.gz

Where:

  • -x Extract files

If you wish to extract files in particular directory, for example in /tmp then you need to use the following command:

$ tar -zxvf prog-1-jan-2005.tar.gz -C /tmp
$ cd /tmp
$ ls -

A note about non gnu/tar command

The above syntax use GNU tar command for compressing and uncompressing tar files. If your system does not use GNU tar, you can still create a compressed tar file, via the following syntax:

tar -cvf - file1 file2 dir3 | gzip > archive.tar.gz

cyberciti

Added on 14.Jan.2016
Tags: linux zip gzip tar compress terminal archive

How to execute multiple commands in a single line

Execute Command A, then execute Command B (no evaluation of anything)

Command A & Command B

Execute Command A, and redirect all its output into the input of Command B

Command A | Command B

Execute Command A, evaluate the errorlevel after running and if the exit code (errorlevel) is 0, only then execute Command B

Command A && Command B

Execute Command A, evaluate the exit code of this command and if it's anything but 0, only then execute Command B

Command A || Command B

Added on 14.Jan.2016
Tags: linux terminal exec

Truncating a file while it's being used in Linux

If you want to truncate/zero a log file to which you don't have write access, you can do:

sudo truncate -s0 logfile

serverfault

Optionally you can backup the log file put the current date in the file name and then truncate it. You can do this with this simple command:

tar -zcvf $(date +"%y-%m-%d")"_file.tar.gz" file.log && truncate -s0 file.log

...output example: 16-01-14_test.tar.gz

Added on 14.Jan.2016
Tags: truncate terminal log logfile linux

Node JS disk space module - *nix only

Find the total and free HDD memory.

var exec, path;

exec = require('child_process').exec;
path = require('path');

function check(drive, callback) {
  var total, free, status, error, lines, strDiskInfo, diskInfo;

  total  = 0;
  free   = 0;
  status = null;

  if (!drive) {
    status = 'NOTFOUND';
    error  = new Error('Necessary parameter absent');

    return callback ? callback(error, total, free, status) : console.error(error);
  }

  exec("df -k '" + drive.replace(/'/g,"'\\''") + "'", function(error, stdout, stderr) {
    if (error) {
      status = (stderr.indexOf("No such file or directory") != -1) ? 'NOTFOUND' : 'STDERR' ;

      if (callback) {
        callback(error, total, free, status);
      } else {
        console.error(stderr);
      }

    } else {

      lines       = stdout.trim().split("\n");
      strDiskInfo = lines[lines.length - 1].replace( /[\s\n\r]+/g,' ');
      diskInfo    = strDiskInfo.split(' ');

      total  = diskInfo[1] * 1024;
      free   = diskInfo[3] * 1024;
      status = 'READY';

      if (callback) {
        callback(null, total, free, status);
      }

    }
  });
}

module.exports = {
  check: check,
};

usage:

var disk = require('./free');

disk.check("/", function(error, total, free, status) {
  if (!error) {
    console.log(total);
    console.log(free);
    console.log(status);
  }
});

Added on 11.Jan.2016
Tags: nodejs js disk linux

Search


PHP Libraries


Carbon lib / docs
Idiorm lib / docs
Image Workshop lib / docs
lorenzos/Minixed lib / docs
Parsedown lib / docs
PHP Paginator lib / docs
PHP Redis lib / docs
QrCode lib / docs
Requests lib / docs
Slim lib / docs
Spyc lib / docs
TWIG lib / docs
Upload lib / docs
Validation lib / docs
Zebra Image lib / docs

JS Libraries


AJV lib / docs
BackboneJS lib / docs
Bootstrap Notify lib / docs
C3.js lib / docs
ChartJS lib / docs
FastMD5 lib / docs
HighlightJS lib / docs
jQuery-Storage lib / docs
JS-Cookie lib / docs
Leaflet JS lib / docs
LowDB lib / docs
Marked lib / docs
NeedlyJS lib / docs
ParcelJS lib / docs
RequireJS lib / docs
Swig lib / docs
Toastr lib / docs
Underscore lib / docs
ValidateJS lib / docs
top