When sharing source code, configuration files, or project directories with an AI assistant or performing text-based code analysis, uploading multiple separate files or a ZIP archive can sometimes be tedious. A convenient alternative is consolidating the contents of all files within a directory into a single structured text file.
This guide explains how to recursively extract and merge all files within a specific path into one .txt file via SSH on a Debian server running DirectAdmin, and ensure proper file permissions and ownership.
Benefits of Consolidating into a Single Text File
- AI Context Friendly: Modern Large Language Models (LLMs) can process large context windows in a single prompt.
- Clear File Mapping: Each file's relative or absolute path is preserved as a header before its content.
- No Decompression Needed: Eliminates the step of unzipping archives on remote environments.
Prerequisites
- SSH access to your DirectAdmin server (user or root level).
- Standard bash utilities (
find,cat,sort,chown,chmod).
The Bash Script
Run the following block of code in your terminal. Replace admin or username and example.com with your actual system user and domain path:
# Define user, output file, and source directory
USER="admin"
OUTPUT="/home/$USER/domains/example.com/public_html/allinone.txt"
SOURCE="/home/$USER/domains/example.com/public_html"
# Initialize or clear the output file
> "$OUTPUT"
# Traverse files, append delimiter and file content
find "$SOURCE" -type f ! -path "$OUTPUT" | sort | while read -r file; do
echo "========================================" >> "$OUTPUT"
echo "FILE: $file" >> "$OUTPUT"
echo "========================================" >> "$OUTPUT"
cat "$file" >> "$OUTPUT"
echo -e "\n\n" >> "$OUTPUT"
done
# Set appropriate ownership and permissions
chown "$USER:$USER" "$OUTPUT"
chmod 644 "$OUTPUT"
Single-Line Command
If you prefer a single-line execution with ownership and permission handling included:
> /home/admin/domains/example.com/public_html/allinone.txt && find /home/admin/domains/example.com/public_html/ -type f ! -name "allinone.txt" | sort | while read -r file; do echo -e "\n========================================\nFILE: $file\n========================================" >> /home/admin/domains/example.com/public_html/allinone.txt; cat "$file" >> /home/admin/domains/example.com/public_html/allinone.txt; echo "" >> /home/admin/domains/example.com/public_html/allinone.txt; done && chown admin:admin /home/admin/domains/example.com/public_html/allinone.txt && chmod 644 /home/admin/domains/example.com/public_html/allinone.txt
Why Set Ownership and Permissions?
- File Ownership (
chown username:username): When executing commands as therootuser, newly created files inherit root ownership. Assigning the domain's user ownership ensures the DirectAdmin File Manager and web services can access and modify the file without permission collisions. - File Permissions (
chmod 644): Sets read/write for the owner and read-only for others (-rw-r--r--), ensuring the file is readable by the web server while remaining secure against unauthorized writes.
Excluding Unwanted Directories (Optional)
To exclude large non-text folders such as .git, node_modules, or cache directories, use the -prune option:
USER="admin"
OUTPUT="/home/$USER/domains/example.com/public_html/allinone.txt"
SOURCE="/home/$USER/domains/example.com/public_html"
> "$OUTPUT"
find "$SOURCE" \
\( -path "*/.git" -o -path "*/node_modules" -o -path "*/cache" \) -prune -o \
-type f ! -path "$OUTPUT" | sort | while read -r file; do
echo "========================================" >> "$OUTPUT"
echo "FILE: $file" >> "$OUTPUT"
echo "========================================" >> "$OUTPUT"
cat "$file" >> "$OUTPUT"
echo -e "\n\n" >> "$OUTPUT"
done
chown "$USER:$USER" "$OUTPUT"
chmod 644 "$OUTPUT"
Verification
To verify the generated file details, size, and permissions, run:
ls -la /home/admin/domains/example.com/public_html/allinone.txt
wc -l /home/admin/domains/example.com/public_html/allinone.txt
Security Note: Remember to delete or move allinone.txt outside public_html once your analysis is finished so sensitive data (such as database credentials or API keys) is not publicly accessible via the web browser.
