1

I'm looking for the best way to handle essentially performing a Unix2Dos on all files with a certain extension .nux.

I actually want to rename the extension to .nun.

Right now my code manually pushes into each hard coded directory to achieve this but I would think / hope that there's a cleaner way to do this. A code that could recursively dive into every folder below /testpath/temp/ and "rename" the .nux file(s) to .nun and change the Linux line endings to Windows line endings.

BASH VERSION 3.2.57(2)-release

pushd /testpath/temp/xyz
if [[ -f PRXYZ.NUX ]]
  then
    sed 's/$/\r/' PRXYZ.NUX > PRXYZ.NUN
    rm PRXYZ.NUX
fi
popd

pushd /testpath/temp/abc
if [[ -f PRABC.NUX ]]
  then
    sed 's/$/\r/' PRABC.NUX > PRABC.NUN
    rm PRABC.NUX
fi
popd

pushd /testpath/temp/lmn
if [[ -f PRLMN.NUX ]]
  then
    sed 's/$/\r/' PRLMN.NUX > PRLMN.NUN
    rm PRLMN.NUX
fi
popd
1
  • 1
    Look up find and its exec option. Something like find . -name "*.nux" -exec sed [...] should do what you want. This command is inclompete, and completing it is left as an exercise to the reader ;)
    – Panki
    Aug 30, 2019 at 14:00

1 Answer 1

2

For newline conversions, there's dos2unix package, available on linux systems, providinf unix2dos, which you can use to convert the files, instead of using sed:

-ascii Convert only line breaks. This is the default conversion mode.

and

-l, --newline Add additional newline.

dos2unix: Only DOS line breaks are changed to two Unix line breaks. In Mac mode only Mac line breaks are changed to two Unix line breaks.

unix2dos: Only Unix line breaks are changed to two DOS line breaks. In Mac mode Unix line breaks are changed to two Mac line breaks.

And, as per Panki's comment, you can use find to grab the files, rename them and convert:

find /testpath/temp/ -type f -name "*.NUX" \
    -exec sh -c 'unix2dos "$1"; mv "$1" "${1%.NUX}.NUN"' _ {} \;
2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .