Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ruqui

macrumors newbie
Original poster
Jan 22, 2013
29
0
I have a question, how can I disable the option to see hidden files when doing a 'ls -l' in a terminal window?
 

GGJstudios

macrumors Westmere
May 16, 2008
44,545
943
http://guides.macrumors.com/ls

To list the names only of all visible items in the current directory (see cd for information on setting the current directory)
ls​
To list detailed information about all items (visible and invisible) in the current directory (the -a option is to show all files, the -l option is to show details)
ls -al​
 

ruqui

macrumors newbie
Original poster
Jan 22, 2013
29
0

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/
 

Stooby Mcdoobie

macrumors 6502a
Jun 26, 2012
834
45
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/

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.
 
Last edited:

ruqui

macrumors newbie
Original poster
Jan 22, 2013
29
0
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.

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

switon

macrumors 6502a
Sep 10, 2012
636
1
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
 

ruqui

macrumors newbie
Original poster
Jan 22, 2013
29
0
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

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.
 

switon

macrumors 6502a
Sep 10, 2012
636
1
RE: Apple and ...

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.

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
 
Last edited:

ruqui

macrumors newbie
Original poster
Jan 22, 2013
29
0
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.

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.

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

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!
 
Last edited:

bradl

macrumors 603
Jun 16, 2008
5,923
17,399
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.
 

ruqui

macrumors newbie
Original poster
Jan 22, 2013
29
0
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.

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

switon

macrumors 6502a
Sep 10, 2012
636
1
RE: zsh...

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!

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
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
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 :) )

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.


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.

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
 

ruqui

macrumors newbie
Original poster
Jan 22, 2013
29
0
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.

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?

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

I'm not sure about this, isn't this equivalent to configure sudo to run without asking for password?
 

dove

macrumors member
Dec 25, 2009
32
0
(Update: Okay, nevermind what I previously posted.)
 
Last edited:

ruqui

macrumors newbie
Original poster
Jan 22, 2013
29
0
It's a bit weird that you log in as root. I didn't know it was possible on OS X but even Unix administrators don't normally do this.

Have you considered using sudo instead? When you need to do a lot of things as root you can use 'sudo -i'.

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.
 

dove

macrumors member
Dec 25, 2009
32
0
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.

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.
 

ruqui

macrumors newbie
Original poster
Jan 22, 2013
29
0
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.

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).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.