DEV Community

Cover image for Shell script that finds all files which correspond to a given template

Shell script that finds all files which correspond to a given template

Andrei Fedotov
I write here some notes
・3 min read

Hello everyone!
Welcome to an ideological continuation of our journey to 1969 year.

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 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

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"
Enter fullscreen mode Exit fullscreen mode

Add the following code to "search" file:

cd "$1"
set +f
for name in *
if [ -d "$name" ]
set -f
sh "$0" "$name" "$2"
set +f
if test "${name#*$2}" != "$name"
echo $name

Enter fullscreen mode Exit fullscreen mode

You can do it with old-school text editor like ED editor

Alt Text

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) 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
Let’s check the work of script for different directories.
Alt Text
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.

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.


Discussion (1)

vlasales profile image
Vlastimil Pospichal • Edited
ls dir1/*test*
ls dir2/*fi*