Re: Change dos files to unix files using dos2unix



On Mon, 4 Feb 2008 01:34:09 -0800 (PST), Meendar wrote:
hi,

I'm using following script to change the dos fles (CRLF) to unix file
systems. However i guess this method takes more time, is there any
optimized way to do that?

for file in `find $dir -type f` ; do
if [ "`file $file | grep 'CRLF'`" ] ; then
dos2unix -k -q $file
fi
done
[...]

There are great number of bugs in that code.

You left $dir unquoted, that's asking the shell to perform word
splitting and globbing on its expansion. You need to quote
variables.

Same as for variable expansion, `...` splits into *words* not
lines, and globbing is performed as well. Moreover, the newline
character is as valid as any other character in the name of a
file, so you can't post process the output of find.

[ "$string" ] is the wrong way to test if a string is empty or
not. Use [ -n "$string" ] instead.

But anyway, it's the wrong way to use grep:

if file "$file" | grep -q CRLF; then

But of course, you'll be in trouble for files that have "CRLF"
in their name.

Note that "file" uses heuristics, it's not fool-proof.

You may want to use a version of dos2unix that can do a backup
copy of the original.

find "$dir" -type f \
-exec sh -c '
file "$1" | grep -q "text, CRLF line terminator\$"
' {} {} \; \
-exec dos2unix -k -q {} \;

--
Stephane
.



Relevant Pages

  • Re: Change dos files to unix files using dos2unix
    ... I'm using following script to change the dos fles (CRLF) to unix file ... Same as for variable expansion, ... character is as valid as any other character in the name of a ...
    (comp.unix.programmer)
  • RE: StreamReader - ReadLine
    ... Are you saying it doesn't end in a CrLF or it doesn't end in a Cr or Lf? ... loading it all into a string is probably a bad ... >there is a strange character that looks like a square and I cannot even ... >Dim fsr As StreamReader = File.OpenText ...
    (microsoft.public.dotnet.languages.vb)
  • Re: CRLF in Unix being translated on Mainframe to x25
    ... This will be using FTP. ... Do you mean that the original file has CRLF rather than LF? ... isn't a command just a CR character followed by an LF character. ... the Unix new line indication is LF, ...
    (bit.listserv.ibm-main)
  • Re: Famous Hacker Uses Challenge-Responses
    ... still followed by CRLF for the line breaks. ... RFC 2045 defines a hard line ... line at the 76th character position, or earlier, and that a soft line ... If your client has a problem with its logical ...
    (comp.os.linux.misc)
  • Re: CRLF in Unix being translated on Mainframe to x25
    ... Do you mean that the original file has CRLF rather than LF? ... isn't a command just a CR character followed by an LF character. ... the Unix new line indication is LF, ...
    (bit.listserv.ibm-main)