cmd alias for printing out sizeof folder cygwin

126 Views Asked by At

I currently try to figure out How exactly i can generate a Doskey Alias that allows Pipes.

I want to gather the foldersize in kB.

I have tried

alias dirsize=du -P -c -a -b $1 | grep total | awk '{print "Folder has: " $1 "kB"}'

But i simply get the Output

Folder has: kB

When I just use

dirsize=du -P -c -a -b $1 | grep total

It gets me

C:\>dirsize Temp
1364201 total

But how do I use the awk pipe now?

What am I doing wrong?

1

There are 1 best solutions below

0
On BEST ANSWER
doskey dirsize=du -P -c -a -b $1 $b grep -E "total$" $b awk "{i=1;print \"Has \" $i \" bytes\"}"

Inside a doskey alias

  • the pipe character is $b

  • all instances of $1 will be replaced with the first argument in the call to the alias, so, the $1 inside the awk command will be replaced during the alias parse and at execution time it will not be a reference to the argument

The usual solution to this problem inside a doskey alias is to convert the $1 into something that will not interfere the command execution but avoids doskey parser to handle it. To do it, the usual solution is to use $^1, but in this case, this sequence is not handled by cmd, it is handled by awk and the ^ is not considered a escape character.

We need to solve it inside awk, replacing the direct $1 using a variable (i) to store the index to retrieve and replacing the $1 with $i that has no special meaning inside doskey aliases.