How do I convert all EOL (dos->unix) of all files in a directory and sub-directories recursively without dos2unix
? (I do not have it and cannot install it.)
Is there a way to do it using tr -d '\r'
and pipes? If so, how?
How do I convert all EOL (dos->unix) of all files in a directory and sub-directories recursively without dos2unix
? (I do not have it and cannot install it.)
Is there a way to do it using tr -d '\r'
and pipes? If so, how?
This removes carriage returns from all files in the current directory and all subdirectories, and should work on most Unix-like OSs:
If
\r
isn't followed by\n
(maybe the case in files of Tim Pote):\r
(usingtr -d
) may remove newlines\r
with\n
may not cause double / triple newlinesMaybe Tim Pote could verify the points above for the files he mentioned.
Do you have sane file names and directory names without spaces, etc in them?
If so, it is not too hard. If you've got to deal with arbitrary names containing newlines and spaces, etc, then you have to work harder than this.
The trap stuff ensures you don't get temporary files left around. There other tricks you can pull, with more random names for your temporary file names. You don't normally need them unless you work in a hostile environment.
You can also use the editor in batch mode.
You can use sed's
-i
flag to change the files in-place:If I were you, I would keep the files around to make sure the operation went okay. Then you can delete the temporary files when you get done. This can be done like so:
For all files in current directory you can do it with a Perl one-liner:
perl -pi -e 's/\r\n/\n/g' *
(stolen from here)EDIT: And with a small modification you can do subdirectory recursion: