Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
Hi Sarandazzo, if either one of the answers solved your problem (I think there are multiple answers that work), would you be so kind to accept the answer (tick the big "V" below the up/down arrows on the left). It is the appropriate way to indicate the answer worked for you. – Jacob Vlijm Mar 22 at 17:11

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

share|improve this answer
    
Ha, you beat me 9 seconds :) – Jacob Vlijm Mar 9 at 9:16
    
Hi Byte, when run from cron, you need to set the DBUS_SESSION_BUS_ADDRESS environment variable to make editing gsettings work. – Jacob Vlijm Mar 9 at 10:09
    
@JacobVlijm Thanks, but I'm not experienced in cron at all. That's why I just linked the how-to site. Would you mind helping me out? – Byte Commander Mar 9 at 10:18
    
Absolutely, when running commands from cron, the set of env vars is (very) limited, as I once found trying to run commands from cron. Nice post is this one: stackoverflow.com/a/19666729/1391444 Radu's lines, I always use in scripts when editing gsettings from cron. – Jacob Vlijm Mar 9 at 10:21
    
@JacobVlijm Included the env part. is it correct? – Byte Commander Mar 9 at 10:48

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
share|improve this answer
1  
I can confirm that your gsettings schema/key is correct on 15.10. And +1 :) – Byte Commander Mar 9 at 9:24
    
@ByteCommander thanks :) – Jacob Vlijm Mar 9 at 9:24
  1. Use gconftool-2 for nautilus-file-management-properties manpage: http://manpages.ubuntu.com/manpages/hardy/man1/gconftool-2.1.html
  2. Use crontab -e for your schedule. manpage: https://help.ubuntu.com/community/CronHowto
share|improve this answer
1  
I can't tell you exactly since when, but gconf is deprecated and no longer used by at least Nautilus. On my 15.10 system, your setting is no longer available. Also you should describe the steps more clearly instead of just giving hints. – Byte Commander Mar 9 at 9:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.