Operating System - Linux
1821244 Members
3007 Online
109632 Solutions
New Discussion юеВ

dos2unix with find command

 
SOLVED
Go to solution
Danny Fang
Frequent Advisor

dos2unix with find command

Hi,

I would like to use the find command along with the dos2unix utility to locate file containing the ^M character and convert them to the standard UNIX file.

This is what I've done:
find . -type f -name "*.sh" -exec dos2unix {} \;

However, the command above only displayed the file contents(without the ^M) at the std I/O, and in the file itself.

May I know how do I include both the original file to modify and the new output file to the dos2unix command arguments, executed with the find command?
bash-2.03$ dos2unix pollerka pollerka
could not open /dev/kbd to get keyboard type US keyboard assumed
could not get keyboard type US keyboard assumed
bash-2.03$

Also, how do I execute dos2unix in silent mode?

Could anyone help out?
9 REPLIES 9
Vibhor Kumar Agarwal
Esteemed Contributor
Solution

Re: dos2unix with find command

for filename in `find . -type f -name "*.sh"`;
do dos2unix $filename $filename
done;

By the way in HP its dos2ux
Vibhor Kumar Agarwal
Leif Halvarsson_2
Honored Contributor

Re: dos2unix with find command

Hi,

dos2ux writes output to stdout, you must redirect output to a file if you want dos2ux "silent".
dos2ux infile >outfile

then you can move the outfile to the original name

mv outfile infile
Stefan Schulz
Honored Contributor

Re: dos2unix with find command

Hi,

have a look at the manpage. It states, that dos2ux writes to standart output. So the command works as designed.

In your case you have to redirect the output to a filename e.g. dos2ux inputfile > outputfile.

If you want to combine this with a find command you can do a loop for each file find returns. Something like:

for filename in 'find ....'
do
dos2ux $filename > $filename.new
done

Hope this helps

Regards Stefan

No Mouse found. System halted. Press Mousebutton to continue.
Muthukumar_5
Honored Contributor

Re: dos2unix with find command

You can try as,

# find . -type f -name "*.sh" | awk '{ print "dos2unix "$0" "$0".unix" }' | ksh

It will create modified file as .unix

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: dos2unix with find command

You have to use dos2ux in hp-ux ;)

# find . -type f -name "*.sh" | awk '{ print "dos2ux "$0" "$0".out" }' | ksh

hth.
Easy to suggest when don't know about the problem!
Sandman!
Honored Contributor

Re: dos2unix with find command

Yet another way to do this would be to pipe the output of find to xargs in order to get rid of the character:

# find . -type f -name "*.sh" | xargs -i dos2unix {} {}.ux

regards!
Rodney Hills
Honored Contributor

Re: dos2unix with find command

You could use perl-

perl -i -pe 'chomp;print $_,"\n";' `find . -type f -name "*.sh"`

This will in effect "chomp" the trailing cr. The -i option tells perl to read in the original and update it "in place".

HTH

-- Rod Hills
There be dragons...
Danny Fang
Frequent Advisor

Re: dos2unix with find command

Hi Rodney,

I tried your method using PERL, but obtained the error shown below:

bash-2.03$ perl -i -pe 'chomp;print $_,"\n";''\find . -type f -name "pollera"'
String found where operator expected at -e line 1, near "name "pollera""
(Do you need to predeclare name?)
syntax error at -e line 1, near "name "pollera""
Execution of -e aborted due to compilation errors.

May I know where exactly is my error, as I believe I've copied exactly what you showed.

Could you or anyone else help out?

Thanks

Rodney Hills
Honored Contributor

Re: dos2unix with find command

Their are a lot of quotes and spaces in the command, and it would be easy to miss one.

Try copying the command from the web page.

Also, try this command instead-

perl -i -pe 's/\r//g' `find . -type f -name "*.sh"`

The 's/\r//g' is the perl statement to execute. The `find ...` executes the find returning the list of file names to be processed.

HTH

-- Rod Hills
There be dragons...