Pages

Wednesday, March 28, 2012

Arithmetic equations in bash

Basic equations in bash scripts:

x=15
y=8
sum=$((x+y))

dif=$((x-y))

mod=$((x%y))

prod=$((x*y))

Tuesday, March 6, 2012

How to search a word in multiple files

1. Search a word in all the files with the .txt extension in current directory.

 grep -H "word" *.txt

2. Search a word in all the files in current directory and its subdirectories. 

grep -H -r "word" home/directory

-H = display the name of the file
-r = search recursively trough subdirectories

Friday, February 24, 2012

How to remove/replace a given character/word from all the filenames in current directory using sed

#!/bin/bash
extension="txt"
for files in *.$extension; 
do 
    mv $files `echo $files | sed 's/B/A/g'`; 
done;
 
The mv command is used to rename the files.

sed parses the filename and replaces all the occurrences of "B" character with "A".

eg. BAOBAB.txt will be replaced by AAOAAA.txt

The script is case sensitive. Also, be careful with special characters that need to be escaped with "\".
For example if you have a file called ^name.txt and want to replace "^" character from the filename with "A", using the command:

mv $files `echo $files | sed 's/^/A/g'`;  

will lead to the following output:

A^name.txt

You need to escape "^" character:


mv $files `echo $files | sed 's/\^/A/g'`;

In this case the output will be:

Aname.txt

This script also works if you use words instead of characters.

Monday, February 13, 2012

Install Xara Xtreme on Ubuntu 10.04

Xara Xtreme is a leading-edge graphic tool, easy-to-use and one of the world's best free graphics program.

Xara Xtreme  is supplied in autopackage format. Follow the next steps to install the program:
  1. Download the Xara Xtreme Linux installation package (http://www.xaraxtreme.org)
  2. Make this package executable:
    • Click on "Properties".
    • Click on the "Permissions" tab.
    • Ensure the checkbox "Allow executing file as program" is checked.
    • Click on "Close".
  3. Double click on .package file
  4. The installer will begin by prompting you whether to download the autopackage code (if autopackage is not already installed). Answer yes (or simply press Enter).
  5. After some time, it will display a window asking for the administrator (sudo) password. This is to install autopackage system-wide.
  6. After autopackage has finished installing, Xara installation will begin. You may be prompted for the password again.
  7. When Xara has finished installing, you should have a Xara icon in your application menu (e.g. Applications > Graphics > Xara Xtreme)

Download autopackage archive from