linuxshdos2unix

dos2unix command


I have this script

#!/bin/sh
for i in `ls -R`
do
  echo "Changing $i"
  fromdos $i 
done

I want to remove "^M" charcaters from many files which are in more subdirectories. I got this:

fromdos: Unable to access file

Is there somethig i'm missing?

Thanks in advance.


Solution

  • I guess you don't need a for loop.

    Here is a quick panorama of solutions for files with extension ".ext" (such commands shall be somehow restrictive)

    note : ^M is obtained with CTRL-V" + "CTRL-M"

    # PORTABLE SOLUTION 
    find /home -type f -name "*.ext" -exec sed -i -e 's/^M$//' {} \;
    
    # GNU-sed
    find /home -type f -name "*.ext" -exec sed -i -e "s/\x0D$//g" {} \;
    
    # SED with more recent nux
    find /home -type f -name "*.ext" -exec sed -i -e "s/\r$//g" {} \;
    
    # DOS2UNIX
    find /home -type f -name "*.ext" -print0 | while read -r -d "$(printf "\000")" -r path; do dos2unix $path $path"_new"; done
    
    # AWK
     find /home -type f -name "*.ext" -print0 | while read -r -d "$(printf "\000")" -r path; do awk '{ sub("\r$", ""); print }' $path > $path"_new"; done
    
    # TR
     find /home -type f -name "*.ext" -print0 | while read -r -d "$(printf "\000")" -r path; do cat $path | tr -d '\r' > $path"_new"; done
    
    # PERL
     find /home -type f -name "*.ext" -exec perl -pi -e 's/\r//g' {} \;