0

I'm using this code:

  numbzip=`ls *.plt.zip | wc -l` &>/dev/null

and trying to get rid of the output in the command window.

No files ending on .plt.zip exist so it comes back with:

ls: cannot access *.plt.zip: No such file or directory

whatever I try it always writes this line in the command window.

I tried:

numbzip=`ls *.plt.zip | wc -l` >/dev/null 2>/dev/null
numbzip=`ls *.plt.zip | wc -l` >/dev/null >>/dev/null 2>/dev/null

Regards, Wilco.

1
  • If one of the answers you receive solves your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites.
    – terdon
    Commented Jan 20, 2016 at 16:41

2 Answers 2

0

You shouldn't parse the output of ls. Instead use find

numbzip=$(find -iname "*.zip" | wc -l)

echo $numzip
1
  • Using find in this way has all of the problems that parsing ls does. The only reason you'd want to use find is because of its -print0 option which prints null-separated output and can deal correctly with file names containing newlines.
    – terdon
    Commented Jan 20, 2016 at 16:44
0

You keep seeing the message because you're redirecting the wrong thing:

numbzip=`ls *.plt.zip | wc -l` &>/dev/null

That redirects the stderr of the variable assignment, not of the ls command. You're running the ls inside the `` and redirecting outside it. To redirect the error output of ls, use:

numbzip=`ls *.plt.zip 2>/dev/null | wc -l` 

That said, while the above will wrk for simple file names, it will fail if your filenames contain newlines. As a general rule, you should avoid parsing the output of ls. You can use shell globbing instead:

zipfiles=( *.plt.zip )
echo ${#zipfiles[@]}

The first command will create the zipfiles array whose contents are all the files/directories matching the glob *.plt.zip. The second line print the number of elements in the array.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .