Linux Essential Commands for Removing Spaces

A Guide for System Administrators


Are you working with text files on your Linux computer and facing problems with extra spaces? Whether these spaces are at the beginning or end of your lines, or even between words, managing these spaces is very important. 

It helps in making your data neat for processing and your text easy to read. In this article, we are going to learn some easy Linux commands that are very useful for removing these extra spaces.

Key Features of Space Removal:

Leading Spaces (Starting Spaces):

Leading spaces are the empty spaces before any text in a line. Sometimes we need to remove them for better looking text or for computer work.

Trailing Spaces (Ending Spaces):

Trailing spaces are the empty spaces after the text in a line. They can cause problems in data work or when writing code.

Alternating Spaces (Middle Spaces):

Alternating spaces are the extra spaces between words in a line. Removing them makes the text cleaner.


Below are the examples explained, which help in removing empty spaces from the text file.


| S.No| Heading                                  | Commands                                       |


| 1   | Remove All Spaces with sed               | sed 's/ //g' sample.txt                        |


| 2   | Remove All Spaces with tr                | cat sample.txt | tr -d ' '                     |


| 3   | Remove All Spaces with awk (Rebuild)     | awk '{OFS=""; $1=$1; print}' sample.txt        |


| 4   | Remove Leading Spaces with awk           | awk '{$1=$1; print}' sample.txt                |


| 5   | Remove Trailing Spaces with awk          | awk '{$NF=$NF; print}' sample.txt              |


| 6   | Alternating Spaces with awk (Specific)   | awk '{OFS=""; $1=$1; print $2,$3}' sample.txt  |


Content of sample.txt file:

# cat sample.txt

 This line having space with starting

This line having space with Ending

To Identify the Spaces in the sample.txt file, use grep command with -A option:

# cat -A sample.txt

 This line having space with starting$

This line having space with Ending $

1. Remove All Spaces with `sed`:

   The command `sed 's/ //g' sample.txt` is a very useful one. It removes all the spaces in your file. This is good when you want your text to be tightly packed without any spaces.

2. Remove All Spaces with `tr`:

   If you want to try another easy method, `tr -d ' ' < sample.txt` also removes all spaces. It is a simple and straight way to clean up your text.

3. Remove All Spaces with `awk` (Rebuild):

   `awk '{OFS=""; $1=$1; print}' sample.txt` is another command for making your text clean. It rebuilds each line without any spaces.

4. Remove Leading Spaces with `awk`:

   Extra spaces at the start of your lines can be removed with `awk '{$1=$1; print}' sample.txt`. This makes your text start right from the edge, making it look neat.

5. Remove Trailing Spaces with `awk`:

   The `awk '{$NF=$NF; print}' sample.txt` command is used to take off spaces at the end of your lines. This makes sure your lines end exactly where your text ends.

6. Alternating Spaces with `awk` (Specific):

   To handle extra spaces between your words, `awk '{OFS=""; $1=$1; print $2,$3}' sample.txt` is a good command. It brings your words closer by removing these extra spaces.


Handling spaces in text files is a common task in Linux, and knowing these commands can really help you work better and more accurately. 

Whether you are writing code, looking at data, or just cleaning a document, these methods to remove spaces are very important. Try them out and see how they make your work with text files easier.

Follow Us for More Updates and Articles:

If you enjoyed this article, follow us on social media platforms such as Twitter, Facebook, subscribe to our channel, and connect with us on LinkedIn.