Ubuntu – I don’t want the ls command in the script to print results on screen

bashcommand lineprintingscripts

I run "ls -lX" over files umbrella31_*.xvg. I run a command that searches the number on the fifth column of the ls command output, that is larger let's say than 20000. It looks like this:

ls -lX umbrella31_*log |  awk '{if($5 >=20000) {print}}' | wc -l

and outputs a number (the number of lines for which the number in column 5 is > 20000).

When I include the above command in a script:

#!/bin/bash -x


ls -lX umbrella31_*log |  awk '{if($5 >=20000) {print}}' | wc -l

and run it, I get in the screen the result of "ls" printed as well (which I don't want). How can I get my script behave like my on-screen command, and print only the desired number of lines?

Best Answer

Your script will print each of the commands in your pipeline to the terminal because you are running it with the -x flag. Fromman bash:

   -x        Print commands and their arguments as they are executed.

However, your approach using ls and wc is not the best way to count files.


To find file that is >= 20000 you can use find:

find -type f -maxdepth 1 -name 'umbrella31_*log' -size +19999c -ls

(because of how find interpreters + sign (greater than rounded up) you get n+1, therefore the odd
-size n)

count output:
(when we count files we just print a newline because we dont really need an output)

wc -l < <(find -maxdepth 1 -type f -name 'umbrella31_*log' -size +19999c -printf \\n)

-maxdepth n
  Descend at most levels (a non-negative integer) levels of directories below the starting-points.
-size n
  File uses n units of space, rounding up.
-ls
  List current file in ls -dils format on standard output.