Ubuntu – What does ls -al .* command return

command linels

Does it show hidden file names inside all directories? Or just the current directory (including any directory inside it)? I cannot understand why ls -ld .* returns different results (it shows hidden files and directories ONLY in current directory, not showing files inside any other directory inside it)? Also What does -d do alone (ls -d)?

Best Answer

ls -al .* shows the contents of all directories in the current directory whose names begin with . (ie all those matching the shell glob .*)

In Bash, the .* glob resolves to . and .. as well. Since . represents the current directory, and .. the parent directory, the contents of both of these directories, including any hidden files and directories, are shown too.1

.* also catches hidden files in the current directory, but they are listed anyway as contents of .

As explained in What does `ls --directory` stand for? the -d flag causes the directories themselves to be displayed, instead of their contents.


1this is also the case in dash, which is the shell symlinked to sh in Ubuntu. However, other shells, including zsh and mksh, behave more intuitively and don't include . and .. when expanding .*. It is also worth noting that, while most commands will operate on .. and . when they are included in a .* glob (including chmod and chown - you can really mess up your system with those), the rm command will helpfully fail by design to do so.