---
title: Avoid ls piped to grep (prefer globs or find)
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Static Code Analysis (SAST) > SAST
  Rules > Avoid ls piped to grep (prefer globs or find)
---

# Avoid ls piped to grep (prefer globs or find)

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ().
{% /alert %}

{% /callout %}

## Metadata{% #metadata %}

**ID:** `bash-code-quality/avoid-ls-grep-pipelines`

**Language:** Bash

**Severity:** Notice

**Category:** Code Style

## Description{% #description %}

Parsing `ls` output is fragile (locale, columns, odd filenames). Piping `ls` to `grep` is a common anti-pattern. Prefer shell globs, `find`, or tools that work on paths instead of `ls` text.

## Non-Compliant Code Examples{% #non-compliant-code-examples %}

```bash
#!/bin/bash
ls | grep foo
ls -la | grep '\.txt$'
ls |& grep foo
```

## Compliant Code Examples{% #compliant-code-examples %}

```bash
#!/bin/bash
ls
printf '%s\n' *.txt
find . -maxdepth 1 -name '*.log' -print
ls | something | grep
```
  Seamless integrations. Try Datadog Code SecurityDatadog Code Security 
{% icon name="icon-external-link" /%}
 