Enjoy life as it comes...

Stephen Weblog

All about me…
Subscribe

Archive for February, 2008

Strings

February 29, 2008 By: admin Category: Unix Scraps No Comments →

To check content 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.

Bad and Good

February 27, 2008 By: admin Category: Unix Scraps No Comments →

I cringe anytime I see someone code inefficiently.  Here are
three of the most common mistakes, followed by a better way to
do the same thing.

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.

Delete blank lines using grep

February 14, 2008 By: admin Category: Unix Scraps No Comments →

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

Guess!!!

February 12, 2008 By: admin Category: Unix Scraps No Comments →

Try it out on any unix system

ls -R | grep “:$” | sed -e ’s/:$//’ -e ’s/[^-][^\/]*\//–/g’ -e ’s/^/   /’ -e ’s/-/|/’

Remove blank lines

February 11, 2008 By: admin Category: Unix Scraps No Comments →

To suppress the blank lines in a text file:

sed ‘/^$/d’
awk ‘NF>0′

BASH HOTKEYS

February 08, 2008 By: admin Category: Unix Scraps No Comments →

Bash provides many hot keys to ease use. Here is few of them

ctrl-l  — clear screen
ctrl-r  — does a search in the previously given commands so that you don’t
have to repeat long command.
ctrl-u  — clears the typing before the hotkey.
ctrl-a  — takes you to the begining of the command you are currently typing.
ctrl-e  — takes you to the end of the command you are currently typing in.
esc-b   — takes you back by one word while typing a command.
ctrl-c  — kills the current command or process.
ctrl-d  — kills the shell.
ctrl-h  — deletes one letter at a time from the command you are typing in.
ctrl-z  — puts the currently running process in background, the process
can be brought back to run state by using fg command.
esc-p  — like ctrl-r lets you search through the previously given commands.
esc-.  — gives the last command you typed.

cpio over cp

February 08, 2008 By: admin Category: Unix Scraps No Comments →

Using standard UNIX tar to copy a tree doesn’t preserve the original owner and group information for the directories and files during the copy. To do this, use find(1) piped to cpio(1) this way:

% cd <source-directory>
% find . -depth -print | cpio -pudm <dest-directory>

This will create a mirror image of the <source-directory> tree in <dest-directory>.

pfiles

February 07, 2008 By: admin Category: Unix Scraps No Comments →

Ever felt like you wanted to find out all the files opened by a process, and that is when I found pfiles.

Usage :
/usr/proc/bin/pfiles <pid>

Where pid is the process-id of the process. It lists the inode numbers of all the files, opened by that process.

Operating on multiple files

February 07, 2008 By: admin Category: Unix Scraps No Comments →

Have you ever felt the need to perform a set of operations on multiple files simultaneously???

Here is a solution for that.

For instance, if it is required to perform multiple operations like searching a string (using grep), and executing an awk or perl script etc, etc. on not just one file but a set of files, use the following commands at the unix prompt:

$<: foreach i (<file_list>)
? echo $i
? grep <search_pattern> $i > tmp
? awk -f awk_script tmp >> report
? ….
? ….
? end
$<:

Lock down telnet/ftp

February 07, 2008 By: admin Category: Unix Scraps No Comments →

When inbound access isn’t required into a system deny users Telnet or FTP access do the following:

vi /etc/inetd.conf

Comment the line starts with Telnet or FTP. Save the file and exit. Stop and start the inetd daemon now by following commands:

/etc/rc.d/init.d/inet stop
/etc/rc.d/init.d/inet start

(Your flavor may be /etc/init.d)

Now on nobody can telnet or FTP to your server from outside network.