10 must know find and replace command in UNIX


In Unix often we are asked to perform certain things which requires us to find a particular pattern and then to play or manipulate that particular pattern, so now today let us learn how to be a master in that. The list here present 10 must know find and replace command in Unix with explanation.

1. Finding any extension files:

Our first example is how to find a set of files and then play with the files, to play we mean to manipulate the files. The first example deals with finding files which are older than certain time or heavier than certain, we do this most often with log files. Now lets find files which are 15 days old.

We can find files with any extension with *.* feature and then we can find with respect to what option we use with it, for example we will use the mtime option here, mtime is for modification time.

find *.* -mtime -15

find does the recursive search as well via find / -name file name we know that / searches from the root.

2. Finding particular set of files:

find . -name “*.c” -print.

. (dot) searches in current directory and will give relative path.*.c will give only the c extension files. Finding files which are one year old will be found by the below command, so the files which are not accessed from a year will be as below.

3. Finding old files with access time:

Find $HOME will search in the home directory

find /home -atime +365 -print

4. Finding and  negating:

Now the below command will select everything but the c files, so this option negate what we specify and find everything other than that.

find .! -name “*.c” -print

5. Other options in Find:

Now we can also use options with the find command. For example the size option will find the files in order of its size, the inum with the inode number details and type f with finding only the files. Example :

-size +2048 ;    -inum 12345 ;    -type f.

6. Deleting old files:

Now as we have already found out the files we will play with them, lets delete them first. This will be majorly helpful when we want to delete the old logs which are a month old. In this example we are deleting files older than 15 days.

find *.* -mtime -15 -exec rm -rf {}\;

7. Finding a pattern:

Now if we want to find a particular pattern in all files using the find command we can certainly use it to our ease. In the below example we will find the pattern sheron.

find . -type f -exec grep “sheron” {} \; -print

8. Finding and replacing:

Now we can replace the pattern we have found with multiple options, one of them is using perl. In the below example we will replace the pattern sheron with pattern gerard using perl.

find /path/to/start/from/ -type f | xargs perl -pi -e ‘s/sheron/gerard/g’

9. Finding and replacing in multiple files:

The below example finds the pattern sheron in all text files and then in all those files replaces the pattern sheron with the pattern gerard with the use of strean editor. The grep -il statement finds all instances of gerard in files ending in txt and i = ignore case, l = only list the filename. The file names are passed to sed, which runs a regular expression to change all instances of sheron to gerard. Since sed doesn’t overwrite a file, I redirected the output to a temp file and then renamed it back to the original file name.

for file in $(grep -il “Sheron” *.txt)
sed -e “s/Sheron/Gerard/ig” $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file

10. Deleting the carrot M character:

Now our very own find can also be used to t replace ^M character which is introduced in our files when we export files from windows to Unix. Now the below example uses a simple loop to do the same.

for i in `find . -type f` ; do dos2unix $i $i; done

Enjoy playing with files and patterns

How to setup Boot password protection for Grub2 Entries

Since Ubuntu 9.10 uses Grub2 as default boot loader,we cannot use the previous way to set password protection for grub entries.Grub 2 currently supports unencrypted password protection. Encrypted password protection using PBKDF2, as well as password scripting, is currently under development.
This post comes from Ubuntuforums and shows how to set up basic password protection. No user will be able to access the system unless the designated username and password specified in /etc/grub.d/00_header are entered.


* Grub 2 has the ability to set password protection on individual menuentries and/or for specific users. Although multilevel access by more than one user is possible, it has not yet been automated and is beyond the scope of this guide. I will try to create a specific Password HOWTO as time permits.
* The username and password will also be required to gain access to the Grub 2 command line and menu editing modes.
* The username and/or password do not have to be the same as the Ubuntu logon name/password.
* This is basic password security. The name/password are unencrypted; anyone having physical access to the machine and more than an elementary knowledge of how Linux works will be able to access the configuration files and bypass this feature.
* Grub 2 password protection is still evolving. Currently (Grub 1.97beta4) password protection must be assigned to each menuentry as described below. There is a chance the password feature will be revised so that all entries are protected by default. If and when this feature is incorporated in Grub 2, password protection can be eliminated for a specific menuentry by adding “(–unlock)” on the menuentry line. More information will be posted here as it becomes available.

Warning: Errors in creating a password-protected Grub 2 menu may result in an unbootable system. To restore a system with broken passwords, access and edit the Grub 2 configuration files using the LiveCD or another OS.

Now,let’s start following steps to create password protection.You’d better make a backup before changing a file.
Add the following the bottom of /etc/grub.d/00_header

cat << EOF
set superusers=”myname”
password myname 1234

here “myname” and “1234″ after “password” are the username and password you need to type to access grub entries.Change them to what you want.
Change the following in /etc/grub.d/10_linux to password protect Linux installations on the main partition:

menuentry “$1” {


menuentry “$1” –users myname {

Note:This is what I change in my 64-bit machine:

printf “menuentry \”${title}\” –class linux –users myname {\n” “${os}” “${version}”

Change the following in /etc/grub.d/20_memtest to password protect the memtest86+ option:

menuentry “Memory test (memtest86+)” {


menuentry “Memory test (memtest86+)” –users myname {

Additional memtest86+ entries (from other partitions) may also be located in this file. The line will start with “menuentry”. Change these lines as desired.
Change the following in /etc/grub.d/30_osprober to password protect kernels/operating systems on other partitions.Look for any line in /etc/grub.d/30_osprober which begins with “menuentry”.

menuentry “${LONGNAME} (on ${DEVICE})” {

menuentry “${LLABEL} (on ${DEVICE})” {

Make the change as described in previous step.
Finally,requiring a password for every menuentry in a file can be added using the following command:

sudo sed -i -e ‘/^menuentry /s/ {/ –users myname {/’ /etc/grub.d/10_linux  /etc/grub.d/20_memtest86+ /etc/grub.d/30_os-prober /etc/grub.d/40_custom

To undo this command:

sudo sed -i -e ‘/^menuentry /s/ –users myname {/ {/’ /etc/grub.d/10_linux  /etc/grub.d/20_memtest86+ /etc/grub.d/30_os-prober /etc/grub.d/40_custom

Save the files, run sudo update-grub, and reboot. At the Grub 2 menu, you will be presented with the normal menu. When you make a selection, a prompt will ask for the username and password.

Install MacOS theme Mac4Lin in Ubuntu 9.10(Karmic)

Mac4Lin brings the Mac OS X user interface to POSIX Operating Systems (GNU/LInux, FreeBSD, openSolaris etc.). The project is built for desktop environments based on GTK.
This tutorial shows how to install this Mac theme in ubuntu karmic and do some easy configurations.
Download and install Mac4Lin
First,let’s download the source package from:http://sourceforge.net/projects/mac4lin/files/

I download the version1.0,and my firefox store the download files in ~/Downloads.

So open up the command line console from Applications->Accessories->Terminal,type following to navigate to the folder:

cd ~/Downloads

and then uncompress and install it:

unzip Mac4Lin_Install_v1.0.zip
cd Mac4Lin_Install_v1.0/

it will ask you some questions for yes/not to install something,read the question and make your choice.
Note:There if you type yes for this question:”Do you want to enable Metacity Compositor? If you are unsure about this or use Compiz, type ‘n’ [y/n]?”.You will unable to enable compiz 3D effects again if you close them.You need to run this command:


navigate to /apps/metacity/general,uncheck “compositing_manager” in right box.Log out and back in,then you can enable compiz 3D effects again.

Configure Ubuntu desktop

1.There are two Mac OS X wallpapers in “Mac4Lin_Install_v1.0/Wallpapers” folder.Right click on desktop and select “Change Desktop Background”,in Background tab click Add… and navigate to choose this two wallpapers.
2.If you are operating on 64-bit ubuntu,you need to manually install the AWN theme.In following AWN Manager window,click add and select Downloads/Mac4Lin_Install_v1.0/AWN/Mac4Lin_AWN_v1.0.tgz.

3.Delete gnome default panels.I have installed AWN and make it auto-run at login,default gnome panels are useless.Run this command:


navigate to

/desktop/gnome/session, double click “required_component_list” in right box and delete “panel”.

4.Min/Max/Close buttons are default in left of windows’ title bar.If you would like to move them to right,run this command again:


navigate to app->Metacity->general,double click “button_layout”in right box and change its value to “menu:minimize,maximize,close” (without quotes).

Put Home, Computer, Network and Trash Icons on Ubuntu Desktop

f you used to be a Windows user,you should prefer the Computer,Home,Network and Trash icons on your ubuntu gnome desktop.
Press Alt+F2 key combination,and this window pop up,type gconf-editor to start configuration editor.

Navigate to apps/nautilus/desktop in next window,check the marked four checkboxes and change their icon-name as you want.


Sharevar el = document.getElementById(’share-link-845113745′);el.params = {title: ‘Put Home,Computer,Network,Trash […]

How to reset forgotten user/boot password in Ubuntu

If you forgot your ubuntu user password,or you have enabled root user and forgot the password,you can try following to reset the forgotten password.

First,start your machine and when it boots into grub menu,select “Ubuntu GNU Linux …. (recovery mode)” (just use up/down arrow key to highlight it,not press enter).

Press E to get into edit screen,there  find ro single and change it into rw single init=/bin/bash,then press Ctrl+x or b to boot into a command line.

type following command:

mount -a
passwd your-user-name

it will prompts to input new password.If you want to reset the root password,change the second command to passwd root.
Now,restart and login with the new password.

How-To: Redirecting network traffic to a new IP using IPtables

While doing a server migration, it happens that some traffic still go to the old machine because the DNS servers are not yet synced or simply because some people are using the IP address instead of the domain name….

By using iptables and its masquerade feature, it is possible to forward all traffic to the old server to the new IP.

This tutorial will show which command lines are required to make this possible.

In this article, it is assumed that you do not have iptables running, or at least no nat table rules for chain PREROUTING and POSTROUTING.

The first thing to do is do enable IP forwarding. This is done either by using:

# echo “1” > /proc/sys/net/ipv4/ip_forward


# sysctl net.ipv4.ip_forward=1

Then, we will add a rule telling to forward the traffic on port 1111 to ip on port 1111:

# iptables -t nat -A PREROUTING -p tcp –dport 1111 -j DNAT –to-destination

and finally, we ask IPtables to masquerade:

iptables -t nat -A POSTROUTING -j MASQUERADE

Optionally, you could only redirect the traffic from a specific source/network with, for a host only:

# iptables -t nat -A PREROUTING -s -p tcp –dport 1111 -j DNAT –to-destination

or for a whole network

# iptables -t nat -A PREROUTING -s -p tcp –dport 1111 -j DNAT –to-destination

that’s it, now the traffic to port 1111 will be redirected to IP .
If you go on host, you should see a lot of traffic coming from the host doing the redirection.

How-To: Changing the default text editor

There is a few software that will use the editor command to find out what text editor to use. Example commands will be dch to add a new .deb changelog entry, revision control softwares when prompting for a commit message …

There is basically 2 ways for changing the default editor:

  • System wide
  • At user level

1. System Wide


$ sudo update-alternatives –config editor

And then choose whichever editor you want to be default.

2. User Level

As a user, you cannot change the setting for the whole system, but you can add an alias for editor to let say vim.

Open and edit ~/.bashrc and add:

alias editor=vim

Next time you will open a bash prompt, your default editor will be vim.