---
title: "grep: How to Find Empty Lines in a File"
description: "Here is the most useful command to get empty lines from a file using grep in Linux."
url: https://srinimf.com/2021/12/11/grep-how-to-find-empty-lines-in-a-file/
date: 2021-12-11
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2021/12/empty-lines-in-a-file-new.jpg?fit=1200%2C675&ssl=1
categories: ["Shell scripts"]
tags: ["grep to find empty lines", "Linux grep"]
type: post
lang: en
---

# grep: How to Find Empty Lines in a File

Here is a command to find empty lines in a file in Linux. You might be aware **Grep is a popular command (utility) **to search words in a file (dataset). With that, you can find empty lines too. Below is the command.

Empty lines mean blank lines. [You can use cap (^) and Dollar ($) in grep command to display empty lines in a file.](https://www.amazon.in/Command-Shell-Scripts-Pocket-Primer/dp/1683925041?tag=srinimf-21)

Table of contents

1. [Here is sample file](https://srinimf.com/2021/12/11/grep-how-to-find-empty-lines-in-a-file/#here-is-sample-file)
2. [Cap with grep command](https://srinimf.com/2021/12/11/grep-how-to-find-empty-lines-in-a-file/#cap-with-grep-command)
3. [Dollar with grep command](https://srinimf.com/2021/12/11/grep-how-to-find-empty-lines-in-a-file/#dollar-with-grep-command)
4. [Empty lines: use grep with ^$](https://srinimf.com/2021/12/11/grep-how-to-find-empty-lines-in-a-file/#empty-lines-use-grep-with)

## Here is sample file

```
I am strini working for srinimf

Here I will show you how to find empty lines
you are nice person

Thanks for reading the srinimf
```

Here in the above file data, lines 2 and 5 are empty. The same I will get in Ubuntu(Linux).

## Cap with grep command

The purpose of ^ is to get the beginning word. For example, I have a record of Srini coming today.

I will show you [how to](https://srinimf.com/2022/11/28/linux-sed-command-way-to-get-match-criteria/) filter it. Here is the result of how the cap works. It has displayed the record of the first word Srini.

![grep with cap ](https://i0.wp.com/srinimf.com/wp-content/uploads/2021/12/cap.png?resize=581%2C216&ssl=1)

## Dollar with grep command

When you use Dollar in the grep command, it displays the last word. See the result. That means the line that ends with the word. See the usage. Useful for interviews also.

![grep with dollar](https://i0.wp.com/srinimf.com/wp-content/uploads/2021/12/dollar.png?resize=538%2C126&ssl=1)

## Empty lines: use grep with ^$

For empty lines, you need to use both Dollar and cap. How to use these in grep command, let me show you.

```
grep -n ^$ filename.txt
```

The -n displays line numbers. The ^$ filters for empty lines. The input file is filename.txt. Here the line numbers 2 and 6 are empty. So it displayed 2 and 6 in the output.

![grep with cap and dollar to find empty lines](https://i0.wp.com/srinimf.com/wp-content/uploads/2021/12/empty-lines.png?resize=571%2C215&ssl=1)

Summary

1. You can do multiple searches using grep command
2. The ^$ filters and displays empty
3.

**References**

- [Linux Shell Script](https://srinimf.com/2023/09/24/how-to-sort-text-files-a-linux-shell-script/)ing: A Project-Based Approach to Learning
- Top rated bash shell-scripting Udemy course

- Best book to read: [Do Epic Shit](https://www.amazon.in/gp/product/9391165486?tag=srinimf-21)

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2021%2F12%2F11%2Fgrep-how-to-find-empty-lines-in-a-file%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2021%2F12%2Fempty-lines-in-a-file-new.jpg%3Ffit%3D1200%252C675%26ssl%3D1&description=grep%3A%20How%20to%20Find%20Empty%20Lines%20in%20a%20File)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2021/12/11/grep-how-to-find-empty-lines-in-a-file/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2021/12/11/grep-how-to-find-empty-lines-in-a-file/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2021/12/11/grep-how-to-find-empty-lines-in-a-file/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2021/12/11/grep-how-to-find-empty-lines-in-a-file/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20grep%3A%20How%20to%20Find%20Empty%20Lines%20in%20a%20File&body=https%3A%2F%2Fsrinimf.com%2F2021%2F12%2F11%2Fgrep-how-to-find-empty-lines-in-a-file%2F&share=email)
-

### Like this:

Like Loading…
