3

Is it possible to specify a new pattern to match files and folders I wish to hide from an OS's file manager (Explorer, Finder, etc.)?

When using some editors (in my case Emacs), automatic backups will be created that are not by-default hidden from the normal user interface of the system. It would be useful to be able to specify a new pattern to match hidden files/folders, such as

((\.|_).+)| ; normal hidden files in *nix and dos, respectively
.*~         ; emacs backup files
#.+#        ; emacs autosave files

Is this possible, and (if so) how can it be done? I'd like solutions for all platforms, if possible.

Oliver Salzburg
  • 87,539
  • 63
  • 263
  • 308
Sean Allred
  • 1,252

4 Answers4

3

No, the definition of a hidden file is not configurable in most operating systems. Windows uses a file attribute, and Unix-based systems hard-code the dot convention throughout the operating system and tools.

However, it is possible to customize Emacs to hide the files.

(setq backup-directory-alist
      `((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
      `((".*" ,temporary-file-directory t)))

This will place all Emacs backup and autosave files into your system temporary directory, out of your way. For more information, see Backup Directory on Emacs Wiki.

Other tools provide similar features, but there is no universal convention, so you will need to check documentation and configure each individually.

Bradd Szonye
  • 1,249
  • Good to know, but I was hoping to find a solution that integrates with the default explorers - not everyone uses Emacs ;). I yet ask because I believe that the developers of these operating systems are smart enough not to have the patterns hard-coded, implying that they can be modified. – Sean Allred Apr 18 '13 at 23:03
  • 1
    Unfortunately, the hidden file conventions really are hard-coded. On Unix, that's because the feature requires the cooperation of many tools and libraries (e.g., bash, glob, and readline), and they use simple code to check for a leading dot. From glob.c: if (noglob_dot_filenames == 0 && pat[0] != '.' ... ) – Bradd Szonye Apr 18 '13 at 23:47
  • As for developer smarts: Unix's hidden dot files aren't that way by design, they're the result of a useful bug that became a standard: https://plus.google.com/101960720994009339267/posts/R58WgWwN9jp – Bradd Szonye Apr 18 '13 at 23:58
  • Well, that is depressing. Nonetheless, I'll wait and see if anyone finds a hack before I award the bounty. (Very good mini-article, by the way.) – Sean Allred Apr 19 '13 at 09:28
  • Thanks! It's possible that somebody has tweaked a Unix shell to customize this. The standard tools mostly go in the other direction, with options to make dot files more visible. For example, the match-hidden-files option to readline: "This variable, when set to ‘on’, causes Readline to match files whose names begin with a ‘.’ (hidden files) when performing filename completion. If set to ‘off’, the leading ‘.’ must be supplied by the user in the filename to be completed." (Also note how the definition of a hidden file is written right into the docs!) – Bradd Szonye Apr 19 '13 at 20:23
  • 1
    It truly is fascinating how total accidents end up becoming de facto standards. – Sean Allred Apr 19 '13 at 20:28
  • 2
    ls is configurable though: in your alias for ls put some combination of --hide='.~' (if you still want to see them with -a) or --ignore='#.'. Or you can use --color=auto and use dircolors to set the LS_COLORS var to something that doesn't stand out (so you can still see them, but they won't catch your eye.) – Wandering Logic Apr 23 '13 at 12:37
  • 3
    Also: bash has GLOBIGNORE. I haven't tried it but the man page says, "A colon-separated list of patterns defining the set of filenames to be ignored by pathname expansion. If a filename matched by a pathname expansion pattern also matches one of the patterns in GLOBIGNORE, it is removed from the list of matches." – Wandering Logic Apr 23 '13 at 12:44
  • 1
    @WanderingLogic Good catch! That would cover the main situations where hidden files matter. You should write this up as an answer so that vermiculus can award the bounty to you. – Bradd Szonye Apr 23 '13 at 20:02
  • @WanderingLogic Absolutely!! This is fantastic! – Sean Allred Apr 23 '13 at 20:21
  • @vermiculus So apparently it'd deeply hardcoded into the system tools and documents, but also configurable. I should have known. :) – Bradd Szonye Apr 23 '13 at 20:50
3

Your question said you wanted to hide the files from Finder or Explorer. I am a cranky old man, so I don't know what those are. I use bash and ls in an xterm as my "file explorer". ;-]

bash has an environment variable you can set called GLOBIGNORE. I haven't tried it but the man page says,

A colon-separated list of patterns defining the set of filenames to be ignored by pathname expansion. If a filename matched by a pathname expansion pattern also matches one of the patterns in GLOBIGNORE, it is removed from the list of matches.

That should cover things like autocompletion inside bash itself, but I don't know if any other programs pay attention to the GLOBIGNORE envvar.

ls is also configurable. In your alias for ls put some combination of --hide='.*~' (if you still want to see them with ls -a) or --ignore='#.*'. Or you can use --color=auto and use dircolors to set the LS_COLORS var to something that doesn't stand out (so you can still see them, but they won't catch your eye.)

0

Well, you could try a hack for OSX or other *nix. It is not elegant, and may cause other problems, but you could simply write a little script that monitors a directory for files matching your pattern and renames them to dot files. If you then run this script as a cronjob all matching files will be renamed to hidden ones. For example:

#!/bin/env bash
while true; do 
  find ~/ -name "*~" -o -name "\#*\#"| ## find files matching your pattern
   while IFS= read -r n; do            ## save each file as $n
     d=`dirname $n`;                   ## $d is the directory $n was found in
     b=`basename $n`;                  ## $b is the name of the file, no path
     mv "$n" "$d/.$b";                 ## rename it as a hidden file
     sleep 1;                          ## wait one second
   done;
done

If you save this as ~/renamedot.sh and make it executable (chmod a+x ~/renamedot.sh) You can then create a crontab (run cron -e) like this:

@reboot /Users/your_user/renamedot.sh &

Now, this script will be run as a daemon and automatically rename any files matching your pattern to hidden dot files.

WARNING: This is not a good idea. The programs that generate these files and might depend on them will no longer find them since they have been renamed. For example, emacs's M-X recover-this-file will no longer work because its backup will not be found. You could always point it to the renamed file manually of course, but it might not be so easy in other cases. So, use at your own risk.

terdon
  • 53,403
  • 1
    I agree that this would not be a good idea at all. It could screw a lot of things up as it actually renames the files. At any rate, doing this even on the home folder would take several minutes at least (but obviously I haven't tried it). – Sean Allred Apr 21 '13 at 15:02
0

Not 100% automatic, but maybe you can use Pathfinder 6 http://cocoatech.com/pathfinder/

You can get file information with CMD + I and tick the checkbox Invisible attribute.

The file will disappear in Pathfinder, Finder, but still present and untouched (confirmed with ls). I don't know what exactly does Pathfinder behind the scene to make the file invisible but it works.

Michel
  • 199