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