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.
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:
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 nameFor 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 filesIf 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 -
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
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
If you want to truncate/zero a log file to which you don't have write access, you can do:
sudo truncate -s0 logfile
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
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);
}
});