Skip to main content
Topic solved
This topic has been marked as solved and requires no further attention.
Topic: How - on shutdown - do you delete any empty directories that exist in /media  (Read 1644 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

How - on shutdown - do you delete any empty directories that exist in /media

I have made my system default to use the /media directory when any external devices are mounted. Mainly due to my using pmount which requires /media to exist.

Due to this I also created /etc/tempfiles.d & put the following in a file called media.conf

Code: [Select]
D /media 0755 root root 0 -

Now my understanding is obviously wrong, as what I thought the media.conf file would do, was to make the /media directory use the tmpfs & therefore cause any empty directories in /media to be deleted on shutdown. The empty directories remain on reboot.

So, to echo the name of this thread again, - How - on shutdown - do you delete any empty directories that exist in /media  ?

I have created /etc/local.d/clear-media.stop with the following command - rm /media/* in an executable file, but it has no effect.

I placed the same file in /etc/init.d & symlinked to it from /runlevels/shutdown (which I expect is a no no) still no go.

So I'm really looking forward to someone showing me just how this seemingly simple task is achieved?

Thanks for reading. :)

Re: How - on shutdown - do you delete any empty directories that exist in /media

Reply #1
Are you talking about the media folder that is in the run directory as this is where external devices are mounted automatically in cinnamon at least. When this was broken, due to lightdm I could not use /run/media/my-folder as a mount point as on every reboot my-folder was deleted. I ended up using /mnt which does not it appear to have an automatic erase.

Re: How - on shutdown - do you delete any empty directories that exist in /media

Reply #2
If I understand correctly, you need to remove directories; the rm command you put in /etc/local.d/clear-media.stop needs the '-r' option to remove directories. Better yet, '-fr' to override any environment '-i' switches that might have crept in.

Re: How - on shutdown - do you delete any empty directories that exist in /media

Reply #3
If you want to delete something automatically check what you are deleting (if empty), because when something fails your rm -rf might erase your device.

Re: How - on shutdown - do you delete any empty directories that exist in /media

Reply #4
@robin0800  I used the following in a file I made /etc/udev/99-udisks2.rules

Code: [Select]
# UDISKS_FILESYSTEM_SHARED
# ==1: mount filesystem to a shared directory (/media/VolumeName)
# ==0: mount filesystem to a private directory (/run/media/$USER/VolumeName)
# See udisks(8)
ENV{ID_FS_USAGE}=="filesystem|other|crypto", ENV{UDISKS_FILESYSTEM_SHARED}="1"

Which successfully turned OFF /run/media/handy & made /media the default location for any external media that I mount (my USB external drives). That works fine, very happy about that bit. :)

What I want (that I can't get as of this writing) is for the /media directory to behave similarly to the /run/media directory, in that when an external device is unmounted, the empty directory that it was mounted to, is removed. I'll happily settle for this to happen on reboot/shutdown.

As mentioned in previous post, I've tried by creating the /etc/tempfiles.d directory &  putting a file in there with a string in it that I'm told on the ArchWiki will make /media use the tempfs. I'm thinking that perhaps that command string only works with systemd? I don't know, I'm operating above my pay grade at the mo'. I may be misunderstanding just exactly what "make /media use the tmpfs" means?

@nous & @SGOrava When I use the following command in the CLI, it removes the empty directories & it won't touch a directory that has any files in it:

Code: [Select]
sudo rmdir /media/*

So I think that this command is the one that I want, I don't want a command that will delete the contents of a directory that is holding one of my external drives. Because I WILL forget, & I WILL lose the lot, without that protection.

@nous Using rmdir which has been specially created to only remove empty directories, I don't have many (next to none) options available, so the -i won't help me here.

I was just looking at the permissions of the /tmp directory, it has rwx for all, it also has a t on the end. I would have thought that if I'd set /media to be using the tempfs that it would have a t too.

I'll see if I can get anywhere by searching about making a directory use tempfs. I'll let you know what I find out.

[edit:] OK, I've learned something important, I really don't want /media to use tmpfs as that means that the contents of the external devices that I mount will be stored in RAM. No way do I want that.

So I just want to be able to delete any empty directories on shutdown or reboot (preferably shutdown)?

Re: How - on shutdown - do you delete any empty directories that exist in /media

Reply #5
OK, I've finally found a way that actually checks for empy directories in /media & deletes them, leaving any directories that have one or more files in them. :D :D :D

I was looking into writing a script to do this (the hard way...), when I discovered the following very nice command string:

Code: [Select]
find . -type d -empty -delete

So now my /etc/local.d/clear-media.stop file looks like this:

Code: [Select]
#!/bin/bash
## This file's purpose is to delete any empty directories in /media
## as pmount creates them (as do I from time to time):

cd /media
find . -type d -empty -delete

& it works perfectly.  8)

Re: How - on shutdown - do you delete any empty directories that exist in /media

Reply #6
I use "if [-f or -d] specify path to file or directory;  then do something"  # checks for existence of a file or a directory
else "do something else"  # file or directory does not exist.

 

Re: How - on shutdown - do you delete any empty directories that exist in /media

Reply #7
Use this command and try again to delete folder with files

sudo rmdir /media/*

or check full guide of remove or delete directory in linux