

# File listing functions

Use this function to list the files in Amazon S3 whose paths match a glob pattern, for example to check which files a pattern matches before you read them.

For a description of the sample tables and files that the examples on this page use, see [Table functions in DuckDB](duckdb-functions-table.md).

## `glob` table function
<a name="athena-duckdb-functions-utility-glob"></a>

Returns one row for each file whose path matches `pattern`, with the full path of the file in the `file` column. Use it to see which files a pattern matches before you read them with a function such as `read_parquet` or `read_csv`. You can also use it to find the partition folders under a table location.

In the pattern, `*` matches any characters within one part of the path but not `/`, and `**` matches any number of folder levels. The wildcard `?` matches one character, and `[abc]` matches one character from the set. Pass a list of patterns to list the files that match any of them. If no file matches, the function returns no rows.

To test whether a string matches a pattern, use the `GLOB` operator instead. For more information about the operator, see [`GLOB` operator](duckdb-functions-string-pattern-matching.md#athena-duckdb-functions-string-glob).

### Syntax
<a name="athena-duckdb-functions-utility-glob-syntax"></a>

```sql
glob({{pattern}})
glob({{pattern_list}})
```

### Arguments
<a name="athena-duckdb-functions-utility-glob-arguments"></a>

The `glob` table function takes the following arguments:

**{{pattern}}**  
An Amazon S3 path that can contain wildcards, as a `VARCHAR` value, for example `'s3://amzn-s3-demo-bucket/tables/sales_csv/*/*'`.

**{{pattern\_list}}**  
A list of such paths, as a `VARCHAR[]` value.

### Return type
<a name="athena-duckdb-functions-utility-glob-return-type"></a>

A table with one row per matching file and one column, `file` (`VARCHAR`), that holds the full Amazon S3 path of the file.

### Examples
<a name="athena-duckdb-functions-utility-glob-examples"></a>

The following SQL query lists the partition folders of a partitioned table location. It extracts the folder name from each file path.

```sql
SELECT DISTINCT
    regexp_extract(file, 'region=[a-z]+') AS partition
FROM glob(
    's3://amzn-s3-demo-bucket/tables/sales_csv/*/*'
)
ORDER BY partition
```

The query returns the following results.


| partition | 
| --- | 
| `region=east` | 
| `region=north` | 
| `region=west` | 

The following SQL query uses `**` to match the files at any folder level under the table location.

```sql
SELECT DISTINCT
    regexp_extract(file, 'region=[a-z]+') AS partition
FROM glob(
    's3://amzn-s3-demo-bucket/tables/sales_csv/**'
)
ORDER BY partition
```

The query returns the following results.


| partition | 
| --- | 
| `region=east` | 
| `region=north` | 
| `region=west` | 

The following SQL query passes a list of two patterns and returns the partitions whose files match either one.

```sql
SELECT DISTINCT
    regexp_extract(file, 'region=[a-z]+') AS partition
FROM glob([
    's3://amzn-s3-demo-bucket/tables/sales_csv/region=east/*',
    's3://amzn-s3-demo-bucket/tables/sales_csv/region=west/*'
])
ORDER BY partition
```

The query returns the following results.


| partition | 
| --- | 
| `region=east` | 
| `region=west` | 

The following SQL query uses a pattern that matches no file, because the table has no `region=south` partition. The function returns no rows, so the count is `0`.

```sql
SELECT count(*) AS file_count
FROM glob(
    's3://amzn-s3-demo-bucket/tables/sales_csv/region=south/*'
)
```

The query returns the following result.


| file\_count | 
| --- | 
| `0` | 

### Considerations
<a name="athena-duckdb-functions-utility-glob-considerations"></a>

A partitioned table stores its files in folders under the table location, such as `region=east/`. A pattern that ends in `/*` directly after the table location matches only files in the location itself. To include the files in the partition folders, use `/*/*` or `/**`.