Saturday, 7 February 2015

Cool tricks in vi editor, convert to upper case, convert to lower case, removing ^M characters etc

Hi there,

In this post, I would post commands/procedures which we use less frequently in our (DBA) job but they are very handy. 

I will be updating this post on regular basis and hopefully with inputs from readers.

To convert all alphabets to upper case in a file, use below string.
Hit 'Esc' and then type:%s/.*/\U&/

To convert all alphabets to lower case in a file, use below string.
Hit 'Esc' and then type:%s/.*/\L&/ 


To delete all the lines matching a pattern in a file, inside vi editor

Command$ Hit 'Esc' and then type :g/<pattern>/d

to delete control M (^M) characters in a file inside vi editor
Command$ :%s/(Ctrl V + Ctrl M)//g

to find and replace word by word
Command$ :%s/existingpattern/ newpattern /gc

Sunday, 20 July 2014

Examples of find command in Linux/UNIX

find command and its usage

When working on Linux/UNIX OS, most of the times we will come across a situation where in we need to search for a specific file and perform various operations. find command is a very handy option in this situation and I will be posting about the find command with syntax and usage with examples.


find command
  1. basic general syntax of the find command
syntax$ find <location> –name filename

  1. to find a file by name “filename”
Command$ find / -name filename
Here the location is “/” which will search in the entire filesystem. You can change this to desired location

  1. to find a file which matching a specific pattern (all files with current PID $$)
Command$ find <location> -name *.$$

  1. to find a file which doesn’t match a specific pattern ( ! does the trick here)
Command$ find <location> ! -name *.$$

  1. to find a file “file.$$” at a location “dbatmpdir” and display
contents of it ($$ == Process id).
Command$ find <location> -name file.$$ -exec cat {} \;
Here 1. find command will find the file first
        2. then give it as an argument to cat {} which will execute it using “exec” and
        3. \; is the syntax for completion.

Similarly to find a file matching specific criteria and then delete it
(Here the criteria is *.$$ i.e. all files in specific location (<location>) ending with pid $$)
Command$ find <location> -name *.$$ -exec rm {} \;

  1. find command lists all types of files. To find only plain files matching a specific pattern and then delete use -type
Command$ find <location> -type f -name *.$$ -exec rm {} \;

  1. to find files which are older than 7 days matching a specific pattern and then delete
use -type and -mtime

Command$ find <location> -type f -mtime +7 -name *.$$ -exec rm {} \;

  1. to grep for a pattern in multiple files in current directory
Command$ find . -type f -name "*" -exec grep -in "pattern" {} \;

  1. problem with above command is, it prints all the matching lines but will never tell in which file the match line exists. In order to print the file name add –print to above command
Command$ find . -type f -name "*" -print -exec grep -in "pattern" {} \;

  1. to find files of specific pattern and copy them to different location

Command$ find . -name "*pattern*" -exec ls {} \; 2>/dev/null | cpio -pdumv path