dos2unix command

2020-04-19 06:51发布

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.

2条回答
叛逆
2楼-- · 2020-04-19 07:26

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' {} \;
查看更多
仙女界的扛把子
3楼-- · 2020-04-19 07:30

ls -R lists everything, including directories. So you're telling fromdos to act on actual directories is some cases.

Try something like this:

find . -type f -exec fromdos {} \;
查看更多
登录 后发表回答