1

When entering data in my filesystem, some files got into the wrong directory and now I have to reset all of the folders with a specific name, without touching the files in the other folders of that subdirectory. Since all of the directories I want to empty have the same name I assume that this is possible, unfortunately I dont really know how to do that.

I have following structure:

dir

   subdir1

       folder_I_want_to_empty   //all of these folders have the same name

           file_that_needs_to_be_deleted.txt

       folder_I_do_not_want_to_empty   

           file_that_has_to_remain.txt

   subdir2

       folder_I_want_to_empty   //all of these folders have the same name

           file_that_needs_to_be_deleted.txt

       folder_I_do_not_want_to_empty

           file_that_has_to_remain.txt 

   subdir3

       folder_I_want_to_empty   //all of these folders have the same name

           file_that_needs_to_be_deleted.txt

       folder_I_do_not_want_to_empty

           file_that_has_to_remain.txt

How can I empty folder_I_want_to_empty in every directory via the command prompt without deleting the folders or deleting any data from folder_I_do_not_want_to_empty?

3
  • Do you know that name of every file you want to delete or files that you want to keep?
    – kemotep
    Oct 3 '19 at 12:55
  • 1
    I'm really curious about the usage of the words "folder" and "directory". I typically use "folder" to refer to a gui artifact, and "directory" to refer to objects in a filesystem. Are you using "folder" to mean a directory which contains only links to regular files? Oct 3 '19 at 13:53
  • @WilliamPursell I wasn't really thinking about it when I asked the question. I use "folder" when it's a "final" directory that doesn't contain any subdirectories. Probably just a bad translation in my head since Im not a native speaker. :)
    – php_n00b
    Oct 4 '19 at 14:52
0

You can use

rm -fr */folder_I_want_to_empty/* */folder_I_want_to_empty/.??*

Please note that this command is very destructive and does not prompt but instead removes non-interactively without mercy.

If you wish to see what would be deleted, replace rm -fr by ls -ld:

ls -ld */folder_I_want_to_empty/* */folder_I_want_to_empty/.??*

Small print: Due to the pattern used above there is a very small chance that files or folders with only two characters, the first one being a dot . are not deleted. If this is a problem for you let us know in a comment and I will adjust the pattern.

1
  • The command worked, thanks alot! All of the file names are generated with the uniqid() function of php, thus the two character issue wasn't a problem :)
    – php_n00b
    Oct 4 '19 at 14:57
0

Here's how I'd probably do it. for my example, I set up directory /test to use for testing.

find /test -type d -iname folder_I_want_to_empty -print0 | xargs -0 -I % find % -type f -print -delete

First, I find the directories (-type d) called folder_I_want_to_empty. Then for each of those, I have xargs run a find for files within that directory, print their names, and delete them. This doesn't delete subdirectories under folder_I_want_to_empty but it could be made to do so; in your example there aren't any.

Here's an example dockerfile to make this reproducible by readers:

FROM debian
RUN mkdir -p /test
WORKDIR /test
RUN mkdir -p subdir1/folder_I_want_to_empty subdir2/folder_I_want_to_empty subdir1/folder_I_do_not_want_to_empty subdir2/folder_I_do_not_want_to_empty && \
    find /test -mindepth 2 -maxdepth 2 -type d -print0 | xargs -0 -I % touch %/test1 %/test2
ENTRYPOINT [ "/bin/sh" ]
CMD [ "-c" , "find /test -type d -iname folder_I_want_to_empty -print0 | xargs -0 -I % find % -type f -print -delete; find /test -type f" ]

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.