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.

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

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

Advertisements

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

or

# sysctl net.ipv4.ip_forward=1

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

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

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 192.168.1.1 -p tcp –dport 1111 -j DNAT –to-destination 2.2.2.2:1111

or for a whole network

# iptables -t nat -A PREROUTING -s 192.168.1.0/24 -p tcp –dport 1111 -j DNAT –to-destination 2.2.2.2:1111

that’s it, now the traffic to port 1111 will be redirected to IP 2.2.2.2 .
If you go on host 2.2.2.2, 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

Run:

$ 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.