Unix Notes.
Unix Commands and scriptlets: ================================================================================
  1. Send attachments using mailx.
  2. tar command.
  3. Create CDROM Boot disk, floppy boot version.
  4. Create CDROM Boot disk, using isolinux.
  5. Exchange ssh keys.
  6. Rename files.
  7. Replace text in file.
  8. Count in shell scripts.
  9. Kevins cool awk command for directory sizes.
10. Delete ^M from you files.
11. Datestamp and Bakup Files With Datestamp.
12. Backup partition over ssh.
13. VI Spell check.
14. tar through ssh.



1. Send attachments using mailx.

================================================================================ From: KUMAR, Bageshwar, FM [mailto:Bageshwar.KUMAR@rbos.com] Sent: terga-feira, 3 de Fevereiro de 2004 10:25 To: 'Joao Pedro Rodrigues' Subject: RE: Send attachments Let us assume the file u want to send is : /tmp/test.html Run this command :
    
    uuencode /tmp/test.html test.html | mailx -s "Subject_Line" 

Return to top of page.

2. tar command.

================================================================================ Interesting uses for the TAR command.

    cd sourcedir; tar -cf - . | (cd targetdir; tar -xf -)

The command also works using short option forms:

    cd sourcedir; tar --create --file=- . | (cd targetdir; tar --extract --file=-)

This is one of the easiest methods to transfer a tar archive. In my experience, the parenteses are not important, but the quotes are. Also it is a good habit IMHO to replace the semicolon with && so the line would read:

    /usr/local/bin/gtar -czf - | rsh linuxsys -a 'cd /tmp && tar -xzf -'
    
That would prevent an embarasment if the cd command failed. In other words, tar is only run if cd was successful. By the way, what does the '-a' do? Return to top of page.

3. Create CDROM Boot disk, floppy boot version.

================================================================================ mkisofs -J -v -V "RedHat 9.0 (Shrike)" -c boot.cat -b boot.img -o /root/bootcd.iso .
    creates a cd with all the files in the . directory, one level below current.
Return to top of page.

4. Create CDROM Boot disk, using isolinux.

================================================================================

    mkisofs -J -v -V "RedHat 9.0 (Shrike)" -c isolinux/boot.cat -b isolinux/isolinux.bin \
   -no-emul-boot -boot-load-size 4 -boot-info-table -o /root/bootcd1.iso .
   
(command all on one line, uses the . current directory for file source) Return to top of page.

5. Exchange ssh keys.

================================================================================ (I stole this off the internet, it has errors and spellings from UK english.) On the ssh2-client, ssh-keygen -t rsa creates two identity files called id_rsa and id_rsa.pub in $HOME/.ssh.
    
	ssh-keygen -t rsa

Copy the id_rsa.pub file to ssh2-server: scp .ssh/id_rsa.pub userid@ssh2-server:id_rsa.pub mkdir .ssh (if necessary)

	scp id_rsa.pub root@(ssh2-server):/user/directory/id_rsa.pub

Connect to the server and copy the contents of the id_rsa.pub file to the authorization file for protocol 2 connections: cat id_rsa.pub >>$HOME/.ssh/authorized_keys.
    
	cat id_rsa.pub >> $HOME/.ssh/authorized_keys

Remove the id_rsa.pub file: rm /id_rsa.pub The resultant authorization file should look like this :
    userid@ssh2-server $ less .ssh/authorized_keys
    
    ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA4E8RTHpvJabH561CSG+SFluCSFw1GZrwqx1dO5UWzbAR 
    dwDwLLB3Ad3CemjyQ44rnAWvFS0TZWeEWjNHvJOW1igMr/IYCQtrhomUpdUKMtzBBUWv2vnpoAldgTR1 
    1aDpaSAx/ioaTzYPuG+hat4RLCjPIgM7ymzr/QQVRFYbHOc= userid@ssh2-client 

edit the /etc/ssh/sshd_config file and allow RSA authentication. The SSH server is paranoid, and will refuse to honour your authorized_keys settings if they have poor file permissions, so you should lock down your files and directories:
    username@server$ cd $HOME
    username@server$ chmod go-w .
    
    username@server$ cd $HOME/.ssh
    username@server$ chmod 700 .
    username@server$ chmod 600 *

Return to top of page.

6. Rename files.

================================================================================ To move all files of .bs extension, to no extension, use this sed trick.

    for i in *
    do
        mv $i `echo $i | sed 's/.bs//'`
    done

Return to top of page.

7. Replace text in file.

================================================================================ =>On February 11, 2004 09:33 am, Piero Calucci wrote: =>> btw if you are going to _replace_ text you should better look at sed or =>> perl one-liners Here's something I've been using for years: replace_str.sh ---------snippity------------------------Start here--------------------
    #!/usr/local/bin/perl -i.old -w
    use strict;
    
    my $from_text = shift;
    my $to_text = shift;
    
    while( <> )
    {
        s/$from_text/$to_text/go;
        print;
    }
---------snippity------------------------End here-------------------- Use like this:
        replace_str oldstr newstr *.html
or if you have a lot of files
    find blah | xargs replace_str oldstr newstr
Don't use the find -exec \; trick. It'll start a seperate child process for each file it operates on. Return to top of page.

8. Count in shell scripts.

================================================================================
    #!/bin/bash
    #
    COUNT=1
    
    for PROCESS in 1 2 3 4 5 6 7 8 9
    do
        if [ "$PROCESS" -eq "10" ]; then
            exit
        fi
        echo "PROCESS NUMBER: $PROCESS"
        sleep 1
        ((COUNT++))
        echo "COUNT=" $COUNT
    done
OR....
    #!/bin/ksh
    #
    COUNT=1
    
    for PROCESS in 1 2 3 4 5 6 7 8 9
    do
        if [ "$PROCESS" -eq "10" ]; then
            exit
        fi
        echo "PROCESS NUMBER: $PROCESS"
        sleep 1
        let COUNT=COUNT+1
        echo "COUNT=" $COUNT
done
Return to top of page.

9. Kevins cool awk command for directory sizes.

================================================================================

    find . -type d -name archive -exec du -sk {} \; | awk '{ s += $1} { print s}'

Try this for incrementing group numbers:

    GROUPNUM=`tail -1 /etc/group | awk -F\: '{print $3 += 1}'`
 
Return to top of page.

10. Remove carraige returns from files.

================================================================================ Subject: How to delete ^M character using vi Reply: There is a much easier way. In vi, do a :%s/^M//g To get the ^M hold the control key, press V then M (Both while holding the control key) and the ^M will appear on the ex line. This will find all occurances and replace them with LF. Return to top of page.

11. Datestamp and backup.

================================================================================ # produces date output
date +%m%d%y-%H%M%S # uses brace expansion #
{,.`date +%m%d%y-%H%M%S`}
# example: #
cp /etc/hosts{,.`date +%m%d%y-%H%M%S`}
############################### Bakup Files With Datestamp ##################################
bkup () { BKUP_ARG1=$1; BKUP_FILE=$2; BKUP_DATE_STAMP=`date +%m%d%y-%H%M%S`; case ${BKUP_ARG1} in -v) cp -i ${BKUP_FILE}{,.$BKUP_DATE_STAMP}; vim ${BKUP_FILE} ;; -l) cp -i ${BKUP_FILE}{,.$BKUP_DATE_STAMP}; ls -l ${BKUP_FILE}{,.$BKUP_DATE_STAMP} ;; -h) echo "Function to bakup files with datestamp."; echo; echo "Example: bkup [option] /etc/hosts"; echo; echo "Options: -v backup, then vi original."; echo " -l backup, then ls original and backup."; echo " -h this help menu."; echo ;; *) cp -i $1{,.$BKUP_DATE_STAMP} ;; esac }
Return to top of page.

12. Backup partition over ssh.

================================================================================ SECURE BACKUPS OVER NET Securely backup a partition across a network using dd, gzip and SSH: 1. The partition to be backed up must be unmounted. 2. The following does the trick:

    dd if=/dev/partition_to_be_backed_up | gzip | ssh user_name@backup.server dd of=name_of_backup_file.gz

As an example, the following backs up an image of a floppy disk:

    dd if=/dev/fd0 | gzip | ssh user_name@backup.server dd of=floppy.img.gz

3. To restore a backed-up partition across a network: (From the backup server)

    dd if=name_of_backup_file.gz | gzip -d | ssh user_name@target.server dd of=/dev/target_partition

To complete our floppy disk example:

    dd if=floppy.img.gz | gzip -d | ssh user_name@target.server dd of=/dev/fd0

Return to top of page.

13. VI Spell check.

================================================================================ SPELL-CHECK WITHIN VI To spell-check without leaving a vi session:

    :w !spell -b

Return to top of page.

14. tar through ssh.

================================================================================

ATar through ssh Tuesday March 23, 2004 (04:52 PM GMT) By: Graeme Winter To transfer a (large, complicated) file tree from one machine to another, using stuff which is usually supported:

tar cf - stuff | ssh bob@wendy.no.where.com tar xf - -C /home/brian

Autopsy:
(tar cf - stuff)
- tar stuff to the standard output
(| ssh bob@wendy.no.where.com)
- pipe this to an ssh connection to wendy - where
(tar xf - -C /home/brian)
- is run - which will untar the standard input and place the result in /home/brian.... Neat. This is better than using scp -r or tar then scp, because you can send > 4 gig files, and also retain softlinks etc which get broken by scp...

Reference Source

Return to top of page.