-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree-to-fs
More file actions
executable file
·56 lines (45 loc) · 1.58 KB
/
tree-to-fs
File metadata and controls
executable file
·56 lines (45 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
# Check for required arguments
if [ $# -ne 2 ]; then
echo "Usage: $0 <parent-folder> <tree-file>"
exit 1
fi
parent_dir="$1"
tree_file="$2"
# Create parent directory if it doesn't exist
mkdir -p "$parent_dir"
declare -a depth_levels
depth_levels[0]="$parent_dir"
while IFS= read -r line; do
# Skip lines that don't contain tree structure
if [[ "$line" =~ ^(.*)(├|└)──[[:space:]]+(.*) ]]; then
indentation="${BASH_REMATCH[1]}"
entry="${BASH_REMATCH[3]}"
# Trim whitespace from entry
entry="$(echo -e "${entry}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
# Calculate depth based on indentation (4 characters per level)
indent_length=${#indentation}
depth=$((indent_length / 4))
# Verify valid depth
if [[ -z "${depth_levels[$depth]}" ]]; then
echo "Error: Invalid tree structure at line: $line"
exit 1
fi
parent="${depth_levels[$depth]}"
if [[ "$entry" == */ ]]; then
# Directory processing
dir_name="${entry%/}"
full_path="$parent/$dir_name"
echo "Creating directory: $full_path"
mkdir -p "$full_path"
# Update depth level for next items
depth_levels[$((depth + 1))]="$full_path"
else
# File processing
full_path="$parent/$entry"
echo "Creating file: $full_path"
touch "$full_path"
fi
fi
done < "$tree_file"
echo "Structure created successfully in: $parent_dir"