Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Monday, February 6, 2012

Using vi as Line Editor in bash

For folks who are comfortable with vi, use the following command and override the default line editor (emacs) in bash:

$ set -o vi

Now start using vi commands to edit bash commands. It supports both insert and escape modes, just like the vi text editor.

Tuesday, February 23, 2010

Installing xmllint on cygwin

Select the following pacakges
  • libxml
  • libxslt

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

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

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

Wednesday, August 15, 2007

Getopts with bash


#!/bin/sh

usage()
{
echo "Usage: $0 -a all -b ball -c -d -e";
exit 1;
}

if [ $# -lt 1 ] ; then
usage;
fi

# ":" decides which options require an argument
# In the example below options "a" and "b" will require a value to be passed along
while getopts a:b:cde opt
do
case "$opt" in
a) echo "hello $OPTARG";;
b) echo "hello $OPTARG";;
c) echo "c is selected";;
d) echo "d is selected";;
e) echo "e is selected";;
\?) usage;;
esac
done

Wednesday, July 11, 2007

UNIX - find cheet sheet

Find all files named "foo" in directory "/" recursively
find / -name foo

Find all files named "foo" in directory "/"
find / -maxdepth 1 -name foo

Find all files name "foo" in current directory descending recursively
find . -name foo

Find all files starting with "foo" and ending with "bar" in the current directory
find . -name foo*bar
find . -name "foo*bar"

Find all files modified in the last seven days and tar them
find / -type f -mtime -7 | xargs tar -rf weekly_incremental.tar

Find all files named core and delete them
find / -name core | xargs /bin/rm -f
find / -name core -exec '/bin/rm -f {} ;'

Locate files modified less than 10 minutes ago
find / -mmin -10

locate files that are writeable by "others"
find . -perm +o=w

Locate files owned by a user
find . -name core -user ssinghal

Find mtime and mmin options
When specifying time with find options such as -mmin (minutes) or -mtime (24 hour periods, starting from now), you can specify a number "n" to mean exactly n, "-n" to mean less than n, and "+n" to mean more than n.
find . -mtime 0 # find files modified within the past 24 hours
find . -mtime -1 # find files modified within the past 24 hours
find . -mtime 1 # find files modified between 24 and 48 hours ago
find . -mtime +1 # find files modified more than 48 hours ago
find . -mmin +5 -mmin -10 # find files modifed between 6 and 9 minutes ago

Tuesday, July 10, 2007

moving around in bash

when you are in bash , do as emacs does.

The default orientation of bash command line is emacs. And though I am a big vim fan, I have gotten used to emacs mode for command line. And as they say, old habits die hard!

Here are some shortcuts to help you move around on the command line .


  • ctrl + e : takes you to the end of a command

  • ctrl + a : takes you to the beginning of a command

  • esc + f : takes you one word forward

  • esc + b : takes you one word backward

  • esc + d : deletes a word and puts it in a buffer

  • crtl + k : deletes the line from current word and puts it in a buffer

  • ctrl + y : paste contents of buffer