Shell script that finds all files which correspond to a given template
Tags: shell, bash, unix, linux
Published: 2019-11-15 17:53:33

---
title: Shell script that finds all files which correspond to a given template
published: true
description: In today’s short trip we will develop a program with using of shell language. We won’t to use find command. Our program will search all files whose names correspond to a given template. The search will be in a given subtree of the file structure.
tags: shell, bash, unix, linux
cover_image: https://thepracticaldev.s3.amazonaws.com/i/pn1dshyrsc46mjjer2wa.jpg
---

Hello everyone!
Welcome to an ideological continuation of [our journey to 1969 year](https://dev.to/koscheyscrag/back-to-1969-or-introduction-to-ed-old-school-text-editor-ffo).

__Introduction__
In today’s short trip we will develop a program with using of shell language. We won’t to use [find](https://renenyffenegger.ch/notes/Linux/shell/commands/find) command. Our program will search all files whose names correspond to a given template. The search will be in a given sub-tree of the file structure.

The result of the execution will be a list of the names of the desired files on the screen. The program will consist of two scripts. The first is the main script. It displays the prompt to enter the path name for initial directory and ask to type the search template (mask). Then it performs the input of this data from the keyboard and displays a list of the searched files in the initial search directory (if there are any). Then it calls a nested script for each subdirectory, passing it two input parameters: the relative name of the subdirectory and the search pattern.

__Let’s go!__

First, create files in the directories so that the script can work with them.


![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/lmy1hzjp0ndmx8mbjgio.png)

Next, create two files: "search" and "runsearch". The first one is a search program, and the second is designed to run.

Add the following code to "runsearch" file (the code's explanation is below):

```
read  p "Catalog: " catalog
set -f
read -p "Search mask: " mask
sh ~/../home/admin/dirC/search "$catalog" "$mask"
```

Add the following code to "search" file:
```
cd "$1"
set +f
for name in *
do
if [ -d "$name" ]
then
set -f
sh "$0" "$name" "$2"
set +f
else
if test "${name#*$2}" != "$name"
then
echo $name
fi
fi
done

```
You can do it with old-school text editor like [ED editor](https://dev.to/koscheyscrag/back-to-1969-or-introduction-to-ed-old-school-text-editor-ffo)

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/fohz6flkxizrsjc9suum.png)

__Code explanation__
`$1` – refers to the first argument given to the script at execution. For the script executed in the following manner: `sh search catalog mask`, it refers to the catalog name given as a first argument;
`$2` - refers to the second argument (mask);
`$0` - refers to the name of the script (search);
`set -f` is the portable (i.e. [POSIX](https://en.wikipedia.org/wiki/POSIX)) way to disable filename expansion.
When it is enabled (by default or with set +f), filename expansion is an operation performed by the shell that replaces, when possible, command line arguments containing:
• occurrences of wildcards (? = any single character and * = any number of characters)
• ranges enclosed in square brackets (e.g. [a-z12] = any character from a to z, or 1 or 2)
• non-matching lists (e.g. [^a-z] = any character not in the range a to z)
• and character classes (e.g. [[:xdigit:]] = any character that can be used to represent an hexadecimal number)
by the file names that match them.
When disabled, these arguments are left unchanged.

Let's move next. Do scripts executable:
`chmod +x ./.runsearch`
`chmod +x ./.search`

Make sure that they have become such (displayed in a different color).
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/8xrum50iuyrft4xvsro2.png)
Let’s check the work of script for different directories.
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/e5ew9agf98kkrg89n3m2.png)
So, as we see when we run the script it asks us to input the catalog name and search mask. In the result it gives us the list of files which correspond to the mask in the given directory and sub-directories.

__Conclusion__
We have developed a script that searches in a given subtree of the file structure of all files whose names correspond to a given template.

__Cheers!__