# Remove files having 0 byte or only header

**URL:** <https://community.unix.com/t/remove-files-having-0-byte-or-only-header/365618>\
**Category:** Shell Programming and Scripting\
**Created:** [March 16, 2017, 11:03am UTC](https://community.unix.com/t/remove-files-having-0-byte-or-only-header/365618 "2017-03-16T11:03:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ace\_friends22](https://community.unix.com/letter_avatar/ace_friends22/32/6_449c533f586939b6e5f246632a46421f.png) [@ace\_friends22](https://community.unix.com/u/ace_friends22)\
**Post date:** [March 16, 2017, 11:03am UTC](https://community.unix.com/t/remove-files-having-0-byte-or-only-header/365618/1 "2017-03-16T11:03:42Z")

</div>

Hi Team,

I'm looking for a command which removes files having 0 byte of having only header line (1 line).

My ETL process generates these files. Few files are not having header, in that case if no data from source, it will be 0 byte and few files are having header, in that case if no data from source, it will generate a byte with header information only, but no data.

I found the command to remove file with 0 byte

```nohighlight
find . -type f -size 0 -exec rm {} \;

```

But not able to find command to remove file having only one line (header). I tried below command, but it is not working

```nohighlight
find . -type f -size 0 -exec rm {} \;

```

I need it in form of command, as I do not want to create shell script to run at the end of ETL process

---

<div class="post-metadata">

**Author:** ![digioleg54](https://community.unix.com/letter_avatar/digioleg54/32/6_449c533f586939b6e5f246632a46421f.png) [@digioleg54](https://community.unix.com/u/digioleg54)\
**Post date:** [March 16, 2017, 12:23pm UTC](https://community.unix.com/t/remove-files-having-0-byte-or-only-header/365618/2 "2017-03-16T12:23:57Z")

</div>

find them by time

```nohighlight
 find . -ctime -1
 

```

1 is what was created for the last 24 hours

---

<div class="post-metadata">

**Author:** ![ace\_friends22](https://community.unix.com/letter_avatar/ace_friends22/32/6_449c533f586939b6e5f246632a46421f.png) [@ace\_friends22](https://community.unix.com/u/ace_friends22)\
**Post date:** [March 16, 2017, 12:43pm UTC](https://community.unix.com/t/remove-files-having-0-byte-or-only-header/365618/3 "2017-03-16T12:43:27Z")

</div>

Hi digioleg54, I want to remove file which have 0 in size or have only one row.

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_449c533f586939b6e5f246632a46421f.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [March 16, 2017, 1:00pm UTC](https://community.unix.com/t/remove-files-having-0-byte-or-only-header/365618/4 "2017-03-16T13:00:58Z")

</div>

Okay. You need to describe how to tell a header-only file from a file you want to keep.  
If is simply size. Pretend example -

You want to delete 36 byte files and smaller, this finds files smaller than 37 and created in the last 24 hours:

```nohighlight
find /path/to/files -type f -size -37c -ctime -1 -exec rm {} \;

```

One line files work this way:

```nohighlight
find /path/to/files -type f -ctime -1 |
while read fname 
do  
       sz=`cat $fname | wc -l` # Not a UUOC done to get just a line count
       [$sz -lt 2] && rm $fname
done

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [March 16, 2017, 3:03pm UTC](https://community.unix.com/t/remove-files-having-0-byte-or-only-header/365618/5 "2017-03-16T15:03:37Z")

</div>

> [@jim mcnamara](#):
>
> Okay. You need to describe how to tell a header-only file from a file you want to keep.  
> If is simply size. Pretend example -
> 
> You want to delete 36 byte files and smaller, this finds files smaller than 37 and created in the last 24 hours:
> 
> ```plaintext
> find /path/to/files -type f -size -37c -ctime -1 -exec rm {} \;
> 
> ```
> 
> One line files work this way:
> 
> ```plaintext
> find /path/to/files -type f -ctime -1 |
> while read fname 
> do  
> sz=`cat $fname | wc -l` # Not a UUOC done to get just a line count
> [$sz -lt 2] && rm $fname
> done
> 
> ```

While it is true that:

```plaintext
wc -l "$fname"

```

pollutes the output with an unwanted filename:

```plaintext
cat $fname | wc -l

```

is still an unneeded use of `cat` . Try:

```plaintext
wc -l < "$fname"

```

instead.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 16, 2017, 3:25pm UTC](https://community.unix.com/t/remove-files-having-0-byte-or-only-header/365618/6 "2017-03-16T15:25:56Z")

</div>

The `-ctime -1` was not required anywhere.

> [@ace\_friends22](#):
>
> .  
> .  
> .  
> I need it in form of command, as I do not want to create shell script to run at the end of ETL process

I'm afraid this requirement is vain; you should go for an adequate/applicable SOLUTION. Try this shell script

```plaintext
wc -l * 2> /dev/null | while read CNT FN; do [$CNT -ge 2] && ["$FN" != total] && echo rm "$FN"; done 

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [March 16, 2017, 3:55pm UTC](https://community.unix.com/t/remove-files-having-0-byte-or-only-header/365618/7 "2017-03-16T15:55:55Z")

</div>

> [@rudic](#):
>
> The `-ctime -1` was not required anywhere.
> 
> I'm afraid this requirement is vain; you should go for an adequate/applicable SOLUTION. Try this shell script
> 
> ```plaintext
> wc -l * 2> /dev/null | while read CNT FN; do [$CNT -ge 2] && ["$FN" != total] && echo rm "$FN"; done 
> 
> ```

I thought the goal here was to remove files with no lines or 1 line. Isn't the test above backwards. Shouldn't that be:

```plaintext
wc -l * 2> /dev/null | while read CNT FN; do [$CNT -lt 2] && ["$FN" != total] && echo rm "$FN"; done 

```

and then rerun it without the `echo` shown in red above if the list looks right?

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 16, 2017, 5:31pm UTC](https://community.unix.com/t/remove-files-having-0-byte-or-only-header/365618/8 "2017-03-16T17:31:06Z")

</div>

Absolutely. Thanks for pointing this out!
