pick-a-file is a command-line utility written in Rust that allows users to select a random file from a specified directory based on one or more given file extensions. It supports an optional command-line switch to control output behavior.
- File Selection: Choose a file from a specified directory that matches one or more given file extensions.
- Output Control: Use the
--bareflag to return only the selected file's path tostdout. This is great for command substitution or integration into scripts.
cargo run [--bare] <path> <file_extension1> <file_extension2> ...<path>: The directory to search for files.<file_extension1>,<file_extension2>: The file extensions to filter files by (e.g.,.txt,.jpg,.git).
To run the program and select files with .txt and .jpg extensions from the documents directory:
cargo run /path/to/documents .txt .jpgTo return only the selected file's path using the --bare option:
cargo run --bare /path/to/documents .txt .jpgThis tool is particularly handy when used in conjunction with Unix(-like) terminals. It allows for seamless integration into scripts or workflows, for when you want to test another tool with a random file of a specific type.
-
Direct Invocation: You can invoke
pick-a-filedirectly in the terminal to select a random file of a specified type from your home directory:pick-a-file ~ .ext -
Command Substitution: Use command substitution to pass the selected file as an argument to another command. For example, if you want to open a random
.txtfile withnano:nano "$(pick-a-file --bare ~/documents .txt)"
Or, if you prefer the backtick syntax:
nano "`pick-a-file --bare ~/documents .txt`"Note that double quotes will make sure paths with spaces are handled correctly.
- Scripting: Incorporate
pick-a-fileinto shell scripts for automated tasks. For instance, you can create a script that processes a random image file:#!/bin/bash image="$(pick-a-file --bare ~/images .jpg)" display "$image" # Assuming you have a command to display images
-
Clone the repository:
git clone https://github.com/yourusername/pick-a-file.git
-
Navigate to the project directory:
cd pick-a-file -
Build the project:
cargo build
