Showing posts with label unix. Show all posts
Showing posts with label unix. Show all posts

Tuesday, February 7, 2012

Using key mapping in vi

vi or vim provide a very useful technique whereby user can assign arbitrary functions to control key strokes. For eg, here is one such mapping:

map ^k I/*A*/

When a hits ctrl+k, the line on which the cursor currently is, is commented. To explain the command:
map ^k - assigns the functionality to ctrl+k character
I/*A*/ - concatenation of smaller operations which are
- Move to Esc mode,
I - Insert in the beginning of the line
/* - characters to be inserted
- Move to Esc mode
A - Append at the end of the line
*/ - characters to be appended
- move to Escape mode

The Esc key can be typed by first typing Ctrl+V followed by Esc key.

By adding this mapping to .vimrc file, this mapping can be saved across vim invocations.

Monday, October 3, 2011

Google Code Page for Unix Utils

 Adding some of my utility shell scripts to a common google page.

http://code.google.com/p/nixtils/ 

Monday, July 25, 2011

Linux - See which process is using which port

netstat -anop

Then use ps and grep to find the process using this port

Tuesday, February 23, 2010

Installing xmllint on cygwin

Select the following pacakges
  • libxml
  • libxslt

Saturday, November 7, 2009

Find lines of code in your repository


for ext in "*.java" "*.xml" "*.css" "*.jsp" "*.js" "*.sql"
do
echo -n "$ext,$location,"
find ./$location -iname "$ext" -exec wc -l '{}' \;|gawk '{total+=$1} END{print total}'
done

Sunday, May 10, 2009

bash : search and replace from previous command

tar -cvf sandeep.tar sandeep/

!!:gs/sandeep/singhal will mean

tar -cvf singhal.tar singhal/

:gs is for global search and replace

bash - last argument of the previous command (nth argument)

  • Get the last argument of the previous command : !!$
  • Get the first argumnet of the previous command : !!^
  • Get the 1st argument of the previous command : !!1
  • Get the nth argument of the previous command : !!n

Examples : 

$> ls :!!$  (or !$) 

$> ls :!!^

$> ls :!!2

$> ls :!!n

Monday, February 16, 2009

Cron entry for other users

Execute the following as root :

cat /var/spool/cron/ssinghal

where ssinghal is the user name

Friday, February 6, 2009

Sed - Deleting lines matching a pattern

sed -i '/.*log$/d' file_names.txt

Deletes all the lines ending with "log" from the file "file_names.txt"

Monday, January 26, 2009

sed : inplace replacement

It is easy to open a file in vim and do a search replace - but if the file runs into huge sizes, a regular editor would not be able to perform search and replace tasks on it efficiently. For these situation, you need a "stream" editor. Here is an example for replacing "foo" with "bar" in a file called snafu.txt


sed -i -e 's/foo/bar/g' snafu.txt


Where:

-i : Is for inplace replacement

-e: sed script to be run on the file

samba mount

Using samba to mount a windows shared folder

mount -t cifs -o username=Home\\sandeep //192.168.0.1/Shared/ /media/Shared

Where /media/Shared is the target folder where the network folder will be mounted.

Sunday, January 25, 2009

Executing a command remotely using ssh

Following command will run a shell script on a remote machine

ssh ssinghal@192.168.0.1 /tomcat/jakarta/bin/kill.sh

Friday, December 26, 2008

Capturing output of terminal on Unix

Terminals do not have a great scroll length and even though you can increase the number of lines that can be scrolled up, it is always useful to have the output captured in a file so that you can search on it. Here are few ways

1. Redirection

cat foo.txt > bar.txt

Outout of foo.txt is captured in bar.txt

2. Redirection with "append"

ls >> bar.txt

This will append the output of "ls" to bar.txt ( Instead of overwriting it)

3. "tee"
Redirect and redirect with append will not display the content on console and hence if you want to see the content and also redirect it to a file , use "tee"

ls | tee bar.txt

this will display the contents of ls on terminal and also redirect it to bar.txt

4. output to multiple files

ls | tee a.txt b.txt c.txt

This will output contents of ls to a.txt, b.txt, c.txt

5. Outout all the terminal interaction to a file

script -f out.txt

Monday, December 22, 2008

Converting UTF-16 to UTF-8

I was running grep on an xml file (big one) and realized that it did not match the pattern I was expecting the the file. I then did a "head" from the file to see a pattern that is present in it. Grepping on that too returned nothing telling me that grep is somehow not working on this file.
"head" was returning some junk characters at the begining of the file - This idicated that the file does not have a normal text encoding and probably that is the reason why grep is not working on it.
Reading the XML header proved that, since it was "UTF-18", grep was not running over it. I had to convert the file to UTF-8 which is more text friendly if the content is only English.

iconv --from-code UTF-16 --to-code UTF-8 input_file.xml > output_file.xml

Saturday, December 13, 2008

screen

when to I use screen :

* When "nohup" is just not enough
* When the power goes off and you are left in the middle of nowhere
* When I need to log on to 10 different terminals each day
* When I do not want to enter 10 different username passwords each day

"screen" is a terrific unix utility that allows you to start a terminal without having to worry about you job being interreputed mid-way. It is better than nohup in that respect nohup only allows unix commands to be started in a "nohup" fashion. If for instance you are working with an interactive program like sqlplus and your internet connection happen to break, you will loose your context and any transactions that you were working on.

It essentially allows you following :
* Multiple sessions in a single ssh window (screen multiplexing)
* Ability to "resume" a session in the state you left it - irrespective of power outage, internet connection termination etc

Here is how you use it :

1. Make sure it is installed on your system by typing "screen --help"
2. If it is not installed, you can install it by "yum install screen" on a fedora box.
3. start a screen session by typing "screen" and you should be in a screen session.
4. If you are disconnected or closed the terminal, type "screen -r -d" to reattach to the session

Here are some of the short-cuts available (All after CTRL+A)

c : creates a new
n : next screen window
p : previous screen window
a : toggle between two screen windows
[0-9] : Go to a specific screen window identified by a number
A : Set the name of a screen window
" : List all the existing screen
ESC : Scroll up or down on a screen

Friday, October 17, 2008

Renaming Files in Linux

Type the following on commandline of a bash shell and change the logic as needed.


for i in `ls /home/ssinghal` ; do mv "$i" "$i.new" ; done

Thursday, June 12, 2008

nmap - portscan

scan all the ports in a given range on a specific machine

nmap -p1-10000 127.0.0.1

The above command will scan host 127.0.0.1(localhost) to see if any of the ports from range 1 to 10000 is up.