Monday, April 28, 2014

Replace a String in Multiple Files in Linux Using Grep and Sed

So, I had to refactor my code and replace the occurrence of a particular text with another. The problem was that, the text to replace occurred in multiple files spanning various directories. So I used grep to find the word and if it finds it to run sed to replace the text.

The format is as follows:
grep -rl stringToFind somedir/ | xargs sed -i 's/stringToFind/replacementString/g'

For example: I wanted to replace the string 'SCOpe' with 'SCOPE'. The following is what I used:
grep -rl 'SCOpe' ./ | xargs sed -i 's/SCOpe/SCOPE/g'
This will search for the string 'SCOpe' in all files relative to the current directory and replace 'SCOpe' with 'SCOPE' for each occurrence of the string in each file.

Wednesday, November 6, 2013

Delete all files with a particular extension in a particular folder and its sub directories

Delete files of a given extension within a directory and its sub directories e.g. to delete files with '.pyc' extension, use the following command:

find . -type f | grep '.pyc'$ | xargs rm

Tuesday, January 29, 2013

Varnish Cache

Recently I was tasked with installing and configuring Varnish cache for one of our websites. I went through alot of documentation on the internet and I would just love to post some of them here:

A Practical Guide to varnish
Speeding Up your website with Varnish
Magento with Varnish
Wordpress with Varnish
Varnish Documentation

Feel free to post others that I might have left out.

Tuesday, December 4, 2012

GNOME Power Manager: install problem

In the upper right corner of my login screen, I get the following error in Ubuntu:

INSTALL PROBLEM! The configuration defaults for GNOME Power Manager have not been installed correctly. Please contact your computer administrator.

When I type in my password. It seems to do something, the screen turns black and a cursor appears for a second but then it returns to the login screen. The theme also appears to be bluish.

I noticed that this is more related to my hard disk memory reaching its limits.

So I followed the following steps:

  • Start the computer
  • On the login screen, press CTRL+ALT+F1. This will take you to the command line
  • Login with your username and password
  • Check the disk usage using the following command: df -h
  • Clear some space by deleting a directory that you do not need. It has to be a big directory, say 1GB. In my case, I used the following command: rm -vR /home/frank/very_big_dir
  • Then reboot: sudo reboot.
Hopefully, this should have fixed your problem

Friday, April 13, 2012

Modifying Existing Content Types using archetypes.schemaextender

I was working with Plone 4 and needed to extend the Folder Content type to add an image and a title.

Being a newbie in Plone, I rummaged through the internet (Google) and was fortunate to find the tutorial below. It has been a real help and helped solve my issue in less than 30 mins.

Tweaking Existing Content Types using archetypes.schemaextender

Enjoy!!!

Thursday, April 5, 2012

Unix/Linux Commands every developer should know

General Commands:

1. date: shows date and time
2. history: lists the previously executed commands
3. man ls: shows online documentation by program name
4. who: who is on the system and what they are doing
5. whoami: who is logged onto this terminal
6. cal: calendar
7. banner DZone 
8. clear: clears the screen
9. tty: displays your terminal

Directory Management Commands:

1. pwd: will print your home directory on screen. pwd means print working directory
2. ls: displays the contents of directory
3. cd: change to new directory
4. mkdir dirNameCreate: creates new directory
5. mv dirNameChange dirNameChange2: renames the directory           
6. rmdir dirNameremove: removes empty directory (you must remove files first)
7. dircmp dir1 dir2: compares two directories

File Management Commands:

1. find myFile.txt: finds files that match specific criteria  
2. file myFile.txt: displays about the contents of a given file, whether it is a text (Ascii) or binary file.
3. head myFile.txt: by default it will display the first 10 lines of a file.
If you want first 50 lines you can use head -50 filename or for 37 lines head -37 filename and so forth.
4. tail myFile.txt: by default it will display the last 10 lines of a file.
If you want last 50 lines then you can use tail -50 filename.
5. more myFile.txt: will display a page at a time and then wait for input which is spacebar. For example if you have a file which is 500 lines and you want to read it all.
6. wc myFile.txt: counts the characters, words and lines in a file depending upon the option
7. mv myFile.txt yourFile.txt: is used to move a file from one directory to another directory or to rename a file
8. cp myFile.txt yourFile.txt: is used to copy a file from one directory to another directory/same directory
9. rm myFile.txt: removes the file
10. cmp myfile.txt yourfile.txt: compares two files


Source : Important Linux/Unix Commands

Tuesday, April 3, 2012

Creating Local SVN Repository

In this tutorial I will explain how to create a local Subversion (SVN) repository, intended for a single user. I assume that you already know the benefits of keeping track of old revision of projects or important documents such as a resume or a thesis you have been writing. Subversion offers you a very convenient yet strong method to do so, and the easiest way to do so with Subversion (SVN) is to create a local, home, repository intended for a single user – you.

The repository we are going to create will be used by a single user, working locally on the machine. In this tutorial I will assume the repository will be created for a project called “project1″. “project1″ can be a real project you are doing, a paper you are writing or any thing else that can be stored under revision control (that almost everything).

Creating the repository

We will start by creating the repository. From the command line do the following:
$ mkdir /home/user/svnrep
$ cd ~/svnrep
$ svnadmin create project1

In the first line we create a directory to house all of your repositories (I assume you are working under the username “user”). I prefer to use different repositories for different projects that are unrelated, no matter how small they are. Because I use many repositories, I prefer to have a single directory underneath all repositories will reside in an organized way (i.e. no junk files or any other kind of stuff except directories). Next thing, is to cd into the directory we created, and actually create the repository using the svnadmin command. To repository we’ve created is called “project1″.

The brand new “project1″ repository is currently empty and in revision 0. This will change once we put some data in it. The first thing you do with a new repository is to import initial project data into it.
$ svn import /home/user/project1 file:///home/user/svnrep/project1/trunk -m "Initial import of project1"

Will import the current project1′s file into the repository (assuming that project1 indeed resides in /home/user/project1). The trunk appended to the end of the repository URL, is part of the directory layout convention used by many Subversion users. The last part of the command is the message that will be attached to the import in the SVN log.

Now the repository holds data and you are ready to checkout the code from it, and start working.

Checkout a Working Copy and Start Working

When using SVN (as well as in most revision control systems) you don't work directly on the repository. Instead you checkout a working copy from it, and work on this copy. To checkout a working copy from the repository, use:
$ svn co file:///home/user/svnrep/project1/trunk /home/user/project1_work

This will create a working copy of the repository under /home/user/project1_work. You can edit this copy safely.

After you've done editing your working copy, you will want to commit those changes back to the repository. Assuming you are already inside the directory of the working copy, just do:
$ svn commit -m "Some log message"

This will send you changes back to the repository and store them there. Change the "Some log message" to some useful short description of the changes you've made.

Some Useful Commands

To view a list of log messages that were attached to the operations on the repository, use:
$ svn log
$ svn log -r 5:HEAD

The first command will print all log messages. The second command will print the log messages from revision 5 to the latest revision. You can substitute the HEAD with a number to get the log messages of the revisions up to a specific one.

To view the changes you made before committing your code, use:
$ svn diff


Another important command is the one to update your working copy with the latest revision from the repository. This can be done using:
$ svn update

However, since your are the only user of the local repository, you won't have to use this function often (if at all), unless you use two, or more, working copies for your project.