โ† scripts 4 min read

How to Search for Files in Linux with Custom File Name and Directory Inputs

In Linux, the find command is a powerful tool for searching files and directories based on various criteria. However, it becomes even more useful when combined with custom scripts that allow user input. This article demonstrates how to create a Bash script that prompts the user to input both a file name and a directory to search in, with specific validation rules for the file name.

Script Requirements

The script will:

The Script

Below is the Bash script that implements these requirements:

#!/bin/bash

# Function to validate file name input
validate_filename() {
    if [[ "$1" =~ ^[a-zA-Z0-9_][a-zA-Z0-9_.*]*$ && "$1" != "*" ]]; then
        return 0
    else
        return 1
    fi
}

# Ask the user for a file name
while true; do
    read -p "Enter the file name (at least 1 character, '*' allowed as wildcard after first character): " filename
    if validate_filename "$filename"; then
        break
    else
        echo "Invalid file name. Please make sure the name contains at least 1 character before using '*' as a wildcard."
    fi
done

# Ask the user for a directory to start the search in
read -p "Enter the directory to start the search in (e.g., '/'): " directory

# Search for the file and display details
find "$directory" -type f -name "$filename" | while read file; do
    echo "File: $file"
    echo "Created: $(stat --format='%w' "$file" | sed 's/?/Not available/')"
    echo "Last modified: $(stat --format='%y' "$file")"
    echo ""
done

Script Breakdown

  1. File Name Validation: The script uses a validate_filename function to ensure that the user provides a file name that starts with at least one valid character (letters, numbers, or underscores). After the first character, the wildcard * can be used. This ensures that file names like index* (which might match index.html, index.php, etc.) are valid, but a single * (which would match all files) is not.

  2. User Input:

    • The user is prompted for a file name. If the input is invalid (e.g., only * is provided), the script will display an error message and ask again until valid input is given.
    • The user is also asked to input the directory to search in. Any valid directory is allowed, including / (the root directory).
  3. File Search:

    • The find command is used to search for the file(s) matching the input file name in the specified directory.
    • For each file found, the script uses the stat command to display the file's creation date (if available) and last modified date. The sed command is used to handle cases where the creation date is unavailable.

Example Usage

When you run the script, it will ask you for a file name and directory:

Enter the file name (at least 1 character, '*' allowed as wildcard after first character): index*
Enter the directory to start the search in (e.g., '/'): /home/user/

The script will then search for files matching index* in the /home/user/ directory and display information like this:

File: /home/user/website/index.html
Created: 2023-07-21 10:45:12
Last modified: 2023-08-10 14:32:09

File: /home/user/projects/index.js
Created: Not available
Last modified: 2023-08-02 09:17:48

Practical Uses

This script is useful in several scenarios:

Conclusion

With just a few lines of Bash, you can create a user-friendly file search tool that validates input and offers flexibility with wildcards and directory selection. This script is highly adaptable and can be extended further to meet more specific needs, such as filtering by file size or date ranges. By incorporating basic validation and user interaction, it provides an intuitive way to leverage the power of Linuxโ€™s find command.