RSS
 

Archive for the ‘Operating Systems’ Category

Unix tips #2: Convert all files from DOS to Unix within a directory

10 Jun

Sometimes, while moving text files from a MS Windows system to a Unix one, we can find a ^M character at the end of each file’s line.

This happens especially when the original files have been modified in MS Windows and then moved to Unix.

A new line in MS Windows is interpreted as a CRLF (Carriage Return, Line Feed) with Hex code 0D0A, while in Unix as a LF, Hex code 0A.

So that ^M is just the ASCII translation of the exceeding CR, 0D.

In Unix we can use the dos2unix utility in order to convert DOS text files to Unix.

The only problem of this utility is that  you cannot overwrite directly the file, but you have to redirect the output to a new file and then replace the original one:


$> dos2unix input.txt > output.txt
$> rm input.txt
$> mv output.txt input.txt

You can imagine how could be nasty when you have to convert more than one file in the same directory.

Well, I have been googling for a while to find a solution and finally I have found very useful the following command:


find . -type f -exec dos2unix {} {} \;

By using the command above you can convert all the files within a directory in one shot!

 

 

Solaris tips #1: How to get process PID bound to a given port

10 Mar

Lately I wasn’t able to startup an application cause the port needed was already used by another process.

An istance of the process I was attempting to run resulted to be idle, but it wasn’t really straight forward to identify that process by executing the command

ps -ef | grep ...

I have found useful a script that I have found in internet and that I have used in the following way.

1. First of all, create a shell script file

vi which_pid_is_bounded_to_port.sh

2. Then type in the following script and save

#!/bin/ksh

line='---------------------------------------------'
pids=$(/usr/bin/ps -ef -o pid=)

if [ $# -eq 0 ]; then
read ans?"Enter port you would like to know pid for: "
else
ans=$1
fi

for f in $pids
do
/usr/proc/bin/pfiles $f 2>/dev/null | /usr/xpg4/bin/grep -q "port: $ans"
if [ $? -eq 0 ]; then
echo $line
echo "Port: $ans is being used by PID:\c"
pargs -l $f
#/usr/bin/ps -o pid,args -p $f
fi
done

3. Now you can run the script and prompt the port in order to find the pid is bound to

. ./which_pid_is_bounded_to_port.sh
Enter port you would like to know pid for: 8080

4. You will get an output that looks like the following:

---------------------------------------------
Port: 8080 is being used by PID:/usr/server/http

5. If you haven’t got the PID in the previous step, you can obtain it with the following:

ps -ef | grep /usr/server/http
user   585 15735   0 16:49:43 pts/12      0:00 grep /usr/server/http
user 17018 13104   0 15:57:44 pts/12      0:25 /usr/server/http

Once you get the PID, you can kill the process if you need it.

kill -9 17018
 

Unix tips #1: Replace multiple white spaces with one blank by using sed

07 Mar

Sometimes is useful to remove from a file multiple white spaces by replacing them with one only blank.

If you need to achieve this, you could probably find useful the sed utility; sed allows you to apply regular expressions to a string / file.

The command below prints the string multiple spaces everywhere

echo "multiple    spaces    everywhere" | sed -e "s/ \+ / /g"

The following example works for a file (blanks are removed from input.txt and the result is store in output.txt):

cat input.txt | sed -e "s/ \+ / /g" > output.txt

Enjoy!