How do I find files that do not end with a newline/linefeed?

后端 未结 12 1248
广开言路
广开言路 2021-02-01 03:45

How can I list normal text (.txt) filenames, that don\'t end with a newline?

e.g.: list (output) this filename:

$ cat a.txt
asdfasdlsad4rand         


        
12条回答
  •  执笔经年
    2021-02-01 04:15

    This should do the trick:

    #!/bin/bash
    
    for file in `find $1 -type f -name "*.txt"`;
    do
            nlines=`tail -n 1 $file | grep '^$' | wc -l`
            if [ $nlines -eq 1 ]
                    then echo $file
            fi
    done;
    

    Call it this way: ./script dir

    E.g. ./script /home/user/Documents/ -> lists all text files in /home/user/Documents ending with \n.

提交回复
热议问题