How would you use the “find” command in a Shell script to recursively search for all files with a specific extension and then append the results to a new text file?

1 Answers
Answered by suresh

Using the "find" Command in a Shell Script to Search for Files

Using the "find" Command in a Shell Script

In a Shell script, you can use the find command to recursively search for all files with a specific extension and then append the results to a new text file. Here's how you can achieve this:


#!/bin/bash
extension=".txt"  # specify the file extension you are looking for
output_file="results.txt"  # specify the name of the output text file

find /path/to/directory -type f -name "*$extension" >> $output_file

echo "Search results have been saved to $output_file."

This script sets the extension variable to the specific file extension you want to search for, and the output_file variable to the name of the new text file where the search results will be appended. Replace /path/to/directory with the actual directory path you want to search in.

After running this script, the results of the search for files with the specified extension will be saved in the results.txt file.

Answer for Question: How would you use the “find” command in a Shell script to recursively search for all files with a specific extension and then append the results to a new text file?