6

I have a folder /home/userA/folderA this folder contains many files and folders and subfolders. What I want to do is to delete all files that have certain names data.txt and glass.txt. I also want to delete any folder named match with all its contents. I'd be thankful for any advice in how to do this.

14

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 "{}" \; 
| improve this answer | |
  • its giving this error find: invalid expression; I was expecting to find a ')' somewhere but did not see one. – Tak Sep 28 '14 at 17:05
  • Try: find /home/userA/folderA/* -depth -name "data.txt" -type f -exec rm {} \; and find /home/userA/folderA/* -depth -name "glass.txt" -type f -exec rm {} \; – Nathan Adams Sep 28 '14 at 17:15
  • that did it :) thanks! will you update your answer to mark it as answered? – Tak Sep 28 '14 at 17:20
4

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
| improve this answer | |
1

You can simply run the following command (be careful and double check everything because this is not reversible!) This will delete any files that have the word data or glass in it that are .txt files.

sudo rm -rf /home/userA/folderA/*data*.txt

sudo rm -rf /home/userA/folderA/*glass*.txt

You can find any folder named match by using the locate commmand:

locate match

Once you find the paths, simply do the command above for the entire folder

sudo rm -rf /path/to/match

Best, Lev

| improve this answer | |
  • 2
    Mind you: locate is not real-time. It uses a database and if a matching file is not yet in it it will not show with that command. – Rinzwind Sep 28 '14 at 16:40
  • thanks for your answer! regarding the match folder answer, the problem is that i'm expecting more than 100 folder named match in this folder :) this is why I'm asking for an easy solution :) – Tak Sep 28 '14 at 16:41
  • Take a look at the solution below from Nathan for the match problem, it will work great! – levlaz Sep 28 '14 at 16:48
  • You should only use sudo when you really mean it, and there's no need for it here. – evilsoup Sep 29 '14 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.