A simple Bash script to collect and consolidate files from subdirectories of a root directory into a single _all folder, while preserving their parent directory names as filename prefixes.
This script scans all subdirectories (excluding _all) of a specified root directory and copies all files found into a new _all folder under that root. Each file is renamed using the format:
<parent_directory>_<original_filename> This helps avoid filename collisions and keeps track of each file's origin.
Given the following directory structure:
/data ├── project1 │ └── report.txt ├── project2 │ └── report.txt Running:
./collect_files.sh /dataCreates:
/data/_all/ ├── project1_report.txt ├── project2_report.txt ./collect_files.sh /path/to/root-directory- Replace
/path/to/root-directorywith the actual path to the root directory containing the subfolders. - The script will create (or update) a
_allfolder inside the root directory.
- Files in the
_alldirectory are overwritten if a name collision occurs. - The script skips the
_alldirectory to avoid infinite recursion. - The original directory structure is not modified.
- Bash (tested on GNU Bash 5.1+)
find,dirname,basename, andcputilities (available in most Unix-like systems)