Tips

To set the PS1 variable:

 PS1=`hostname -s`:'$PWD>'
            or 
 PS1="`hostname -s` [\$PWD]# "

Do not user hostname -s in solaris, use uname -n

 PS1="`uname -n`@\$PWD>"
        or
 PS1="`uname -n` [\$PWD] # "

To send the standard error and output to error.txt file:

 $ ls -l > error.txt 2>&1 

To make the History file separate for each user when they su to root :

 Add the following lines to the .profile file of root: 

 ME=`who am i | awk '{print $1}'` 
 HISTSIZE=2000 
 HISTFILE=$HOME/sh_history/.sh_history.$ME 
 export HISTSIZE HISTFILE

To delete the files which are modified more than 30days

 $ find /u/arnold -ctime +30  -exec \rm {} \; 

To find out the files older than

 touch -t YYYYMMDDhhmmss /tmp/touchfile
 find ./ -older /tmp/touchfile -exec rm -f {}\;

 touch -t [4hours ago] touchfile
 find /path/to/files ! -newer touchfile  -exec rm -f {} \;

To find out files newer that 2 hours and take action on them:

 #!/usr/bin/ksh
 #----subtract 30 minutes (1800 seconds) from time
 STAMP=$(perl -e '($ss, $mm, $hh, $DD, $MM, $YY) = localtime(time() - 1800);
 printf "%04d%02d%02d%02d%02d", $YY + 1900, $MM + 1, $DD, $hh, $mm') 

 #----create an empty file with a timestamp of 30 minutes ago
 touch -t $STAMP /tmp/flagfile

 #----find older files
 find /start/dir -type f ! -newer /tmp/flagfile -print | while read FILE
 do
   : something with $FILE
 done

To print sequence of numbers from 0 to 255 with increment of 1

 # seq 0 1 255

Another way to do the same using for

 for i in {1..255}; do echo $i; done

To print sequence of numbers from 0 to 255 with increment of 5

 # seq 0 5 255
 0 
 5 
 10 
 ...... 
 255

To traslate all lower case letters to UPPER case

 $ tr 'a-z' 'A-Z' < textfile

xargs

 # ls | xargs -t ls -l 

To kill all the defunct process

 # ps -ef | grep defunct | awk '{print $2}' | xargs -i kill -9 {}

To rename all the files in a directory with an .old extension

 # ls | xargs -t -i mv {} {}.old

Connecting to console via console server device

 $ telnet consoleserver 1001

To quit from the console

 press ~.  or break keys - ~#

Generating SSH Key

 # 1024 bits with a key type of dsa
 ssh-keygen -b 1024 -t dsa

Man
Run "catman" to build the "/usr/share/lib/whatis" file.

Then you can run the "man -k [some command] to see what manual sections the command is in. For example, if you run "man -k mkdir", it will show that "mkdir" is in sections 1 and 2.

To see section 2 you would run "man 2 mkdir".

sort:

 -t to define delimeter
 -r to sort in revers order
 -n to say sort that the sorting field is numbers

To start a password file by user ids

 # sort -t : -n +2

grep, egrep and fgrep

  • egrep command is an extended grep command that supports more regular expression characters
  • fgrep command treats all characters as literals, meaning that regular expression meta-characters are not special
    -n # prints the line number the the actual line
    -v # display lines only that do not match
    -e # one or more patterns to be used during search
    -i # ignore case
    -h # does not display file names

Backing up MySQL
To backup any single database, you just have to type:

  code:
  mysqldump --add-drop-table -u dbusername -p dbname > dbname.bak.dump
   // or better yet...
  mysqldump --opt -u dbusername -p dbname > dbname.bak.dump

e.g. If I had to backup a certain database with the following details:
MySQL username is jds_user1 and MySQL database name is jds_db1

  code:
  mysqldump --add-drop-table -u jds_user1 -p jds_db1 > jds_db1.bak.dump
   // or better yet...
  mysqldump --opt -u jds_user1 -p jds_db1 > jds_db1.bak.dump