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;}'