Re: Change dos files to unix files using dos2unix
- From: Stephane Chazelas <stephane_chazelas@xxxxxxxx>
- Date: 04 Feb 2008 09:52:30 GMT
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
.
- Follow-Ups:
- Re: Change dos files to unix files using dos2unix
- From: Meendar
- Re: Change dos files to unix files using dos2unix
- References:
- Change dos files to unix files using dos2unix
- From: Meendar
- Change dos files to unix files using dos2unix
- Prev by Date: Change dos files to unix files using dos2unix
- Next by Date: Re: elf core file format
- Previous by thread: Change dos files to unix files using dos2unix
- Next by thread: Re: Change dos files to unix files using dos2unix
- Index(es):
Relevant Pages
|