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
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
teereproduce, appending (because of optiona), each file submited argument, become on standard input. - bash feature: "
here strings" (seeman -pless\ +/here.strings bash), usecommand <<<"here string""in replacement ofecho "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
Post a Comment