Using vi to replace a string in multiple files

Aug 9th, 2008

Sometime you may want to replace occurences of a string across multiple files.

There is an easy way to do so with the help of the vi editor.

This example will illustrate the power of vi:

Suppose you have 100 .html files, and you want to replace the occurence of the string ’2007′ with ’2008′.

As such, execute the following at your prompt:

vi *.html

This will open all the files ending with ‘.html’ in your current working directory. Then issue the following command:

:argdo %s/2007/2008/g | wq

That’s it! the above command will loop over each file, replacing (%) the word 2007 with the word 2008 globally (g) then it will save each file (w) and exit from it (q).

Note that the string to find and the string to use for replacement can be replaced with regular expressions. For instance, a caret (^) refers to the start of a line, the dollar sign ($) refers to its end, etc. So if we were to replace any line starting with ‘sample’ and ending with ‘test’, one could use:

:argdo %s/^sample.*test$/g | wc

where .* matches anything in between the two string.

Note too that you may do the find and replace action without respect to the case (i.e. a search for ‘word’ will match ‘WoRd’). For that matter, simply replace ‘/g’ with ‘/gi’

Tags:

Display page load time

Aug 4th, 2008

You may want to display the time it took to load a page.
Following is a set of simple PHP code which would help you achieve this goal.

Place the following snippet at the very top of your page.
(Ultimately, it’s advisable to have a header file and a footer file that are included on every page of your website).

<?php
$timer_start = str_replace(" ","",microtime());
?>

Then place the following code at the very end of your footer.

<?php
$timer_end = str_replace(" ","",microtime());
$timer_diff = number_format($timer_end-$timer_start,6,'.','');
if($timer_diff<0) $timer_diff = "0.00";
echo "<div id='load-time' style='font-family: verdana; font-size: 8pt; color: #444444; text-align: center'>Page load time: <b>$timer_diff</b> second</div>";
?>

Obviously, we’re assuming here that this code should be within .php files, with the current server configuration allowing for parsing of such files.

Tags:

Microsoft Office Picture Manager takes a lot of time to load

Mar 3rd, 2008

Is your Microsoft office manager taking way too much time to load?

The reason behind it is that  the picture manager keeps track of the most recently used paths, and if any of these paths is no longer available, the software will stall for a long period.
The most possible scenario is that you have accessed a folder over the network, and you are no longer able to access that location.
The fix is rather simple.

On windows XP, go to c: -> documents and settings -> username (your login) -> local settings -> application data -> Microsoft -> OIS
Open the file ‘oiscatalog.cag’ with our favorite text editor (e.g. notepad) and delete any line pointing to a remote location.
You might as well delete that file, and it will be recreated upon starting the software again.

PS: if you’re on vista, the path to that file is C:\Users\yourusername\AppData\Local\Microsoft\OIS

Tags:

Linux file permissions explained

Dec 16th, 2007

Some people may not be very knowledgeable about *nix file permissions, and what they mean, so this is intended to be some sort of a guide.

Any file or folder has a permission associated to it. Look at it as: ‘who has the power over this file/folder, and what kind of power does he have’.

There are 3 types of permissions:

  • read
  • write
  • execute

These permissions apply to 3 groups of people:

  • The file/folder owner
  • The group to which the owner of this file/folder belongs
  • The rest of the crowd, called world.

So suppose you have a file ‘x’, then this file x will certainly have:

  • a set of permissions defining what its owner can do with it
  • a set of permissions defining what users that belong to the same group as the owner can do
  • what other people that do not fit int he first 2 categories can do

You can assign a single permission (e.g. read) or a set of permissions (e.g. read, execute) to this file/folder.

As an instance, you can allow the owner to do whatever he wants with the file (read,write,execute), allow the group to which the owner belongs to simply read the file, and disallow everyone else on the system from doing anything.

Now, to make it easier for you, and to not get into how these numbers are calculated, just memorize the following:

  • read equals to 4
  • write equals to 2
  • execute equals to 1

Let’s get back to the example listed above.

  • We wanted the owner to have full permissions, this means the permission for the owner should be: 4 (read) + 2 (write) + 1 (execute) = 7
  • We wanted to give the group read and write permissions, meaning 4+2=6
  • We wanted to deny complete access to the world, so that’s a 0

So, the permission for all the 3 types of users we mentioned earlier sums up to: 750

Keep in mind, you use the digit corresponding to the owner’s permissions first (7) , then the group (5) then to world.

Pretty simple, eh?

How do you set such a permission for a certain file ‘x’? Simply execute at your shell prompt chmod 755 x

Let’s do another exercise but backward this time. If i were to tell you that by default, a new created file would have a permission of 644, what would the permission of each of the 3 types of users be?

6 is basically 4+2 (you can’t get it any other way, remember you only have 3 numbers: 4,2 and 1 which would potentially take part of making up this number). This means read and write.
4 is well, 4 … that’s a simple read

So a 644 permission is a read and write permission for this file/folder’s owner, and a read permission for the owner’s group users and for the rest of the world.

One question arises though: what is the difference between the permissions we set to a file and those we set to a folder?

Basically:

  • A read permission on a file means we can see its content, while a read permission on a folder means we can list its content (get the list of files and folders it contains).
  • A write permission on a file means we can alter its content, while a write permission on a folder means we can create, and modify files and folders under it
  • An execute permission on a file means we can literally execute it (e.g. shell script), while an execute permission on a folder means we can browse into it.

A final point:

If you get a long listing of the files/folders on a *nix system, you may as an example notice the following:

-rwxr-xr-x

This is the textual representation of the numbered permissions I explained above. The dash – means ‘no’. ‘r’ means read, ‘w’ means write, ‘x’ means execute. The very first bit would be an indication whether this is a file (-) or a folder (d).

Again, we go by the owner, group, world sequence. So the above example indicates that this is a file, owner has read, write, execute permission, group has read, execute permission, world has read,execute permission.

Hope this guide was simple yet beneficial to the readers.

Tags:

Resetting your mysql root password

May 17th, 2007

It is quite frequent that an administrator simply forgets his mysql’s root password.

Luckily, it is quiet easy to reset it, here are the steps:

  1. SSH as root to your machine
  2. Turn off the mysqld daemon if running
    • RedHat/Fedora users can do so by executing:  service mysqld stop
  3. Run safe_mysqld by executing:
    • safe_mysqld –skip-grant-tables
      (this will run allow you to connect without a password)
  4. Open a second shell / SSH again and execute:
    • mysql mysql
      (to directly connect and select the mysql database which contains the user authentication data)
  5. On the mysql prompt, execute:
    • update user set password=password(‘newpassword’) where user=’root’;
      where newpassword is your newly chosen password.
  6. That’s it! close everything and start your mysql daemon again:
    • service mysqld start
Tags:

Add authentication to any php page the easy way

Apr 3rd, 2007

This is useful for anyone who’d like to add a login form to any php page.

Simply, save the following code as access.php, and insert the following snippet on the first line of any php file you want protected.

< ?php include "access.php"; ?> (remove the space before the question mark)

Voila! as easy as 1,2,3. The crentials can be edited in the access.php file, by changing the values of $ADMIN_USER and $ADMIN_PASSWORD.

This useful script requires no database, and no big tweaking to the code on your pages.

Tags:

PI to 16,777,200 decimals

Mar 15th, 2007

For the fun of it.

File formats:

PI to 16,777,200 decimals text format 12 MB

PI to 16,777,200 decimals zip format  8 MB

Tags:

PHP script to import csv data into mysql

Feb 19th, 2007

This is a simple script that will allow you to import csv data into your database. This comes handy because you can simply edit the appropriate fields, upload it along with the csv file and call it from the web and it will do the rest.

It allows you to specify the delimiter in this csv file, whether it is a coma, a tab etc. It also allows you to chose the line separator, allows you to save the output to a file (known as a data sql dump).

It also permits you to include an empty field at the beginning of each row, which is usually an auto increment integer primary key.

This script is useful mainly if you don’t have phpmyadmin, or you don’t want the hassle of logging in and prefer a few clicks solution, or you simply are a command prompt guy.
Just make sure the table is already created before trying to dump the data.
Kindly post your comments if you got any bug report.

Download file here

Tags:

How to import a csv file into mysql from the prompt

Jan 11th, 2007

For the follows out there who do not know how to import coma separated data into mysql from the prompt, here it is:

Assume:

PATH is the path to the csv file (can be relative to the directory you were in before logging into mysql)
TABLE is the table name that you have already created and whos fields match the csv file fields.
USERNAME is the mysql user that have access to insert data into TABLE
DATABASE is the database containing that table

- login to mysql by typing: mysql -u USERNAME -p DATABASE
The prompt will ask you for the user password, type it.

- Execute the following query:

load data local infile ‘PATH’ into table TABLE fields terminated by ‘,’ lines terminated by ‘\n’;

And voila.

Tags:

Using iptables in order to share an internet connection

Nov 9th, 2006

Many users run small networks, be it at home or work. One of the widely used techniques for sharing the internet connection over a small local area network is Microsoft’s ICS (Internet Connection Sharing).

What if the main server is not running windows? Some prefer to opt for linux, and run several services that benefit client computers on this network (such as dns, dhcp, samba, NIS, etc…)

The good news for linux users is that they can benefit from the same functionality of ICS, but using linux iptables.

The following is a sample rule:

/sbin/iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE

Note that you will need to have ip forwarding enabled.

RedHat/Fedora users can execute the following:

echo 1 > /proc/sys/net/ipv4/ip_forward

Now, these rules can be saved in a .sh file, make that shell script executable (chmod +x filename.sh)

Then, make this file execute whenever your system boots. An example would be editing /etc/rc.local and setting the path to that file in there.

This simple iptables rule can be expanded in order to allow certain ports, block others, check protocols for incoming packets on such ports (tcp, udp) , etc.

Advanced rules to come later hopefully.

Tags:
This blog is protected by Dave\\\\\\\'s Spam Karma 2: 14411 Spams eaten and counting...