Delete files and folders with specific name from a certain directory
You can delete the files and folders in the subdirectories of folderA
.
To remove the files, run:
find /home/userA/folderA/* -type f \( -name "data.txt" -or -name "glass.txt" \) -delete
and to remove the folders match
:
find /home/userA/folderA/* -depth -name "match" -type d -exec rm -rf "{}" \;
And the verbose (python) option:
#!/usr/bin/env python3
import os
import shutil
# --------------------------------------------------------
reorg_dir = "/path/to/your/folder"
remove_files = ("data.txt", "glass.txt")
remove_dirs = ("match")
# ---------------------------------------------------------
for root, dirs, files in os.walk(reorg_dir):
for name in files:
if name in remove_files:
os.remove(root+"/"+name)
for dr in dirs:
if dr in remove_dirs:
shutil.rmtree(root+"/"+dr)
Copy the script into an empty file, set the directory and if you want/need: edit the list of files and folders to remove, save it as reorg.py and run it by the command:
python3 /path/to/reorg.py