Search for files with line feed code CR + LF under the current directory

To find a file with a line feed code CR + LF under the current directory on Linux or Cygwin, do the following.

Method 1

This is the method using grep. You have specified -l to display only the file name in grep. (This method will include the binary file in the result, but it may be more meaningful search if you specify the extension as in Example 2).

Addendum (2014/06/23 19:10): It was intended to detect CR + LF, but Currently, the following commands will hit even if only CR is used. If you find a good way, fix it. </ del> I found a good way to fix it.

Addendum (2014/06/24 15:25): I was able to detect CR + LF by the method found by elfmimi.

Example 1


$ find . -type f | xargs grep -lzUP '\r\n'

Example 2


$ find . -name "*.java" | xargs grep -lzUP '\r\n'

Method 2

For the time being, I made my own command in C language and dealt with it (^ ー ^); (This way </ del> also includes binary files in the result)

How to use


$ gcc -o /usr/local/bin/crlf crlf.c
$ find . -type f | xargs crlf

crlf.c


#include <stdio.h>

int main(int argc, char **argv)
{
  if(argc < 2) return 1;
  int i;
  for(i=1; i<argc; i++)
    {
      FILE *fp = fopen(argv[i], "r");
      if(!fp) continue;
      int last = 0;
      int c = 0;
      while((c=fgetc(fp)) != EOF)
        {
          if(last=='\r' && c=='\n')
            {
              printf("%s\n", argv[i]);
              break;
            }
          last=c;
        }
      fclose(fp);
    }
  return 0;
}

Method 3

The method is to take out the output (standard output) when the file command of Linux (or Cygwin) is executed, and output the file name if "with CRLF line terminators" is included. Binary files can be excluded. (Before doing it in the shell, I tried to realize it using C language commands for the time being)

How to use


$ gcc -o /usr/local/bin/textcrlf textcrlf.c
$ find . -type f | xargs textcrlf

textcrlf.c


#include <stdio.h>
#include <string.h>
#define MAXLEN (1024)

int main(int argc, char **argv)
{
  if(argc < 2) return 1;
  int i;
  for(i=1; i<argc; i++)
    {
      static char cmd[MAXLEN];
      sprintf(cmd, "file %s", argv[i]);
      FILE *fpin = popen(cmd, "r");
      if(!fpin) continue;
      static char line[MAXLEN];
      if(!fgets(line, MAXLEN, fpin))
        {
          pclose(fpin);
          continue;
        }
      if(strstr(line, "with CRLF line terminators"))
        {
          printf("%s\n", argv[i]);
        }
      pclose(fpin);
    }
  return 0;
}

Extra edition

I also made a C command to find a file with a line feed code of LF on Linux (or Cygwin). This command also depends on the output of the file command.

How to use


$ gcc -o /usr/local/bin/textlf textlf.c
$ find . -type f | xargs textlf

textlf.c


#include <stdio.h>
#include <string.h>
#define MAXLEN (1024)

int main(int argc, char **argv)
{
  if(argc < 2) return 1;
  int i;
  for(i=1; i<argc; i++)
    {
      static char cmd[MAXLEN];
      sprintf(cmd, "file %s", argv[i]);
      FILE *fpin = popen(cmd, "r");
      if(!fpin) continue;
      static char line[MAXLEN];
      if(!fgets(line, MAXLEN, fpin))
        {
          pclose(fpin);
          continue;
        }
      if(strstr(line, "with CR line terminators"))
        {
          ;
        }
      else if(strstr(line, "with CRLF line terminators"))
        {
          ;
        }
      else if(strstr(line, "ASCII text"))
        {
          printf("%s\n", argv[i]);
        }
      pclose(fpin);
    }
  return 0;
}

Recommended Posts

Search for files with line feed code CR + LF under the current directory
List all files under the current directory line by line with full path
Search for files with the specified extension
Unzip all zip files under the current directory
Search for large files on Linux from the command line
Check what the character code is for all files under the directory that is Python and output
Command for the current directory Python
I tried searching for files under the folder with Python by file name
Delete all pyc files under the specified directory
How to list files under the specified directory in a list (multiple conditions / subdirectory search)
Checks if there is a specific character string for all files under the directory that is Python and outputs the target line
[Boto3] Search for Cognito users with the List Users API
I wrote the code for Japanese sentence generation with DeZero
Recursively copy files from the directory directly under the directory using Python
Google search for the last line of the file in Python
Prevent unnecessary directories from being created under the current directory when you start project with Django