sorting columns in bash
I need to show the freedisk space in any directory, for example /tmp
Code:
df /tmp | tail -1 |
Quote:
Code:
df /tmp | tail -1 |awk '{print $3}' |
Quote:
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 |
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 |
Quote:
Code:
df -k /tmp | awk '$4 ~ /[0-9]/ {print $4}' for details .... And df still doesn't operate on a per directory basis... Cheers, Tink |
Quote:
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. |
There is a sort utitlity in *nix. Read man sort.
|
Quote:
Code:
df -h 'df -k' doesn't produce anything different to 'df' for me. |
Try these examples:-
Code:
# du -ks /var/adm Code:
# du -ks /var/adm | awk '{printf "%8.2f %s\n",$1/1024,$2}' Code:
# du -ks /var/adm | awk '{printf "%8.2f %s\n",$1/2048,$2}' Code:
# du -ks /var/adm | awk '{printf "%8.2f %s\n",$1/1024,$2}' | awk '{print $1}' # 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. |