V
V
vunigeni2021-03-11 09:56:11
linux
vunigeni, 2021-03-11 09:56:11

How to delete empty folders?

Good afternoon.
There is a directory with many files and folders, you need to delete files older than 15 days + then delete empty folders and subfolders. I made two scripts for this case and put them in cron.
Script No. 1 fulfills, everything is ok.
But Script #2 deletes empty folders only in the parent directory, and there is no subfolder. For example, if there is another empty folder in an empty folder of the parent directory, then it skips it.
What needs to be specified in the command so that the deletion occurs recursively?
if I want to watch porn I choose the best here https://bestfreepornsites.pro
Script #1: find /common/"Shared Folder"/ -type f -mtime +15 -exec rm {} ;
Script #2: find /common/"Shared Folder"/ -type d -empty -exec rmdir {} ;

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
Rsa97, 2021-03-11
@Rsa97

# man find
...
 -depth
    Always true. Causes descent of the directory hierarchy to be done
so that all entries in a directory are acted on before the directory
itself.

Z
Zzzz9, 2021-03-11
@Zzzz9

For example, if there is another empty folder in an empty folder of the parent directory, then it skips it.

If there is a directory in the parent directory, even an empty one, then the parent directory is not empty.

D
Dmitry, 2021-03-11
@q2digger

why two scripts, if you can do everything in one line ..
-exec rm -rf {} \;

A
Alexey Leontiev, 2021-03-11
@BEERsk

rmdir must be run with the -p switch, then it deletes all parent empty directories, something like this:
Script #2: find /common/"SharedFolder"/ -type d -empty -exec rmdir -p {} ;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question