> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/piscine-life/shell01/count_files.md).

# count\_files

**Objective:**

Write a shell script named `count_files.sh` that counts and displays the number of **regular files and directories** in the current directory and all its subdirectories. The count should include the current directory (`.`).

***

**Key Concepts:**

1. **File and Directory Counting**: Learn how to count files and directories using the `find` command.
2. **Combining Conditions**: Use the `-o` (OR) operator in `find` to search for both files and directories.
3. **Word Count Command**: Use the `wc -l` command to count the number of lines output by `find`.

***

**Script Implementation:**

Here’s the script you will use:

```bash
#!/usr/bin/env bash
find . -type f -o -type d | wc -l
```

***

**Explanation of the Script:**

Let’s break down the script line by line:

1. **`#!/usr/bin/env bash`**:
   * This is the **shebang**. It tells the system to use the `bash` shell to execute the script. Using `#!/usr/bin/env bash` is more portable than `#!/bin/bash` because it finds the `bash` interpreter in the user's environment.
2. **`find . -type f -o -type d`**:

   * The `find` command is used to search for files and directories.
   * `.` specifies the current directory (and its subdirectories) as the starting point for the search.
   * `-type f` tells `find` to look for **regular files**.
   * `-o` is the **OR** operator. It allows `find` to search for either files (`-type f`) or directories (`-type d`).
   * `-type d` tells `find` to look for **directories**.

   **Example**: If your directory contains the following:

   ```
   ./file1.txt
   ./folder1/
   ./folder1/file2.txt
   ./folder1/subfolder/
   ```

   The `find` command will output:

   ```
   .
   ./file1.txt
   ./folder1
   ./folder1/file2.txt
   ./folder1/subfolder
   ```
3. **`wc -l`**:

   * The `wc` command is used to count lines, words, or characters in input.
   * The `-l` option tells `wc` to count the number of **lines**.
   * Since `find` outputs one file or directory per line, `wc -l` will count the total number of files and directories.

   **Example**: If `find` outputs 5 lines, `wc -l` will return:

   ```
   5
   ```

***

**Testing the Script:**

To test your script, follow these steps:

1. **Create Test Files and Directories**:
   * Create a few files and directories in your current directory:

     ```bash
     mkdir -p folder1/subfolder
     touch file1.txt folder1/file2.txt
     ```
2. **Run the Script**:
   * Execute the script:

     ```
     ./count_files.sh
     ```
3. **Expected Output**:
   * The output should be the total number of files and directories, including the current directory (`.`). For the example above, the output will be:

     ```
     5
     ```

***

**Why This Script Works:**

* The `find` command locates all files and directories in the current directory and its subdirectories.
* The `-o` operator ensures that both files and directories are included in the search.
* The `wc -l` command counts the number of lines output by `find`, which corresponds to the total number of files and directories.

***

**Additional Resources:**

* **`find` Command Documentation**: Learn more about the `find` command by typing `man find` or visiting the [GNU Findutils documentation](https://www.gnu.org/software/findutils/manual/html_mono/find.html).
* **`wc` Command Documentation**: Learn more about the `wc` command by typing `man wc` or visiting the [GNU Coreutils documentation](https://www.gnu.org/software/coreutils/manual/coreutils.html#wc-invocation).
* **Bash Scripting Guide**: For more information on writing bash scripts, check out this [Bash scripting tutorial](https://ryanstutorials.net/bash-scripting-tutorial/).

***

**Key Takeaways:**

* You learned how to use the `find` command to search for both files and directories.
* You practiced using the `wc -l` command to count lines of output.
* You combined conditions in `find` using the `-o` (OR) operator.

<br>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/piscine-life/shell01/count_files.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
