Saturday, July 21, 2012

10 must know find and replace command in UNIX


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 rahul.
find . -type f -exec grep “rahul” {} \; -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 rahul with pattern miglani using perl.
find /path/to/start/from/ -type f | xargs perl -pi -e ‘s/rahul/miglani/g’

9. Finding and replacing in multiple files:

The below example finds the pattern rahul in all text files and then in all those files replaces the patternrahul with the pattern miglani with the use of strean editor. The grep -il statement finds all instances of rahul 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 rahul to miglani. 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 “Rahul” *.txt)
do
sed -e “s/Rahul/Miglani/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 :) .

No comments:

Post a Comment