25

I am doing an applescript that is supposed to set the size of an folder to an varible. This is the code so far:

set sizeVar to do shell script "du -skh -m /Users/JS_Admin/Desktop"

Output:

"4242   /Users/JS_Admin/Desktop"

The thing is that I only want the size in numbers, no space or directory location.

How do I do that?

slhck
  • 235,242
DevRandom
  • 415

2 Answers2

47

Specifying both -k and -m does not make sense: either you want 1-Mbyte or 1-Kbyte blocks. Also -h does not make sense in combination with -k and -m. Only the last one -m will be considered

You can use cut to remove anything after the space:

du -sm /Users/JS_Admin/Desktop | cut -f1

With -f you specify which field you need (in this case the first one).

Matteo
  • 8,097
  • 3
  • 47
  • 58
1

An approach using awk.

du -s /path/to/folder | awk '{ print $1 }'

$1 refers to the first column. And regarding du -s:

-s, --summarize
     display only a total for each argument
joe
  • 101
  • 2