I want to delete all files contained in a folder and his subfolder recursively without deleteing folders.
For example I have this structure :
MainFolder
│ Somefile6
│ Somefile7
│
└───child1
│ | Somefile
│ | Somefile2
│ │
│ └───child2
│ │ Somefile3
│ │ Somefile4
│ │
│ └───child3
│ │ Somefile5
After running a bash script (what I tried) :
#!/bin/bash
shopt -s nullglob dotglob # Include hidden file
dir=(/root/some/path/*)
if [ ${#dir[@]} -gt 0 ]; then
for file in "$dir"
do
echo "Doing some custom action before deleting..."
echo "Deleting $file"
rm "$file"
sleep 1
done
else
echo "The folder is empty";
fi
What I expect to obtain
MainFolder
│
└───child1
│ │
│ └───child2
│ │
│ └───child3
│ │
Problem:
It's just deleting the files Somefile6
and Somefile7