-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean_recursive.sh
More file actions
executable file
·53 lines (45 loc) · 1.37 KB
/
clean_recursive.sh
File metadata and controls
executable file
·53 lines (45 loc) · 1.37 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
#!/bin/bash
# Configurable batch size
NBATCH=${1:-1000}
# Directories and file types to clean
PATTERNS=("*.log" "__pycache__" "*.so" "*.o" "*.tmp" "*.swp" "*.bak" "*.pyc" "*.pyo")
# Collect all matching files
FILES=()
for pattern in "${PATTERNS[@]}"; do
while IFS= read -r -d '' file; do
FILES+=("$file")
done < <(find . -type f -name "$pattern" -print0 2>/dev/null)
done
# Collect matching directories (like __pycache__)
for pattern in "${PATTERNS[@]}"; do
while IFS= read -r -d '' dir; do
FILES+=("$dir")
done < <(find . -type d -name "$pattern" -print0 2>/dev/null)
done
# Function to confirm and delete in batches
clean_files() {
local count=0
local batch=()
for file in "${FILES[@]}"; do
batch+=("$file")
((count++))
if ((count % NBATCH == 0 || count == ${#FILES[@]})); then
echo "Found ${#batch[@]} files to delete in this batch:"
printf '%s\n' "${batch[@]}"
read -p "Delete these files? (y/n) " CONFIRM
if [[ "$CONFIRM" == "y" ]]; then
rm -rf "${batch[@]}"
echo "Deleted ${#batch[@]} files."
else
echo "Skipping this batch."
fi
batch=()
fi
done
}
# Run cleanup if there are files to delete
if [[ ${#FILES[@]} -eq 0 ]]; then
echo "No files to clean."
else
clean_files
fi