Convert all EOL (dos->unix) of all files in a dire

2020-02-17 07:56发布

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?

6条回答
来,给爷笑一个
2楼-- · 2020-02-17 08:32

This removes carriage returns from all files in the current directory and all subdirectories, and should work on most Unix-like OSs:

grep -lIUre '\r' | xargs sed -i 's/\r//'
查看更多
神经病院院长
3楼-- · 2020-02-17 08:36

If \r isn't followed by \n (maybe the case in files of Tim Pote):

  • deleting \r (using tr -d) may remove newlines
  • replacing \r with \n may not cause double / triple newlines

Maybe Tim Pote could verify the points above for the files he mentioned.

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-02-17 08:37

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.

tmp=${TMPDIR:-/tmp}/crlf.$$
trap "rm -f $tmp.?; exit 1" 0 1 2 3 13 15

find . -type f -print |
while read name
do
    tr -d '\015' < $name > $tmp.1
    mv $tmp.1 $name
done

rm -f $tmp.?
trap 0
exit 0

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.

查看更多
啃猪蹄的小仙女
5楼-- · 2020-02-17 08:45

You can also use the editor in batch mode.

find . -type f -exec bash -c 'echo -ne "%s/\\\r//\nx\n" | ex "{}" ' \;
查看更多
虎瘦雄心在
6楼-- · 2020-02-17 08:49

You can use sed's -i flag to change the files in-place:

find . -type f -exec sed -i 's/\x0d//g' {} \+

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:

find . -type f -exec sed -i'.OLD' 's/\x0d//g' {} \+
find . -type f -name '*.OLD' -delete
查看更多
在下西门庆
7楼-- · 2020-02-17 08:53

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:

find | xargs perl -pi -e 's/\r\n/\n/g'
查看更多
登录 后发表回答