Search for Tips & Tricks

Google
 

Thursday, January 24, 2008

How to redirect output to a file and stdout simultaneously

# How to redirect to stdout and a file

grep 2>&1 | tee

e.g.

grep India *.txt 2>&1|tee output.log

Wednesday, January 23, 2008

How to use find command to find and execute

Find can be clubbed with xargs for finding and executing commands.
e.g.

1. Find log fine in /var and delete them

$ find -path /var -name *.log -exec rm {} \;

Tuesday, January 22, 2008

How to mount a cdrom/pendrive on Linux system

For mounting cdrom user the command as su:

mount -t iso9660 -o ro /dev/cdrom /cdrom

For mounting pendrive the command as su:

mount -t vfat /dev/sda1 /mnt/pendrive

How to list file that doesnot contains some string

Llist files that does not contain string 'dat' in file name

$ ls *[!dat]

Sunday, January 20, 2008

How to open remove application on local machine

This can be done using xhost utility:

Step 1. On the local host

Type the following at the command line:

$ xhost +

Step 2. Log on to the remote host

$ telnet

Step 3. On the remote host (through the telnet connection)

Instruct the remote host to display windows on the local host by typing:

$ setenv DISPLAY :0.0
->> user zsh ( shell)
at the command line. (Instead of setenv you may have to use export on
certain shells. in bash shell use DISPLAY=:0.0)

Step 4. Now you can run software from the remote host.

E.g.: when you type $ xterm on the remote host, you should see an
xterm window on the local host.

Step 5. After You Finish

You should remove the remote host from your access control list as follows.
On the local host type:

$ xhost -

How to Extract a single line from a file

$sed -ne 5p help | /bin/bash

The above command will extract 5th Line from the file 'help'

Friday, January 18, 2008

How to create/Delete a directory tree

>>>>>>>>>>>>>>>>>> Code >>>>>>>>>>>>>

user File::Path;

mkpath('Level1/Level2/Level3'); # Create all directories tree Level1-->Level2-->Level3

rmtree('Level1/Level2/Level3'); # Delete all directory tree

Thursday, January 10, 2008

How to return reference to a File Handle from Subroutine

>>>>>>>>>>>>>>> Code >>>>>>>>>>>
my $FH = returnFileHandle();

print $FH "Hello, World";

sub returnFileHandle {

my $fileName = "file.txt";

open (FILEHANDLE, ">$fileName") or die $!;

return \*FILEHANDLE;

}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
The subroutine returnFileHandle() returns file handle (in overwrite mode) of file "file.txt".

Thursday, January 3, 2008

How to delete files in Perl ?

>>> Code >>>>>>

@fileList = (”file1.txt”,”file2.pl”,”file3.java”);

unlink @filelist;

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

The above code will delete files in fileList arry from current directory. If you want to remove files from a specific directory first use “chdir ” to set the present directory and then user unlink.