Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

up vote 3 down vote accepted
+50

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

share|improve this answer

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.

share|improve this answer
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
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

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.

share|improve this answer
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

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.

share|improve this answer

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.