LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sorting columns in bash (https://www.linuxquestions.org/questions/linux-newbie-8/sorting-columns-in-bash-664705/)

twistadias 08-22-2008 05:48 PM

sorting columns in bash
 
I need to show the freedisk space in any directory, for example /tmp
Code:

df /tmp | tail -1
But i need to only show the remaining free disk space. can anyone help me to to remove the other columns shown.

TheMadIndian 08-22-2008 06:51 PM

Quote:

Originally Posted by twistadias (Post 3256482)
I need to show the freedisk space in any directory, for example /tmp
Code:

df /tmp | tail -1
But i need to only show the remaining free disk space. can anyone help me to to remove the other columns shown.

Code:

df /tmp | tail -1 |awk '{print $3}'

Tinkster 08-22-2008 07:05 PM

Quote:

I need to show the freedisk space in any directory, for example /tmp
Ummm ... df only operates on mount-points, not
directories. While you can invoke it like that it
doesn't make too much sense unless the directories
you're iterating over are indeed on separate file-
systems.

And you don't really need tail if you're using awk:
Code:

df /tmp | awk '$4 ~ /[0-9]/ {print $4}'


Cheers,
Tink

twistadias 08-22-2008 07:08 PM

Thanks for your fast reply. Is there any way I can show this in kb, Mb or GB

@ Tinkster. Although your code does work, i really dont understand the stuff in awk. I am using this for a project in uni wherein I have to explain each and every line. If you could make me understand what your code does, it would be great

Tinkster 08-22-2008 07:10 PM

Quote:

Originally Posted by twistadias (Post 3256547)
Thanks for your fast reply. Is there any way I can show this in kb, Mb or GB

Code:

df -k /tmp | awk '$4 ~ /[0-9]/ {print $4}'
man df
for details ....

And df still doesn't operate on a per directory basis...


Cheers,
Tink

Tinkster 08-22-2008 07:35 PM

Quote:

Originally Posted by twistadias (Post 3256547)
@ Tinkster. Although your code does work, i really dont understand the stuff in awk. I am using this for a project in uni wherein I have to explain each and every line. If you could make me understand what your code does, it would be great

awk '$4 ~ /[0-9]/ {print $4}'

Awk operates on lines and fields within lines, by default
the field separator is whitespace other than \n.

So if you see a line like
/dev/sda7 19518312 7021552 12496760 36% /
12496760 will be field $4.

What I've done is to only create output if the field
is numeric (regular expression) ~ /[0-9]/ which means
that it won't take for the df header.

In English: "Only produce output if the 4th field in any
line of input has a numeric value, and only output that
one field"


Cheers,
Tink

P.S.: If you want a response it's wiser to reply rather than
to edit pre-existing posts. People on the boards don't usually
scroll up to check for changes - that's not what a forum is about.

Samotnik 08-23-2008 04:56 AM

There is a sort utitlity in *nix. Read man sort.

arizonagroovejet 08-23-2008 09:18 AM

Quote:

Originally Posted by twistadias (Post 3256547)
Thanks for your fast reply. Is there any way I can show this in kb, Mb or GB

Code:

df -h
Read 'man df' to find out what that does.

'df -k' doesn't produce anything different to 'df' for me.

vikas027 08-25-2008 12:22 AM

Try these examples:-

Code:

# du -ks /var/adm
1886422 /var/adm


Code:

# du -ks /var/adm | awk '{printf "%8.2f %s\n",$1/1024,$2}'
 1842.21 /var/adm

Code:

# du -ks /var/adm | awk '{printf "%8.2f %s\n",$1/2048,$2}'
  921.10 /var/adm


Code:

# du -ks /var/adm | awk '{printf "%8.2f %s\n",$1/1024,$2}' | awk '{print $1}'
1842.21


# To be run in a directory to find dir. size sorted
Code:

for i in `ls -lrt | awk '{print $9}'`; do du -ks $i  | awk '{printf "%8.2f %s\n",$1/1024,$2}'; done | sort -kr1


All times are GMT -5. The time now is 09:41 PM.