linux - How to add newline character at the end of many files -


this question has answer here:

i've lot of php files , i'd fix newline character after last line (if not present) bash script.

is there command to easily?

thanks :)

short , quick

tee tool you're searching for:

simply:

tee -a <<<'' file1 file2 ... 

or

find /path -type f -name '*.php' -exec tee -a <<<'' {} + 

warning: don't miss -a option!

it's quick, add newline on each files.

(you whipe in second command sed '${/^$/d}' -i file1 file2 ... empty last lines in files. ;)

... ok, requested.

some explanation:

from man tee:

name        tee - read standard input , write standard output , files  synopsis        tee [option]... [file]...  description        copy standard input each file, , standard output.         -a, --append               append given files, not overwrite 
  • so tee reproduce, appending (because of option a), each file submited argument, become on standard input.
  • feature: "here strings" (see man -pless\ +/here.strings bash), use command <<<"here string"" in replacement of echo "here string"| command. this, bash add newline submited string (even empty string: <<<'').

slower, stronger

stay quick because of limited forks, 1 fork tail -c1 have done each files anyway!

find . -type f -name '*.php' -exec bash -c '      file in $@ ;do          ifs= read -d "" foo < <(tail -c1 $file);          [ "$foo" != $'\''\n'\'' ] && echo >> $file;        done' -- {} + 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -