Files
duplicacy-cli-utils/list_directory_structure.sh
2024-08-09 19:39:22 -04:00

48 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
# This script generates a directory structure that mirrors the file tree of a Duplicacy snapshot.
# Duplicacy gives a command to list the files in a snapshot, but it doesn't provide a way to visualize the directory structure.
# Check if a snapshot ID is provided as an argument.
if [ $# -ne 1 ]; then
echo "Usage: $0 <snapshot-id>"
exit 1
fi
# Define the snapshot ID from the argument.
SNAPSHOT_ID="$1"
# Define a temporary working directory.
WORKING_DIR="./duplicacy_snapshot_tree"
# Create the working directory.
mkdir -p "$WORKING_DIR"
# Change to the working directory.
cd "$WORKING_DIR" || exit
# Generate the list of files in the snapshot and process them.
duplicacy list -r "$SNAPSHOT_ID" -files | awk 'NR > 4 {lines[NR] = substr($0, index($0, $5))} END {for (i = 5; i < NR; i++) print lines[i]}' | while IFS= read -r file_path; do
# Trim trailing whitespace that might have been added
file_path=$(echo "$file_path" | sed 's/[[:space:]]*$//')
# Check if file_path is non-empty.
if [[ -n "$file_path" ]]; then
if [[ "$file_path" == */* ]]; then
# Create the directory structure for each file path.
mkdir -p "$(dirname "$file_path")"
fi
# Create empty files to simulate the snapshot's file tree.
touch "$file_path"
fi
done
# Use the `tree` command to display the directory structure, including hidden files.
tree -a
# Change back to the original directory.
cd ..
# Clean up the generated directory structure.
rm -rf "$WORKING_DIR"