Hidden files in Terminal

Discussion in 'OS X' started by ruqui, Jan 31, 2013.

  1. macrumors newbie

    Joined:
    Jan 22, 2013
    #1
    I have a question, how can I disable the option to see hidden files when doing a 'ls -l' in a terminal window?
     
  2. macrumors Westmere

    GGJstudios

    Joined:
    May 16, 2008
    #2
    http://guides.macrumors.com/ls

     
  3. thread starter macrumors newbie

    Joined:
    Jan 22, 2013
    #3
    The problem is that by default 'ls -l' shows the hidden files:

    Code:
    [root->~] ls -lFG
    total 112
    -rw-r--r--  1 root  wheel      3 Dec 18 19:31 .CFUserTextEncoding
    drwxr-xr-x  7 root  wheel    238 Jan 19 00:01 .cpan/
    -r--r--r--  1 root  wheel     10 Jul 21  2012 .forward
    -rw-------  1 root  wheel     60 Jan 22 21:42 .lesshst
    -rw-------  1 root  wheel   1241 Jan 19 23:03 .sh_history
    drwx------  3 root  wheel    102 Jan 19 23:02 .ssh/
    -rw-------  1 root  wheel   5237 Jan 30 20:13 .viminfo
    drwx------  4 root  wheel    136 Dec 24 16:21 .vnc/
    -rw-r--r--  1 root  wheel  32127 Jan 19 22:47 .zcompdump
    -rw-------  1 root  wheel   1681 Jan 31 23:21 .zsh_history
    drwx------  8 root  wheel    272 Dec 24 16:21 Library/
    
     
  4. Stooby Mcdoobie, Jan 31, 2013
    Last edited: Jan 31, 2013

    macrumors 6502a

    Stooby Mcdoobie

    Joined:
    Jun 26, 2012
    #4
    When you're logged in as root, ls -l will show hidden files. This doesn't happen when you're logged in as a regular user/admin, and I'm not sure if there's a way to disable it, but that's why your findings contradict what GGJ posted.

    edit: To clarify, ls with any argument will show hidden files while logged in as root.
     
  5. thread starter macrumors newbie

    Joined:
    Jan 22, 2013
    #5
    Yes, I forgot to mention that the problem is only with user root :) sorry about that.

    Is it possible to disable this behaviour? (I only want to see hidden files when I need them, not every time).
     
  6. macrumors 6502a

    Joined:
    Sep 10, 2012
    #6
    RE: ls and super user...

    Hi ruqui,

    I believe the "-A" is always set for the superuser on the "ls" command. The only way around this is not to be superuser (or perhaps to change superuser's group to not be wheel - but this would be problematic and I'm not sure it would work). So, when logged in as root, you can "su" to a regular administrator's account, "su username". Then "ls" will not show the hidden files. Exiting the "su username" then gets you back to your root login.

    I guess you should ask if you need to be logged in as root in the first place? Perhaps "sudo" would accomplish what you need root for?

    Regards,
    Switon
     
  7. thread starter macrumors newbie

    Joined:
    Jan 22, 2013
    #7
    This behaviour is "awkward" to say the least :) If I'm using root it's supposed that I have total control of my environment so I can do whatever I like (at my own risk of course), I don't like this way of forcing me what to see when I type ls ... it's the only unix-like system I know that does that (and I've worked with several in the last 15 years :) )

    And regarding your question: yes, I do need to be logged-in as root (technically, I'm not logged in as root, I'm opening a root shell, but it's the same for this purpose) ... I don't like at all to be typing my password every time I need a command as root.
     
  8. switon, Feb 1, 2013
    Last edited: Feb 2, 2013

    macrumors 6502a

    Joined:
    Sep 10, 2012
    #8
    RE: Apple and ...

    Yeah, I hear you...I also hate how Apple has made certain "decisions" for the users's sake, and I'm particularly sad at the direction that the Mac OS and the OS Server software is taking: eliminating flexibility and options for the sake of simplicity only. I, too, may be forced back to using only Linux because of this.

    If I were you I would just alias the ls command to a separate bash command that does /bin/ls followed by grep or awk and cut out all of the dot files that you don't want to see. Thus "ls" under root would not show you these hidden files. Actually, I'd probably alias the new command to something like "lsm" so if I did a "lsm" I wouldn't see the hidden files since it runs the bash command instead while if I did a "ls" it would show the hidden files (or vice versa).

    Regards,
    Switon
     
  9. ruqui, Feb 2, 2013
    Last edited: Feb 2, 2013

    thread starter macrumors newbie

    Joined:
    Jan 22, 2013
    #9
    Yes, I agree with you. I've just bought a Macbook Pro because I became somewhat tired of fighting with Linux to recognize all the hardware, at first it was very nice to see that everything worked out of the box in the Mac, but as I start really using it I'm beginnig to notice the lack of flexibility here and there ... probably when Linux solves the issue with the newest UEFi BIOS I'll go back to use them as my primary desktop, probably my next notebook will be Linux again.

    That's a very good idea, I don't use bash (I'm a zsh fan :) ) but I think I can do this in zsh also. Thanks!
     
  10. macrumors 68030

    bradl

    Joined:
    Jun 16, 2008
    #10
    OP, check to see is the ls command is aliased. As root type: 'alias' (no quotes) and see if ls is listed. If it is, it is probably aliased to 'ls -a'. If it is, just type: unalias ls. And you'll be good.

    BL.
     
  11. thread starter macrumors newbie

    Joined:
    Jan 22, 2013
    #11
    Yes, that's the first thing I've checked but unfortunately that's not the case, and it would be very strange if it were, I'm using zsh as the default shell, and they don't share configuration files with bash; also, zsh is not installed by default in OS X.

    I've also tried to execute \ls -l (usually the backslash escapes any alias defined and execute the original command) but I got the same result.

    BTW, this behaviour happens independently of the shell used (at least is the same in both bash and zsh).
     
  12. macrumors 6502a

    Joined:
    Sep 10, 2012
    #12
    RE: zsh...

    Hi ruqui,

    I don't use zsh, but I do know that this alias of "ls" is possible not only in bash but also in csh and tcsh, so I suspect it must also be possible in zsh. It will take a different syntax most likely, however.

    Good luck,
    Switon
     
  13. macrumors 603

    Joined:
    Aug 9, 2009
    #13
    Get the Darwin source for the ls command, change it to do what you want, recompile, install. Total control.

    The feature is probably tied to the C code in ls calling getuid() or geteuid() and checking for 0 (root). Assuming it's that or similar, it wouldn't be hard to find.


    Copy the /usr/bin/env command to a private location, accessible only to your uid. Change its owner to root. Change its mode to setuid. Change its name to suenv (for example). Add the private dir to your PATH env-var.

    To run as root w/o entering a password:
    Code:
    suenv command goes here
    
    Where "command goes here" is the command to run. For example, try the 'id' command:
    Code:
    suenv id
    
     
  14. thread starter macrumors newbie

    Joined:
    Jan 22, 2013
    #14
    Man, that's awesome! I really didn't know the source code is available! It could be a little work but I think is worth it :)

    Obviously, I'm a little lost at the moment :) do you know where to begin? Where can I get the source code for ls?

    I'm not sure about this, isn't this equivalent to configure sudo to run without asking for password?
     
  15. dove, Feb 4, 2013
    Last edited: Feb 4, 2013

    macrumors member

    Joined:
    Dec 25, 2009
    #15
    (Update: Okay, nevermind what I previously posted.)
     
  16. thread starter macrumors newbie

    Joined:
    Jan 22, 2013
    #16
    Well, I must tell you that I am a unix administrator (I administer a couple of enterprise class Oracle Solaris mainframes, a bunch of RedHat blade servers and some other servers) and I can assure you that I do a lot of things as root :)

    I know how to use sudo, the thing is I'm interested in fixing other stuff. BTW, using 'sudo ls' still shows the hidden files, so using sudo is not help at all.
     
  17. macrumors member

    Joined:
    Dec 25, 2009
    #17
    Yeah. Actually, 'sudo -i' was stupid advice because that will log you in as the root user. The effect I was going would be the one you get by running 'sudo sh'. E.g. mostly keep your env but change your privileges.

    But as someone else pointed out, that doesn't work. In fact the ls manual has this to say:

    -A List all entries except for . and ... Always set for the super-
    user.​

    I do wonder if you get the same behavior with GNU ls (on Linux). The manual doesn't seem to say: http://linuxcommand.org/man_pages/ls1.html

    Update: if the behavior is different then maybe you could try installing GNU ls. It's in the package coreutils.
     
  18. thread starter macrumors newbie

    Joined:
    Jan 22, 2013
    #18
    GNU ls don't have that problem, so it's a very good idea to install coreutils. Is there a standard method to install this? (my idea is to install using brew, but I'm not sure if there are better methods to do this).
     
  19. macrumors member

    Joined:
    Dec 25, 2009
    #19
    brew sounds good. I also found this: http://hocuspokus.net/2008/01/a-better-ls-for-mac-os-x/
     

Share This Page