Category Archives: UNIX

Unix snippets.

Bash Session Title Bar

 # determine if okay to set colors and termname then does so  # checks copied from default redhat installation /etc/bashrc   # are we an interactive shell?  if [ "$PS1" ]; then      if [ -x /usr/bin/tput ]; then        if [ "x`tput kbs`" != "x" ]; then # We can't do this with "dumb" terminal          stty erase `tput kbs`        fi      fi      case $TERM in        *xterm* | dtterm | *vt100* | *linux* | *cyg* )          [ `uname -s` = "SunOS" ] && TERM=dtterm || TERM=xterm          HOST=`hostname |sed 's/\..*$//g'`          if [ ${HOST} == "unix001" ];                  then BOX="System Test"          fi          PROMPT_COMMAND='echo -ne "\033]0;${LOGNAME}@${HOST} ${BOX} : ${PWD}\007"'          PS1='\[\e[36;1m\]\u@\[\e[32;1m\]\H:\w# \[\e[0m\]'          if echo hello|grep --color=auto l >/dev/null 2>&1; then            export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'          fi          if ls --help 2>&1 |grep color >/dev/null; then            alias ls='ls -h --color=auto'          fi          ;;        *)          PS1='\u@\h \w\$ '          ;;      esac  fi 

C++ version and dependencies

Before calling a compile C++ executable from a script, it’s worth checking you have the version you expect and that all it’s dependencies are in place.

ldd <program_name>     will list all it’s dependencies

file <program_name>     will give you the version, for example

“ELF 32-bit LSB executable 80386 Version 1 [FPU], dynamically linked, not stripped”

Disable SFTP

In a test/UAT environment you might find it prudent to disable nasties like SFTP, FTP or mailx, such that if you’ve copied your scripts and config file over from production, they don’t inadvertently send you test files out to ‘live’ users & systems.

A quick way to do this is create a /bin directory in your user’s root and put in ‘dummy’ executable versions of ftp, mail, mailx and sftp (say with a single echo statement embedded).

Now add this directory to your $PATH so that it gets called before the real commands.

(eg edit ~./profile and add PATH=.:/usr/local/etc:~/bin🙂

Double check with a “which sftp” and it should now point to your dummy version.

Find differences in permissions

What if you have an app installed in several machines, most work fine but one doesn’t!

You’ve done a diff on the directory contents and they match, but it could be that some thoughtful person has chmod’ed one of the executables, but which one?

On each app tree run this:

ls -l  | awk ‘{ print $1″ “$9 }’ > diff_filex.txt

 

Then compare the 2 diff_files with sdiff. The awk returns the permissions (col 1) and the file name (col 9).

Re-delimit a large file

So what if you have an huge file that you cant simply open and do a find and replace on? But you need to change the delimiters so as to process it somewhere else.

We can use sed to accomplish this in UNIX.

sed -e ‘s+,+\*+g’ bigfile.prn > bigfile_newdelim.csv

Then simply repace the current delimiter “~” with a new one, say “,”

sed -e ‘s+~+,+g’ bigfile_newdelim.csv > bigfile.csv

Delete the *newdelim file afterwards I reckon.

.bashrc

If you put this file in your user profile directory (~) it will give you nice colours, make the back key work and set up user defined variables for your session. This is for a Sybase oriented app server.

create a file called .bashrc, then copy in this code

 

export PATH=$PATH:/usr/local/bin/:/usr/ucb/:/usr/sbin/:/usr/local/sbin/

# Set Sybase environment variables
export SYBASE=/opt/sybase/DBAsdk014
export SYBASE_OCS=OCS-15_0
export PATH=/opt/sybase/DBAsdk014/OCS-15_0/bin:$PATH
export LD_LIBRARY_PATH=/opt/sybase/DBAsdk014/OCS-15_0/lib:/opt/sybase/DBAsdk014/OCS-15_0/lib3p64:/opt/sybase/DBAsdk014/OCS-15_0/lib3p:$LD_LIBRARY_PATH
export INCLUDE=/opt/sybase/DBAsdk014/OCS-15_0/include:$INCLUDE
export LIB=/opt/sybase/DBAsdk014/OCS-15_0/lib:$LIB

# Default Apps Setup
export HOSTNAME=`hostname`

# Locale for Sybase
LC_ALL=en_GB
export LC_ALL

# Set term
#export TERM=vt100
export TERM=dtterm

# Set erase character
stty erase ^?
stty erase ^h

# Colourise term
export PS1='\[\e[36;1m\]\u@\[\e[32;1m\]\H:\w# \[\e[0m\]'</p>'</p>'</p> <p># Call set up for title bar</p> <p>source ~/.titlebar</p>'

Process files by timestamp

Sometime we need to delete or move files based on when they were created. Heres some snippets that do just that.

Find everything created in local directory in last 24 hours and copy them out

 

            touch -t 01162011 reference

                        create a reference file with mod date 16 Jan 2011

            find . -newer reference -exec cp {} ./my_sub_dir \;

                        copy all files older than reference to a sub directory

scp -r ./my_sub_dir kieran@linuxpc:/home/kieran/test_dir

            take all files saved to <my_sub_dir> and copies them to destination

 

Move everything created in last day to new directory

            find * -mtime -1 -exec mv {} my_sub_dir \;

                        this takes all files in * and moves them to directory my_sub_dir.

 

Delete everything older than x days

            find /opt/kieran/logs/* -type f -mtime +1 -exec rm -f {} \;

 

So whats the deal with the curly braces {}?

Each file that the find command finds, is placed in the {} array, so that they can then be manipulated one at a time by the exec command.

 

Heres some nice links with more detail:

http://www.google.com/url?q=http://kb.iu.edu/data/admm.html&sa=U&ei=WT69ToTREtPw8QPmg9SPBA&ved=0CBsQFjAC&usg=AFQjCNF5H3XFnIhnmwRRdqLflTsQh___aw

 

 

 

LS Listing Commands

List only directories

                ls -l | egrep “^d”

 

List only sym links

                ls -l | egrep “^l”

 

List only files

                ls -l | egrep –v  “^d”

 

List files in subdirectories

                ls –ltrR

 

List all files, incl. sub dirs, with full path.

                find /opt/kieran/docs/ ! -type d -print > file_list.txt

 

List files that do NOT have an extension.

                ls -l RUN* | grep -v ‘\.’

                                This looks for all files beginning “RUN*”