Converting windows format file to unix format using script

Hi,

I am having couple of files which i used to copy from windows to Linux, so now in case of text files (CTRL^M) appears at end of line. I know i can convert this windows format file to unix format file by running dos2unix.

My requirement here is that i want to do it automatically using a script i.e. on running a script it should convert all the text files present in a particular directory from windows format to unix format. Now the problem here is all the text files are not having *.txt, some files are present with different file extension but are still text files. So in case if my directory contains any zip file and i run this script blindly to run dos2unix on all the files present in directory, .zip file got corrupted.

So is there any way i can figure out that dos2unix should run only on text files and not on .zip or in other way any means of getting an information that particular file is a text file.

-Sarbjit

You could do something like:

for file in *
do
  ext=${##.file}
  if [ $ext != "zip" ]
  then
    # do your stuff here
  fi
done

Another way would be to use the file-command:

for f in *
do
   ft=$(file $f)
   if [ "$ft" = "$f: ASCII text" ]
   then
       # do your stuff here
   fi
done

If you are using "ftp" to copy text format files from Windows to Unix consider using "ascii" mode not "binary" mode. This will convert the files whether or not they have the ".txt" extension. Always check the process in a test environment because there can be quirks with foreign characrter sets.

If this is not your problem, please state how you are copying the files.

In my office environment we had our unix home / drives mapped through samba server and i directly open my unix drive like a normal windows drive and use standard windows copy/paste procedure while copying file from Windows to unix.

Agreed using file is a good option, but be carefull as the output of file is pretty system specific and you code may not work on another OS or updated version of your OS.

On my OS DOS format files are reported by file as:

test: ASCII text, with CRLF line terminators

While unix formatted files are reported as:

test_unix:ASCII text

And zip files appear as:

test.zip: Zip archive data