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:
- Ask the user for a file name, with the condition that the name must start with at least one character. After the first character, the wildcard
*can be used. - Ask the user for a directory to search in. The root directory
/is valid, but any other directory can also be specified. - Use the
findcommand to locate the files and display information such as creation and modification dates.
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
-
File Name Validation: The script uses a
validate_filenamefunction 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 likeindex*(which might matchindex.html,index.php, etc.) are valid, but a single*(which would match all files) is not. -
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).
- The user is prompted for a file name. If the input is invalid (e.g., only
-
File Search:
- The
findcommand 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
statcommand to display the file's creation date (if available) and last modified date. Thesedcommand is used to handle cases where the creation date is unavailable.
- The
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:
- Wildcard Searches: By allowing wildcards after the first character, users can search for files like
log*to find all log-related files orindex*for all index files. - Customized Directory Search: Users can specify the directory they want to search in, whether itโs a system folder like
/var/log/or a personal folder like/home/user/. - File Metadata: The script not only finds the files but also provides valuable information about each file, such as when it was created and last modified.
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.