Thursday, September 21, 2006

Linux : Changing ownership with find command

Good example of find with -exec...

Following command finds all files under /var/www/html owned by user ketan and changes the owner and group to webuser and web respectively. Only root can do this.

find /var/www/html -user ketan -exec chown webuser:web {} \;

Tuesday, September 19, 2006

Documentation with Pod

Pod is a simple-to-use markup language used for writing documentation for Perl, Perl programs, and Perl modules. POD stands for Plain Old Documentation . Pod documentation can be included in perl scripts or modules.

Translators are available for converting Pod to various formats like plain text, HTML, man pages, and more.

Following commands are supported

    =head1 Heading Text
    =head2 Heading Text
    =head3 Heading Text
    =head4 Heading Text
    =over indentlevel
    =item stuff
    =back
    =cut
    =pod
    =begin format
    =end format
    =for format text

Following is an example of a pod document. To test, copy the following part into a file and run perldoc filename.

#######################################
=head1 NAME

abc.pl

=head1 DESCRIPTION

This line shows up as description. This line shows up as description. This line shows up as description. This line shows up as description.

=head1 USAGE

I< perl abc.pl >

 The script asks for 3 inputs. They are -

 * x
 * y
 * z

=head2 heading2

Try this out ! You may want to include documentation of your scripts in Pod.

=head1 AUTHOR

ketan404

=cut
###############

Monday, August 21, 2006

Linux : find and replace with grep and sed

Following command finds PATTERN and replaces it with REPLACEMENT in all files under directory/path/.

grep -rl PATTERN directory/path/ | xargs sed -i 's/PATTERN/REPLACEMENT/g'

Notes :

-r option of grep does recursive search
-i option of sed edits files in place
g does substitutions in all occurences

Wednesday, August 16, 2006

Bash : Saving time with Environment Variables

You can create an environment variable $dr (for document_root) to save typing the whole path /var/www/html.

In your .bashrc file add this line

export dr=/var/www/html

Then source .bashrc. After this you'll be able to use shortcuts like

vi $dr/index.htm

With export, we create a new environment variable. In c shell, the syntax is

setenv dr /var/www/html

You can see all the environment variables with env.

Thursday, August 10, 2006

Perl : Regular Expression to match an email address

Following regular expression matches a valid email address

/^(\w|\-|\_|\.)+\@((\w|\-|\_)+\.)+[a-zA-Z]{2,}$/

To learn more about regular expressions in Perl, read

man perlre

Linux : find files, find patterns and replace in one go

Following command finds all text file in current working directory and replaces XYZ with ABC in all found files.

find . -name "*.txt" -exec perl -pe 's/XYZ/ABC/g' -i {} \;

Notes
  • You need to have perl installed on your system
  • with -exec you can execute a command on the found files
  • -i option of perl is used to update files
  • -p option of perl command assumes a loop around the code just like the -n switch. Only difference is that it prints the lines.
  • XYZ is a regular expression to be searched and ABC is the replacement text.

Tuesday, August 01, 2006

Fwd: Back after many days !

Hi !

I am back after many days of inactivity.

Wednesday, July 26, 2006

PHP : Getting image size

PHP has a very handy function getimagesize() which returns an array of values like width, height, type etc.

Example

$image_size = getimagesize("path/to/the/image.jpg"); ## URLs can also be passed

$image_size[0] is the width and $image_size[1] is the height of the image.

You can also use the following line (picked from php.net) so as to get the values in four different variables directly with the use of list() function.

list($width, $height, $type, $attr) = getimagesize( "img/flag.jpg");

Wednesday, July 19, 2006

Linux : Appending a tar file with xargs

Whereas -c switch of tar command creates an archive, -r is used to append files to a tar files. In the following example, xargs keeps on sending htm files as input to the tar command. These files are appended to htm_files.tar.

find . -name "*.htm" | xargs tar rvf htm_files.tar

Usual way of tar -cvf does not work for the above purpose.

Sunday, July 16, 2006

Linux : Find and Rename files with -exec

Following command finds .html files in the current working directory and changes the extension to .htm. The command illustrates the use of rename with find's exec switch. A quick way of finding and renaming files.

find . -name "*.html" -exec rename .html .htm {} \;

Find is regarded as a power command by many Linux users. The above example supports the claim ! There are many seemingly complicated or time consuming tasks which can be done quickly with commands.

Tuesday, July 11, 2006

Linux : Counting files per directory with Bash command

With commands, you can do many intricate things. Following command which is a small script, counts the number of .htm files in each subdirectory upto depth 2 of the current working directory.

for x in `find . -type d -maxdepth 2`; do y=`find $x -name "*.htm" | wc -l`; if [ $y != 0 ]; then echo $x - $y; fi done

This command is useful for webmasters who spend some time once in a while to figure out number of files in each of the website's subdirectories.

Notes
  1. The value of -maxdepth may be changed as needed
  2. The above command uses current directory (find . -type ...). The dot may be replaced by path of a directory of which the stats is needed.
  3. "*.htm" part of the filename may also be replaced with whatever you want

Thursday, July 06, 2006

Perl : Obfuscation of ARGV

Did you know ?

$a=eval shift @{$x.=chr for qw * 65 82 71 86 * ;$x};

is just another way of saying

$a = shift @ARGV;

Same thing can be done in many different ways. And some ways are intended at making it hard for humans to grasp the code ! Read more about Code obfuscation.

Thursday, June 29, 2006

Perl : Formatting code

Following one liner attempts to format your codes. Works on programs of perl, c, php etc where curly braces - '{ and }', are used for blocks, loops, conditions etc.

perl -pe '$tabs = substr($tabs,1) if (/^\s*}\s*$/);s/^\s*/$tabs/;$tabs.="\t" if(/{[^}]*$/);' -i.bk
filename.c

Files are backed up with .bk extension.

Wildcards are allowed so *.c can be used in place of filename.c.

Monday, June 26, 2006

Perl : Editing files on the command line

With -i switch of perl, files can be edited on command line. You can optionally take a back-up of the old files by providing an extension.

perl -pe 's/\r\n/\n/;' -i file1

Replaces \r\n (dos newlines) to \n from file1.

perl -pe 's/\<\?php/<?/;' -i.bk *.php

Replaces <?php with <? in php files and creates back-up files with .bk extension.

Thursday, June 22, 2006

vi/vim : Changing the tabsize

Default tabstop is 8. To set the tabsize to 4 spaces, just issue this command

:set ts=4

ts stands for tabstop.

To make this a default setting, write this line without the colon ':' in .exrc (.vimrc if you use vim) file.

Wednesday, June 21, 2006

Vim : Dictionary completion facility

Dictionary completion facility is useful when you don't remember the spelling of a word. To make use of this, we must first set the dictionary option so that vim knows where the dictionary is. Following command does that.

:set dictionary-=/usr/share/dict/words dictionary+=/usr/share/dict/words

Now, start typing a word in insert mode.

astro<ctrl+x><ctrl+k>

This shows the first match in the dictionary. You can loop through the matches by using <ctrl+n> (for next) and <ctrl+p> (for previous).

Notes

To understand why the -= and +=, use this command
:help :set+=

Tuesday, June 20, 2006

Regular Expressions : Dealing with special characters like asterisk

This post is for beginners of perl/regular expressions who find it hard to deal with special characters like asterisks if they appear in patterns. For instance, here is a pattern that contains an asterisk
4*3
If you want to replace or remove the asterisk, you will have to escape it with a back-slash ('\') in a regular expression. Following is a sample code.

$x = "4*3";
print "$x\n";

## following line removes the special character asterisk from the scalar $x
## note that the asterisk is 'escaped' by using a back-slash
$x =~ s/\*//;

print "$x\n";


Output

4*3

43

Use of a back-slash ('\') for escaping special characters is common where regular expressions are used. It's also used for escaping spaces in directory-names on shell prompt.

Some examples

character : escaping it
\ : \\
* : \*
+ : \+
/ : \/

Perl : Removing blank lines from a file

You can do intricate things even on command line. The following command prints filename on the command standard output after removing blank lines

cat filename | perl -ne 's/^\s*\n$//; print;'

Notes
  • Replace filename with the path of the file from which you want to remove blank lines.
  • You can optionally redirect the output by using '>' operator to create a new file.

Monday, June 12, 2006

Linux : Installing Packages .... common procedure

This post is for novices who are scared of building/installing new packages on linux :-) Here are the steps.

  1. Download the package. It's normally a .tar.gz file. It could be in some other format like bz2 as well.
  2. Extract it with tar -zxvf package.tar.gz (for .tar.gz files)
  3. Change to the newly created directory with this extraction
  4. Read README files (good practice)
  5. Run on prompt
    ./confgure
  6. then
    make
  7. Become root if you have not already. Default location of installation of almost all new packages is /usr/local so you need write permission to create any new files there.
  8. issue
    make install
You can install packages in other locations by using --prefix=/path/where/you/want/to/install option to ./configure. Learn more about options by using --help (normally) switch of ./configure scripts. All configure scripts may not present you with nice help.

Friday, June 02, 2006

Linux : Synchronizing directories with rdist

rdist is a linux command for keeping identical copies of directories/files over two hosts. File permissions and modification time are preserved if possible. It can use various protocols like ssh, rsh (default). You can have all the synchronization details stored in a file and pass the filename as an argument with option -f to the command as illustrated below.

rdist -f rdist_file

To specify the protocol, use -P option

rdist -P /usr/bin/ssh -f rdist_file

rdist_file is of the following format.

## comments start with '#' sign
/path/on/master/server    ->   remote_server_name  install  /path/on/slave/server ;

-o option is used for specifying an action to be taken on those files on the slave (remote server) which are missing from the master. For truly synchromising two directlries, you can use -oremove to remove the files. The directive then takes the following form.

/path/on/master/server    ->   remote_server_name  install -oremove /path/on/slave/server ;

For more about actions and about the command, see man pages.