Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: Finding and deleting directories and files with similar names

  1. #1
    Join Date
    Nov 2008
    Beans
    36

    Finding and deleting directories and files with similar names

    On my computer there are about 900 redundant internet folders with the extension
    "_files". In addition there are the corresponding .htm or .html files. These files
    are not within the folders having the extension "_files". They are within the
    same directories as the folders having the extension "_files"

    The redundant folders are all over the place, in various directories. And now is the
    time to get rid of them. And the associated htm and html files.

    What would be the best way to carry out the purge with a few command-line
    operations?

    I only know how to find the folders:

    $ find ~ -name \*\\_files\* > folders.txt

    produces a text list of the redundant folders I have.

    But then what I would like to do is to

    - take the name of each folder without its _files extension
    - delete the corresponding .htm or .html file
    - delete the folder
    - and move on recursively to other directories

    So how can I capture the name of each folder and use it to
    delete first the similarly named htm/html file and then
    to delete the folder itself?

    Please can you help me? 900+ thanks if you can.
    Last edited by gefalu2008; March 17th, 2013 at 01:20 PM.

  2. #2
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: Finding and deleting directories and files with similar names

    Will this command list 'remove commands for all the files you want to remove, but no other files'? Try it (it only lists the commands due to echo.)
    Code:
    sudo find ~ -name "*_files*" -exec echo rm -r {} \;
    If you are happy with it, remove 'echo' from the line and run the real command.

    Warning: it will remove all files in the directories matching the search pattern. Is this what you want?
    Last edited by sudodus; March 16th, 2013 at 01:25 PM. Reason: warning

  3. #3
    Join Date
    Dec 2007
    Beans
    12,521

    Re: Finding and deleting directories and files with similar names

    Just to be clear, you want to get rid of both the htm/html file and its associated folder?

  4. #4
    Join Date
    Nov 2008
    Beans
    36

    Re: Finding and deleting directories and files with similar names

    Yes that is right, I want to get rid of both the htm/html file and its associated folder.

  5. #5
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: Finding and deleting directories and files with similar names

    Then I think you dare try the command I suggested in post #2

  6. #6
    Join Date
    Nov 2008
    Beans
    36

    Re: Finding and deleting directories and files with similar names

    Thank you - I am new to this so I do not understand what makes this command to delete the htm or html files as well. I must be reading your suggestion somehow incorrectly. To me it seems it deletes only the folders.

    The htm or html files are not inside the folders having the extension "_files". The htm or html files are in the same directories as the folders having the extension "_files".

    I edited my original posting to make it more clear - sorry about having to speak about "files in folders having the extension '_files'".
    Last edited by gefalu2008; March 16th, 2013 at 02:25 PM.

  7. #7
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Finding and deleting directories and files with similar names

    OK so how about something like

    Code:
    while read -r -d $'\0' dir; do echo rm "${dir%_files}".html && echo rmdir "$dir"; done < <(find . -type d -name '*_files' -print0)
    or if you want to use your folders.txt file,

    Code:
    while read -r dir; do echo rm "${dir%_files}".html && echo rmdir "$dir"; done < folders.txt

  8. #8
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: Finding and deleting directories and files with similar names

    Quote Originally Posted by gefalu2008 View Post
    Thank you - I am new to this so I do not understand what makes this command to delete the htm or html files as well. I must be reading your suggestion somehow incorrectly. To me it seems it deletes only the folders.

    The htm or html files are not inside the folders having the extension "_files". The htm or html files are in the same directories as the folders having the extension "_files".
    I see. Have you still got the file with all the files to be deleted, that you wrote about in the opening post (OP)? Then you have the paths to the directories containing the "_files" directories.

    Are these the only htm and html files in each of those particular directories? Or are there other files with the same extensions there? There is a risk that you delete files, that you want to keep, so you probably need to separate them manually, if there are files you want to keep in those directories.

    But if you want to delete them all, you can try the following command
    Code:
     for i in $(sudo find ~ -name "*_files*"|sed s/_files.*/*.htm*/); do echo rm "$i"; done
    Check that it 'wants' to remove the htm and html files you want to get rid of but no other files. If it is OK, you can remove echo from the command and run the real thing.
    Last edited by sudodus; March 16th, 2013 at 02:59 PM. Reason: $i --> "$i"

  9. #9
    Join Date
    Nov 2008
    Beans
    36

    Re: Finding and deleting directories and files with similar names

    Thanks for the words of wardning. I have taken precautions - there is an up to date safety copy of my home folder.

    I think we are close now. The htm/html names contain empty spaces. The code seems to be trying to delete each word of the file names separately. I shall also test the interesting suggestion by Steeldriver.

  10. #10
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Finding and deleting directories and files with similar names

    ... you may need to change the .html to .htm* in my version - if you have both suffixes

Page 1 of 3 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •