Tuesday, July 12, 2005

substitute strings within files in UNIX

To change the string `EZZ` to `EZ` (this would look for the backquote, followed by EZZ followed by backquote)
sed 's/`EZZ`/`EZ`/' static.dat > static1.dat

A simple example would be:

sed 's/abc/def/ static.dat > static1.dat


Remove trailing spaces from a file in UNIX

To remove trailing spaces from a file,
sed -e 's! *$!!' /path/to/input >output
sed -e 's! *$!!' chaganlal.txt.bak > chaganlal.txt.nospace

find files in UNIX, Delete Old files, find files based on modified time

Find a file with given name
find /home/chaganlal -name core

Find files larger than 1MB
find /home/chaganlal -size +1000000c -print


Find files larger than 10 KB, but less than 32 KB
(The . denotes current directory)
find . -size +10000c -size -32000c -print

Find files that were modified less than 8, more than 6 days ago
find . -mtime +6 -mtime -8 -print

Find files that were modified exactly 7 days old
find . -mtime 7 -print

Find files that were last accessed more than 30 days ago
find . -atime +30 -print

To delete files older than 200 days
find /home/chaganlal/ -mtime +200 -exec rm {} \;

Friday, July 08, 2005

date reformatting in UNIX (useful when you need to send file with timestamp)

Here is one way of reformatting the date:

date '+ %m%d%Y_ %H%M%S' to get the date in the format, 10052004_114643

The usage takes the format,
$ datetail=`date '+ %m%d%Y_%H%M%S'`
$ echo $datetail

email attachment in UNIX

Okay, you want to send an email with an attachment.
Here is how:

uuencode filename attachment_name mailx -s "Report" -r from_email_id to_email_id

example:
uuencode myfile.csv myfile.csv mailx -s "Here is your report" -r chagan@yahoo.com toyou@hotmail.com

Simple, isn't it???

Sending and email in UNIX- Bells & Whistles

Here is an example of how you can send an email with Subject, To, CC, bcc, etc.

It will follow the format:
cat filename ¦ mailx -s subject -r senders_mail_id -b bcc_list_mail_id -c cc_list to_mail_id

cat filename : this will print the contents of the file called filename on standard output.
¦ : The pipe would pass the standard output to the mailx command.
mailx : This is the magic word. The mailx command.
-s : Specify the subject of the email following -s
subject : A text field (can be surrounded by double quotes if contains space) to be sent as subject of the email.
-r : denotes the return path (Sender's email address)
-b : a list of email ids in the bcc field (put in quotes if multiple, separated by commas)
-c : a list of email ids in the cc field
to_mail_id : The email id that is to appear in the "To" field of the email.

an example:
cat chagansmail.txt ¦ mailx -s "This is a test email - ($datestamp2)" -r myaddress@yahoo.com -b abc@hotmail.com -c "def@gmail.com,hatter@mail.com,mad@earthlink.net" greatman@aol.com


As you can see, you can include variables in the fields, too.