Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

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

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.

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

Tuesday, September 30, 2008

sudo without password

1. Log in to a unix box as root
2. Execute "visudo" to open sudo editor
3. Enter the following line
ALL=(ALL) NOPASSWD: ALL

Friday, February 8, 2008

subversion : clean non-versioned files from svn source tree

svn status|egrep "^\?"|awk '{print $2}'|xargs rm -rf

ruby - installing from source

1. Download ruby.tar.gz from rubyforge

2. unzip and untar the file

3. Navigate to extracted folder

4. following steps ( like any other source compile on linux)

./configure
make
make install


This will install ruby .

5. To install an extention (say to install openssl on linux)

go to ruby-source/ext/open ssl and execute following steps

ruby extconf.rb
make
make install


Thursday, February 7, 2008

unix find - seach based on permissions

All the files in the current directory that does not have a write permission for user and group

find . -not -perm -ug=w

All files owned by 'ssinghal'

find . -owner ssinghal

Wednesday, January 30, 2008

Regular Expressions Across Multiple Lines

I was looking at this huge java file which had hundreds of SQL statements and thousands of java code interleaved like a spaghetti ! What I really needed was to analyze all the SQL statements in the file since they were all related to a single application which we were supposed to modify. I needed a mechanism to extract all the SQL statements from the file for sake of brevity.

Simple regular expressions using vim or sed did not work since they all deal with a single line, but each one of them provide a mechanism to match regular expression over multiple lines.

sed - the following command will extract all lines between a 'select' and the first ';' following that. Hence you would have printed all the sql queries from a file

sed -ne '/^select/,/;/p' SqlIntensiveFile.java

vim - using the 'g' command can achieve the same. For e.g. the following will delete all the sql queries from the file.

%g/select/,/;/d

Thursday, October 18, 2007

Unix Login Without Password (Setting login keys)

Client : Machine from where you want to log-on
Server : Destination machine where you want to log-on from

1. On the client machine

Create the key(id_rsa) file with appropriate permissions

ssinghal@client$cd ~ssinghal/.ssh
ssinghal@client$touch id_rsa
ssinghal@client$chmod 600 id_rsa

Generate is_rsa file

ssinghal@client$ssh-keygen -t rsa

Press enter when you are prompted for a passphrase or for entering a file name ( accept the default)

Id is stored in ~ssinghal/.ssh/id_rsa

Your identification has been saved in /home/ssinghal/.ssh/id_rsa.
Your public key has been saved in /home/ssinghal/.ssh/id_rsa.pub.
The key fingerprint is:
2c:ce:0d:e4:53:83:d3:41:95:82:5f:cc:e5:8f:a1:6b ssinghal@MACHINENAME


2. copy the id_rsa.pub to server

Using any scp or ftp mechanism

3. On the server machine

SSH to the server machine and create keys file with appropriate permissions

ssinghal@server$cd ~ssinghal/.ssh/
ssinghal@server$touch authorized_keys2
ssinghal@server$chmod 600 authorized_keys2
ssinghal@server$cat id_rsa.pub >> authorized_keys2
( id_rsa.pub is client's public key)


Note: The username on client and server needs to be same ( If I am not mistaken)

Logout and from the client machine try to ssh again, you should be able to log-on with just your username

ssinghal@client$ ssh ssinghal@server

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