Search code examples
shellunixpipels

What does "ls -t . | while read line" mean?


Here is the code:

if test $# -eq 1
then
  if test $1 = "--exec"
  then
    ls -t . | while read line
    do
      if test -f $line -a -x $line
      then
        echo $line
      fi
    done
  fi
fi

I don't understand the utility of . here in ls -t . | while read line; can you explain?


Solution

  • The line lists all the files in the present directory in ascending order of their modification time. The pipe operator sends the result of the directory listing to the while loop, which reads in each line from the ls command into the variable "line". When I do this kind of thing, I usually use a foreach loop, but either way works.