LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-22-2008, 05:48 PM   #1
twistadias
LQ Newbie
 
Registered: Aug 2008
Posts: 5

Rep: Reputation: 0
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.
 
Old 08-22-2008, 06:51 PM   #2
TheMadIndian
Member
 
Registered: Dec 2007
Distribution: Fedora Slackware CentOS slax RHEL
Posts: 117

Rep: Reputation: 23
Quote:
Originally Posted by twistadias View Post
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}'
 
Old 08-22-2008, 07:05 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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

Last edited by Tinkster; 08-22-2008 at 07:08 PM. Reason: added quote
 
Old 08-22-2008, 07:08 PM   #4
twistadias
LQ Newbie
 
Registered: Aug 2008
Posts: 5

Original Poster
Rep: Reputation: 0
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

Last edited by twistadias; 08-22-2008 at 07:11 PM.
 
Old 08-22-2008, 07:10 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by twistadias View Post
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
 
Old 08-22-2008, 07:35 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by twistadias View Post
@ 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.
 
Old 08-23-2008, 04:56 AM   #7
Samotnik
Member
 
Registered: Jun 2006
Location: Belarus
Distribution: Debian GNU/Linux testing/unstable
Posts: 471

Rep: Reputation: 40
There is a sort utitlity in *nix. Read man sort.
 
Old 08-23-2008, 09:18 AM   #8
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,093

Rep: Reputation: 197Reputation: 197
Quote:
Originally Posted by twistadias View Post
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.
 
Old 08-25-2008, 12:22 AM   #9
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,300

Rep: Reputation: 102Reputation: 102
Cool

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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting a file at a certain offset in bash matt007 Linux - Software 4 06-24-2008 04:03 PM
Bash: sorting by two fields humbletech99 Programming 7 09-25-2007 03:30 PM
Bash - How can you list 2 arrays in columns zak111 Programming 12 07-23-2007 11:27 AM
bash thinks term is 80 columns? JurgyMan Linux - General 4 06-13-2006 09:14 AM
Sorting files in BASH deleted/ Linux - Newbie 16 01-26-2006 06:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration