> 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/find_sh.md).

# find\_sh

**Objective:**

Write a shell script named `find_sh.sh` that searches for all files ending with `.sh` in the current directory and its subdirectories. The script should display only the file names **without the `.sh` extension**.

***

**Key Concepts:**

1. **File Searching**: Learn how to search for files in directories and subdirectories using the `find` command.
2. **Command Substitution**: Use the `basename` command to remove the `.sh` extension from file names.
3. **Shell Scripting**: Write a script that automates the process of finding and formatting file names.

***

**Script Implementation:**

Here’s the script you will use:

```bash
#!/usr/bin/env bash
find . -type f -name '*.sh' -exec basename -s .sh {} \;
```

***

**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 -name '*.sh'`**:

   * 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 only for **files** (not directories).
   * `-name '*.sh'` filters the results to only include files that end with `.sh`.

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

   ```
   ./script1.sh
   ./folder/script2.sh
   ./folder/subfolder/script3.sh
   ```

   The `find` command will output:

   ```
   ./script1.sh
   ./folder/script2.sh
   ./folder/subfolder/script3.sh
   ```
3. **`-exec basename -s .sh {} \;`**:

   * The `-exec` option allows you to run a command on each file found by `find`.
   * `basename -s .sh {}`:
     * `basename` is a command that strips directory paths and optionally removes a suffix from a file name.
     * `-s .sh` tells `basename` to remove the `.sh` suffix from the file name.
     * `{}` is a placeholder for the current file found by `find`.
   * `\;` indicates the end of the `-exec` command.

   **Example**: For the file `./script1.sh`, `basename -s .sh ./script1.sh` will output:

   ```
   script1
   ```
4. **Final Output**:

   * The script will output the names of all `.sh` files without the `.sh` extension, one per line.

   **Example Output**:

   ```
   script1
   script2
   script3
   ```

***

**Testing the Script:**

To test your script, follow these steps:

1. **Create Test Files**:
   * Create a few `.sh` files in your directory and subdirectories:

     ```bash
     mkdir -p folder/subfolder
     touch script1.sh folder/script2.sh folder/subfolder/script3.sh
     ```
2. **Run the Script**:
   * Execute the script:

     ```
     ./find_sh.sh
     ```
3. **Expected Output**:
   * The output should be:

     ```
     script1
     script2
     script3
     ```

***

**Why This Script Works:**

* The `find` command locates all `.sh` files in the current directory and its subdirectories.
* The `basename -s .sh` command removes the `.sh` extension from each file name.
* The `-exec` option allows `find` to process each file individually.

***

**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).
* **`basename` Command Documentation**: Learn more about the `basename` command by typing `man basename` or visiting the [GNU Coreutils documentation](https://www.gnu.org/software/coreutils/manual/coreutils.html#basename-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 files in directories and subdirectories.
* You practiced using the `basename` command to remove file extensions.
* You worked with the `-exec` option to process files found by `find`.


---

# 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/find_sh.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.
