Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Mar 252010
 

Find out which programs are listening TCP port

Answer:

Use the command: netstat -tlnp

# netstat -tlnp

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
Mar 182010
 

Save a file in vim without the needed permissions

Answer:

Very often, you opened a file using vim, edited it and find out that the file is opened as read only, i.e. you don't have permission to make any change(s).

What will you do? Simplest method is to save it into a temp file and rename it afterward.

However, there exist a simple method that allow you to save the file anyway.

Instead of using :wq, you enter the following.

:w !sudo tee %

That is what you need.

Mar 082010
 

Count the number of matching lines from grep

Answer:

grep command is useful for searching file's content by a key string, e.g.

# grep foo text.txt

All the lines matching foo will be printed.

However, to only print the number of matching lines, you can use the -c flag

# grep -c foo text.txt
4

There are total 4 lines match the word foo.