Archive for the ‘Systems’ Category

How to Check Linux Version and Distro

Wednesday, July 21st, 2010

Who run more than 10 Linux servers with different version of Linux Distro? We all know there are many type of Linux distro such as Ubuntu, Centos, Gentoo, Fedora, debian, OpenSUSE and etc. Every single distro runs on different version and etc. Sometime I am having problem to remember what Linux distro is running on my DNS server or Mail Server, and what Linux version is it on now. There are two methods to find linux version on your server.

To check Linux distro on your server


shell> cat /etc/issue

To check Linux Version and distro on your server


shell> cat /etc/*-release

Linux/Unix Search and Replace Text from Multiple Files

Wednesday, July 14th, 2010

In Linux/Unix, for newbie to do search text from multiple files/folder look like difficult, replacing string from file sound even harder. It’s actually not hard or complicated to search and replace text from multiple files in Unix/Linux. The key is what command to use, there will be two main commands involve in search and replace, which are find and sed

For example I am hosting multiple web applications, the development might have several pg_connect command to connect database, in the event I am migrating database to other IP address, it might be hard for them to dig the file and replace the IP Address with new IP Addresses.

In Linux/Unix, you can search through the directories, looking for the files and replace it with the new IP address. Here is the sample.


shell> find /path/to/dir/* -type f -exec sed -i .backup20100714 -e "s/host=x.x.x.x/host=y.y.y.y/g" {} \\;

The command above will search for files in the directory.
find -type f mean look for File only.
find -exec is to execute something from returned results.
sed -i means backup the files, in case you would like to restore later.
sed -e “s/host=x.x.x.x/host=y.y.y.y/g” is to replace x.x.x.x to y.y.y.y

You have backup files name filename.php.backup20100714, now what you need to do is move the files in a backup directory.


shell> find /path/to/dir/* -name "*.backup20100714" -exec mv {} /your/backup/dir/ \\;

You might want to explore on find command, it can do a lot of cool stuffs.

Perl, Trim White Space from a String

Tuesday, July 13th, 2010

When we grep a bunch of string, there are white spaces where we wish to trim, especially the white space at the beginning and the end of a string. There is no trim function in Perl like trim in PHP. The Perl function below should help you to trim the string.


#!/usr/bin/perl

sub trim($);
sub ltrim($);
sub rtrim($);

sub trim($)
{
	my $trim_string = shift;
	$trim_string =~ s/^\s+//;
	$trim_string =~ s/\s+$//;
	return $trim_string;
}

sub ltrim($)
{
	my $trim_string = shift;
	$trim_string =~ s/^\s+//;
	return $trim_string;
}

sub rtrim($)
{
	my $trim_string = shift;
	$trim_string =~ s/\s+$//;
	return $trim_string;
}

my $long_string = "  \t  foo foo        bar \t \t bar          ";
print trim($long_string);

Modify or Adjust File Date Time on Unix/Linux

Saturday, July 10th, 2010

You have folders or files in your server last modified date/time was 3 months ago, sometime for some reason we would like to change files or folders last modified date/time to older or recent date. You can modify the date/time of a folder or file with Unix/Linux touch command.

For example I have a file name wireless.pcap, last update or created was in year 2009. I can change the last modify time by the touch command touch -t yyyymmddhhmm filename

shell> touch -t 201007101540 wireless.pcap

This will change the file last updated date and time to 10th July 2010 15:40pm.

How to Create User with useradd in FreeBSD

Friday, July 9th, 2010

You can invoke “adduser” command in FreeBSD in order to create new user. Adduser will prompt you for user info like name, uid, gid, shell environment and etc. But sometime we would like to create user access through Bash script. “useradd” command is the good way to do it.

In Linux, using “useradd” is pretty common for creating new user access. You can also use “useradd” in FreeBSD, but not directly invoke “useradd” command. Below is the sample of creating new user in FreeBSD with “useradd”.


shell> pw useradd cheryl -c 'Cheryl Windows Admin' -d /home/cheryl -s /usr/local/bin/bash

New user account is created, but don’t forgot to set password for the account


shell> passwd cheryl

You can run other command like “userdel”, “usermod”, “usershow”, “groupadd”, “groupdel”, “groupmod”, “lock”, “unlock” on FreeBSD by using the “pw” command.