How to remove trailing whitespaces in a file on Linux

Last updated on August 2, 2020 by Dan Nanni

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: sed

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 commands. 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

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.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean