bash 如何递归搜索和删除目录中所有文件中的^M?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33450269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:51:23  来源:igfitidea点击:

How to search and remove ^M in all files in a directory recursively?

bashshellubuntu

提问by maan81

Files edited in Windows have M^ at end lines. How would I remove them ?

在 Windows 中编辑的文件在结束行有 M^。我将如何删除它们?

回答by nehcsivart

NOTE

Be careful when using this command, as it will replace allLF/CR character sequence regardless of position or context. Take extra care in situations where you have binary files or files that have special configuration formats. Directories with git submodules is an example that will likely have problems with this command, as the character sequence has already been stored as persistent commits.

笔记

使用此命令时要小心,因为无论位置或上下文如何,它都会替换所有LF/CR 字符序列。在您有二进制文件或具有特殊配置格式的文件的情况下要格外小心。带有 git 子模块的目录就是一个例子,这个命令可能会出现问题,因为字符序列已经存储为持久提交。

It is a control code for carriage return. Windows uses the LF/CR notation for line delimiters, while UNIX systems used only LF.

它是回车的控制代码。Windows 使用 LF/CR 表示法作为行分隔符,而 UNIX 系统仅使用 LF。

Below should do it for all files recursively down the file system tree relative to the current directory.

下面应该对相对于当前目录的文件系统树中的所有文件递归执行此操作。

find . -type f | xargs -Ix sed -i.bak -r 's/\r//g' x

The code above will make automatic backups (with extension .bakappended to the file name).

上面的代码将进行自动备份(扩展名.bak附加到文件名)。

Once you made sure the files are good, use the following to delete the backups.

确定文件完好后,请使用以下命令删除备份。

find . -type f -name '*.bak' | xargs -Ix rm x