How do I periodically turn off showing hidden files?

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 Ctrl-H. 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?


Solution 1:

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)?

Solution 2:

I will have to check for other releases, but on 14.04 and 15.10, the gsettings command to check visibility of hidden files is:

gsettings get org.gtk.Settings.FileChooser show-hidden
  • To set the value to false (don't show hidden files):

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

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

1. A script to (re) set the value then is:

#!/usr/bin/env python3
import subprocess
import sys
import time

interval = int(sys.argv[1])

key = "org.gtk.Settings.FileChooser"
val = "show-hidden"

while True:
    time.sleep(interval)
    subprocess.Popen(["gsettings", "set", key, val, "false"])

To run it:

  • copy the script into an empty file, save it as reset_hidden.py
  • run it by the command:

    python3 /path/to/reset_hidden.py <interval>
    

    where the time- interval is in seconds, e.g.

    python3 /path/to/reset_hidden.py 600
    

Add it to Startup Applications

To add it to Startup Applications: Dash > Startup Applications > Add. Add the command:

python3 /path/to/reset_hidden.py <interval>

2. Run the command from cron

If you prefer running the command from cron instead of a background script, an important environment variable is not set:

DBUS_SESSION_BUS_ADDRESS

editing/setting gsettings from cron only works if you first set the variable.

If you'd like to run it from cron, make cron run the script below:

#!/bin/bash

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

3. More elegantly, using idle time

If you are working on your computer, you probably wouldn't like to have the files hide unexpectedly while working.

Using the script below, files will be hidden after an arbitrary idle time. The setup is exactly like [1] but the <interval> time is now the <idle_time> (in seconds), before the files should hide (again).

The script needs xprintidle:

sudo apt-get install xprintidle

The script

#!/usr/bin/env python3
import subprocess
import sys
import time

interval = int(sys.argv[1])

key = "org.gtk.Settings.FileChooser"
val = "show-hidden"

idle1 = 0

while True:
    time.sleep(2)
    idle2 = int(subprocess.check_output(["xprintidle"]).decode("utf-8").strip())/1000
    if all([idle1 <= interval, idle2 >= interval]):
        subprocess.Popen(["gsettings", "set", key, val, "false"])
    idle1 = idle2