Cool Unix Tricks for the Ellucian BannerĀ® DBA/Admin (Part 1)

Many times, knowing a single Oracle/Unix command can literally save you untold hours of anguish and suffering. In addition, Unix/Linux sysadmins and Oracle DBAs often learn these tricks by the most unconventional means.

Sometimes, you're just sittlng next to a Unix Guru who happens to show you that "magical incantation", other times you simply notice something while passing by a website.

In any event each SysAdmin/DBA is successful, at least in part, to the respective "bag of tricks".

With that, here are a few Unix/Linux tricks that should be helpful to the Banner® Admin/DBA

1. Tar-ing across different hosts

Often when we need to migrate a large group file from one host to another we will:

  • tar the files under a directory (e.g. /u01/myfiles in a giant tarball (/tmp/myfiles.tgz on the source host (A)
  • transfer the tarball from the source host to the target (B)
  • extract the tarball on the target
   #   on host A
$ tar -zcf /tmp/myfiles.tgz /u01/myfiles
$ scp /tmp/myfiles.tgz
   #   unless you have SSH keys setup, you will be prompted for a password on that last command 
   #   on host B
$ cd /; tar -zxf /tmp/myfiles.tgz

While this definitely works, it requires three separate commands, and requires additional space to create the tgz file on both hosts. Many people don't realize this, but you can redirect STDIO across SSH and we can use this to tar directly across hosts:

  # syntax for this command is: tar -cf - [source files directory] | ssh username@target_host "(cd [target directory] ; tar -xf -)"
$ tar -cf - /u01/myfiles | ssh "(cd / ; tar -xf -)"
 # by doing this we dont ever create the tgz file on disk, which could be vital when your mount points are getting full
 # in addition this command would get you A LOT of "street credibility" from the hackers!

2. Find and replace using sed

Most people know that vi will allow you to do a find and replace. This is fine if you only have a few files to fix.  However, this is tedious for multiple files. If you have a large group of files in which you want to do a find/replace you can use the sed command:

  # syntax is: sed -i -e 's/[search string]/[replace string]/g' [filename]
  # replace all instances of #UPDATEME# to u_pick_it in file login.sql
 $ sed -i -e 's/#UPDATEME#/u_pick_it/g' login.sql

3. Square Paste of text files

All Unix admins know how to vertically paste two files together using 'cat' (ie 'cat file1 file2 > file3'), but you can also horizontally paste files together using 'paste'. When would you use this? Suppose you have a great big list of files in a directory all with a *.txt extension, and you need to change the extension of txt to rtf for all files, leaving the file prefix unchanged (and this is NOT DOS so 'mv *.txt *.rtf' will not work). How would you do this? Well, one way is to:

  • list all files to a text file A,
  • copy file A to file B
  • do a find/replace of txt->rtf using sed
  • "PASTE" the two columns together using the "paste" command
 # you have three files in directory
$ ls 
a.txt b.txt c.txt
$ ls *.txt > A
$ cp A B
# the next command inserts an 'mv' at the beginning of each line in file A ^ is the beginning of the line character
$ sed -i -e 's/^/mv /g' A
$ cat A
mv a.txt
mv b.txt
mv c.txt
# now change all *.txt to *.rtf in file B
$ sed -i -e 's/.txt/.rtf/g' B
$ cat B
$ now you can paste the files together using paste: syntax = "paste [file 1] [file 2] > [output.file]
$ paste A B > C
$ cat C
mv a.txt a.rtf
mv b.txt b.rtf
mv c.txt c.rtf
# you can then run file C
$ sh +x C

4. Mass rename of files

The previous example of mass file rename was used to give an example for a square paste. However actually using the command would get you zero respect from hacker circles. It's tedious, generates temp files, and is all around UGLY.

Real "Unix hackers" would run a for loop over all the files you want to rename, then do a mv using a cut of the extension.

$ ls *
1.txt	2.txt	3.txt
# run a for loop over all *.txt files, then mv the files to the file prefix with nothing after the dot + an rtf erxtension
# yes this command is a mess, but it does look cool!
$ for i in `ls *.txt`; do mv $i `echo $i | cut -d. -f1 `.rtf; done
$ ls *
1.rtf 2.rtf 3.rtf