Thursday, February 3, 2011

To read Core dumps

To check the contents of an object file (binary file), we can't use vi or cat command, for that use strings command.
%  strings [name of binary file]

It will print all the printable strings present in object file.  Basically strings command looks for ASCII strings
in executable file and print it.  Great for core files and other binary error files.

Wednesday, February 2, 2011

8) How to run a command to a set of files


This is an example to suffix all files ending with mp3 with "bak"

prompt$   for i in `ls *mp3`; do cp $i $i.bak; done

7) Listing files by size

If you want to have a listing of the files sorted by size, you can use the following command(s), it will list the files in decrease order. if you need to do the same thing recursively, you could use the second one.

ls -l | grep ^- | sort -nr -k 5 | more

ls -lR | grep ^- | sort -nr -k 5 | more

6) Some good tips:

Bad:    cat somefile | grep something
Better: grep something somefile

Why:    You're running one program (grep) instead of two (cat and grep).

Bad:    ps -ef | grep something | grep -v grep
Better: ps -ef | grep [s]omething
Why:    You’re running two commands (grep) instead of three (ps and two greps).

Bad:    cat /dev/null > somefile
Better: > somefile
Why:    You're running a command (cat) with I/O redirection, instead of just redirection.

Although the bad way will have the same result, the good way is far faster.  This may seem trivial, but the benefits will really show when dealing with large files or loops.

5) To run on last sunday of a month

If you want a job to run on the last sunday of every month, you can use the following syntax from within cron:

00 18 * * 0 [`date "+%d"` -gt 24] && /path/to/script

i.e. on sundays at 18:00 check if the day of the month is greater than 24 - if so run the job (if 23 is specified the job will run on the last 2 sundays of the month)

NOTE: There are back-ticks "`" around the date command, not single quotes.

4) Deleting blank lines from a file using "grep"

For thos who are not familiar with awk, but still want a quick and easy way of removing blank lines from a flat ascii file, remember that the use of 'cat' in conjuction with 'grep' is just as effective.

$ cat file1 | grep -v '^$' >file2
$ mv -f file2 file1

3) Get your IP address

Type this simple command at the unix-prompt

ifconfig | grep "inet addr" |  grep -v "127.0.0.1" | awk '{print $2;}' |  awk -F':' '{print $2;}'

2) Solution for ^M characters


If you deal with DOS files and the "^M" character always appears at the end of the line, here is a command
provided in SCO-UNIX.

dtox -- change file format from MS-DOS to UNIX

From a Unix shell use the command:
$dtox filename > output.file

Other flavers have similar commands: dos2unix or to_unix

$dos2unix filename output.file

Check your man pages ont he flavor you use. If you have whatis set up. Use a

$man -k dos

1) Use of Alias

The following alias will list just the directories in a directory and not other ordinary files. It will list a directory as a directory even if the directory is a symbolic link.

alias dir='ls -Lla|grep ^d'

Without the capital "L" in this alias, the alias will not list directories that are symbolic links.

For using this alias every time, put this command in your ".profile" file in the Home directory.

Another example, suppose you have to go to some logs directory very frequently, which is say something like this:

/siebel78/siebsrvr1/siebsrvr/enterprises/ABCDE/server1/log

You can make an alias,
alias golog='cd /siebel78/siebsrvr1/siebsrvr/enterprises/ABCDE/server1/log'

and then use this alias from anywhere from the server to go to the Logs directory.