Ubuntu – How to periodically turn off showing hidden files

bashfilesschedulescripts

So, in order to hide some files and folders to unskilled eyes, I modified their names putting a dot to the start of the name. It works, and files are hidden. Nosy people that are not skilled will not see them. And using Nautilus I turn "show hidden files" on and off by using the key combination CtrlH. Fine. But then I never know if I leave the feature on or off.

I'd like to write a bash script that automatically starts every, say, 10 mins, and will turn off the "show hidden files" feature. So I would be sure that nosy eyes will never see hidden files.
Now the problem is that:

  • I don't know what bash instruction to use, if any. I'm pretty sure that bash scripts can do almost everything, so, please help!
  • I don't know how to automatically start the bash script every xx seconds or minutes.

How do I do it?

Best Answer

You can use gsettings to access the responsible setting in the dconf registry easily from the command line.

The setting whether to show hidden files (with names starting with .) is located in the schema org.gtk.Settings.FileChooser and called show-hidden.

Allowed values are either true (show hidden files) or false (don't show them).

So here are the commands to enable or disable showing the hidden files:

gsettings set org.gtk.Settings.FileChooser show-hidden true
gsettings set org.gtk.Settings.FileChooser show-hidden false

To automatically run this command every x minutes, there are two good resources to learn how to achieve this:

  • Using cron (minimum resolution is 1 minute): help.ubuntu.com: Cron How-to

    Note that cron runs tasks with a very limited set of env variables which do not include DBUS_SESSION_BUS_ADDRESS, but that's needed for gsettings to work. So we have to take care of setting this variable ourselves in the script we run if we need it

    I prepared a script for you (with the help of @JacobVlijm who linked me this answer on Stack Overflow by @Radu Rădeanu) that takes care of this problem and can be run directly by cron:

    #!/bin/bash
    
    # --> Script to disable showing hidden files - to be run as cron job <--
    
    # export DBUS_SESSION_BUS_ADDRESS environment variable
    PID=$(pgrep gnome-session)
    export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
    
    gsettings set org.gtk.Settings.FileChooser show-hidden false
    
  • Without using cron: How to execute command every 10 seconds (without cron)?

Related Question