I want to capture to a file the output of the ls command

ls >> lsOutput.log

This one works if executed in the command line. But when put inside a shell script (lsOutput.sh), returns

./lsOutput.sh: 3: ./lsOutput.sh: total: not found

lsOutput.sh code


`ls -lrt` >> lsOutput.log

closed as off-topic by Gilles 'SO- stop being evil', slm, Anthon, jasonwryan, vonbrand Mar 1 '14 at 21:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Gilles 'SO- stop being evil', slm, Anthon, jasonwryan, vonbrand
If this question can be reworded to fit the rules in the help center, please edit the question.

  • The backticks around the command in the script make it mean something very different than the plain command run from the command line. – vonbrand Mar 1 '14 at 21:36

Just remove the backticks from your script:


ls -lrt >> lsOutput.log

Otherwise, the command is executed, and then its output is substituted and executed.

For example:

echo date

will output: date, while

`echo date`

will output current date, i.e. it will first evaluate to date, and then executed, which is calling the program date itself.

Not the answer you're looking for? Browse other questions tagged or ask your own question.