How to remove trailing whitespaces in a file on Linux

Question: I have a text file in which I need to remove all trailing whitespsaces (e.g., spaces and tabs) in each line for formatting purpose. Is there a quick and easy Linux command line tool I can use for this?

When you are writing code for your program, you must understand that there are standard coding styles to follow. For example, "trailing whitespaces" are typically considered evil because when they get into a code repository for revision control, they can cause a lot of problems and confusion (e.g., "false diffs"). Many IDEs and text editors are capable of highlighting and automatically trimming trailing whitepsaces at the end of each line.

Here are a few ways to remove trailing whitespaces in Linux command-line environment.

Method One

A simple command line approach to remove unwanted whitespaces is via sed.

The following command deletes all spaces and tabs at the end of each line in input.java.

$ sed -i 's/[[:space:]]*$//' input.java

If there are multiple files that need trailing whitespaces removed, you can use a combination of find and sed. For example, the following command deletes trailing whitespaces in all *.java files recursively found in the current directory as well as all its sub-directories.

$ find . -name "*.java" -type f -print0 | xargs -0 sed -i 's/[[:space:]]*$//'

Method Two

Vim text editor is able to highlight and trim whitespaces in a file as well.

To highlight all trailing whitespaces in a file, open the file with Vim editor and enable text highlighting by typing the following in Vim command line mode.

:set hlsearch

Then search for trailing whitespaces by typing:

/\s\+$

This will show all trailing spaces and tabs found throughout the file.

Then to clean up all trailing whitespaces in a file with Vim, type the following Vim command.

:%s/\s\+$//

This command means substituting all whitespace characters found at the end of the line (\s\+$) with no character.

Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

4 thoughts on “How to remove trailing whitespaces in a file on Linux

  1. This is for emacs users.

    To highlight all trailing blanks:

    M-x set-variable RET show-trailing-whitespace RET 1 RET

    A mode to make all whitespaces visible

    M-x whitespace-mode

    Remove all trailing whitespaces in the selection

    M-x delete-trailing-whitespace RET

    It is of course also possible to use regex replacement (M-C-% is query-replace-regexp).

    M-C-% \s-+$ RET RET

    \s- is the emacs character class for whitespaces but it is also possible to use [[:space:]]+$ or [_TAB]+$ where _ represents a space and TAB a tab character.

  2. I also want to point out that combining find and xargs should not be necessary anymore. The -exec option takes care of that. The syntax is

    find [options] -exec [command] [args]

    The command argument should contain a {} that represents a filename and should end with ; or with +.
    If ; is used (be sure to escape it properly) then the command will be called for each file.
    If + is used the command will be called for multiple files at once (i.e. {} represents multiple files)

    For example, the find command from the article can be replaced by

    find . -name "*.java" -type f -exec echo sed -i 's/[[:space:]]*$//' {} +

    or if you prefer to run sed once per file

    find . -name "*.java" -type f -exec echo sed -i 's/[[:space:]]*$//' {} \;

    Just remove the 'echo' to do the real work.

  3. Method Three:

    In emacs you can use the command:

    ‘M-x delete-trailing-whitespace’

    or to automatically run the command just before saving the file, place this command in your .emacs file:

    (add-hook 'before-save-hook 'delete-trailing-whitespace)

    This will keep all your files free of trailing whitespace!

Leave a comment

Your email address will not be published. Required fields are marked *