Removing a newline character at the end of a file

user1257724 picture user1257724 · May 3, 2013 · Viewed 44.9k times · Source

To remove all newlines you could, say:

tr -d '\n' < days.txt
cat days.txt | tr -d '\n'

but how would you use tr to remove just the newline at the end/bottom of a text file?

I'm not sure to specify just the last one.

Answer

Christophe Quintard picture Christophe Quintard · Dec 3, 2014

A simpler solution than the accepted one:

truncate -s -1 <<file>>

From the truncate man page (man truncate):

-s, --size=SIZE
    set or adjust the file size by SIZE
SIZE may also be prefixed by one of the following modifying characters:
    '+' extend by, '-' reduce by, '<' at most, '>' at least, '/' round down
    to multiple of, '%' round up to multiple of.