Posts Tagged ‘unix’

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.

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.

How to Change Hostname in Unix FreeBSD

Thursday, June 3rd, 2010

We have interesting hostname for our all our servers. Some of it is using Football Team like Manchester, Liverpool, Chelsea, Arsenal. Part of it is striker name like Rooney, Ronaldo and etc. We also have like meganfox, milla, jessicaalba for those “hot” “steamy” servers. Occasionally if the football team is not perform well, we will change the hostname for the server to other football team, for example Manchester to Chelsea. Yea.. We are not loyal support, we only support the no. 1 team.

Joking aside, how to change server hostname in Unix FreeBSD? There are 2 ways to do it, one is the easy way another is the geeky way. Depend on yourself, if you want to show your elite skills, do it with the text editor, or make the change with Sysinstall.

Change Hostname in Unix FreeBSD with Sysinstall

  • In command line, type: sysinstall
  • Select Configure
  • Select Networking
  • Select Interfaces
  • Select your Network Interface Card (em0 em1 fxp0)
  • No IPV6 (Select yes if you are running on ipv6)
  • No DHCP (Select yes if you are running on dhcp)
  • host: should be your server name like chelsea
  • domain: should be your own domain like takizo.com
  • Select Ok
  • Exit, Exit, Exit
  • In command shell, type: hostname chelsea.takizo.com

This will update your new hostname in Unix FreeBSD box. But please take note you will have extra junk in /etc/rc.conf. So it’s recommend to change your hostname in Unix FreeBSD with following method.

Change Hostname in Unix FreeBSD in “Clean” Way

  • In command shell, type: hostname chelsea.takizo.com
  • Edit /etc/hosts with vi, change the existing hostname to your new hostname
  • Edit /etc/rc.conf with vi, on the “hostname” variable change the existing hostname to your new hostname

This is the better approach in order to organize your /etc/rc.conf clean and clear.

How to Change User’s Shell Environment in FreeBSD

Wednesday, May 26th, 2010

In FreeBSD, user’s default shell environment is either sh or csh and I have installed Bash in FreeBSD and would like to change user’s shell environment to Bash.

To change user’s shell environment in FreeBSD.


shell> chsh -s /usr/local/bin/bash userid

To change your shell environment in FreeBSD


shell> chsh -s /usr/local/bin/bash