1

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.

1
  • The variable $orig is undefined. Commented Mar 25, 2011 at 18:52

2 Answers 2

2

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 {} \;
Sign up to request clarification or add additional context in comments.

4 Comments

Glad to hear it! If you'd hit the little 'accept answer' button, I'd be very grateful :-)
adding path to find would be nice :-)
@hornetbzz Sorry, the find variant I usually use doesn't require it, so I rarely pass it.
thx for the edit. I added the comment as I experienced a similar bad use of find/rm, as this may happen to everbody if not explicit :-)
1

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' {} \;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.