6

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

maan81
  • 3,469
  • 8
  • 36
  • 53
  • Any decent text editor can do this for you - did you have a programming question ? – Paul R Oct 31 '15 at 10:00
  • 2
    See last word in headline. – Cyrus Oct 31 '15 at 10:10
  • @amdixon I have not installed dos2unix, and would not / cannot install one . – maan81 Oct 31 '15 at 10:30
  • @PaulR I an not able to do it in a (any) text editor coz. I need to check a complete project, which means thousands of files. Not sensible to go through each file / directory manually. – maan81 Oct 31 '15 at 10:32
  • @maan81: if you have a decent text editor then you can just point it at a directory and tell it to do a global search and replace recursively - I do this all the time with BBEdit - I'm sure there must be editors with similar capabilities on Linux. Again, this isn't really a programming question, so it's off-topic for StackOverflow. – Paul R Oct 31 '15 at 10:39
  • @PaulR Really, thank you maan. :-) IDEs would/could certainly solve the problem. I guess I should the mentioned I needed a bash script without any additional requirements. – maan81 Oct 31 '15 at 10:50
  • @maan81: yes, it's always good to be specific as to what you need when you ask a question - it saves everyone a lot of time and effort. – Paul R Oct 31 '15 at 11:46
  • @PaulR Well, I assumed the StackOverflow's tags would be enough. I didnt use the IDE's or anything similar, and used bash, shell & Ubuntu. Probably I should have used Linux. – maan81 Oct 31 '15 at 15:23

1 Answers1

18

NOTE

Be careful when using this command, as it will replace all LF/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.

It is a control code for carriage return. Windows uses the LF/CR notation for line delimiters, while UNIX systems used only 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 .bak appended to the file name).

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

find . -type f -name '*.bak' | xargs -Ix rm x
nehcsivart
  • 734
  • 7
  • 12
  • `x > x`? A safe solution to destroy all files. Take a look at sed's `-i` option. – Cyrus Oct 31 '15 at 10:13
  • 1
    @Cyrus Thanks for pointing this out, I've changed it to use the `-i` option in `sed`. – nehcsivart Oct 31 '15 at 10:18
  • @tchen Thank you very much. I know about the problem with carriage return and the windows & linux format. I wanted a way to correct the problem. I had done a number of google searches but I was not satisfied with the output. :-) – maan81 Oct 31 '15 at 10:29
  • 3
    Do not use this command if you have git submodule inside the directory. – Alex Bender Mar 11 '16 at 13:08
  • @AlexBender and in case it happened...? I just did such a great thing and having hard time recovering (and I don't have even have a remote repo). – Paolo Dec 07 '17 at 19:27
  • @Paolo if you used the commands as-is, it should have made `.bak` files automatically. You can just replace the new files with the `.bak` files. – nehcsivart Dec 07 '17 at 19:46
  • Ok, I did... I had to replace > 20000 files. This is how: `find -iname \*.* | rename -v -f "s/\.bak//g"`. Nothing lost, HTH – Paolo Dec 07 '17 at 19:50
  • 1
    @Paolo I've updated the answer with a note about this at the top. – nehcsivart Dec 07 '17 at 20:04
  • Is there a way to have it ignore the `.git` folder? – Aaron Franke Dec 13 '19 at 07:22
  • @AaronFranke Yes, there are a few ways you can ignore the `.git` directory. One way is to explicitly list the directories you _do_ want to include (e.g. `find a/ b/ -type f ...`; or even `find * -type f ...`, provided that your shell does _not_ expand `*` to hidden files, and that you have no hidden files of your own). I believe `find` has some flags to ignore certain patterns as well, such as the `-regex` flag. – nehcsivart Dec 15 '19 at 11:00
  • This answer gave problems when aplied to git folders, what I used was https://superuser.com/questions/684017/removing-carriage-returns-m-from-certain-lines-without-modifying-every-line just in case anyone else is here for the same reason – Camilo Casadiego Apr 16 '21 at 00:27