0

What does ls -A do? I have found this shell script and I can't figure out what ls -A does. In the manual it writes that ls -A == do not list implied . and .. but I don't understand what it means.

dir=./$1
if [[ -z $(ls -A $dir) ]]

1 Answers1

5

Normally ls doesn't print filenames that start with a dot. With -a, it does, but that includes . and .., which exist in all directories, and hence aren't very interesting. With -A it prints everything but those two.

$ touch normal .hidden
$ ls
normal
$ ls -a
./  ../  .hidden  normal
$ ls -A
.hidden  normal

In that if-statement, the command substitution $( .. ) captures the output of ls, and [[ -z ... ]] tests if it's the empty string. That is, that there's no files in the directory.

Usually, reading the output of ls is not a good idea, if you want to loop over files in the shell, you can just use *. Here, it should work, though, except for the fact that if the given directory name contains whitespace (or filename glob characters), they'll be expanded on the command line of ls, which may mess up the results.

See:

ilkkachu
  • 138,973