Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

if there a way to get drives space details without drive names. E.g., the output of

df -h

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              20G   15G  4.2G  78% /
/dev/sda6              68G   39G   26G  61% /u01
/dev/sda2              30G  5.8G   22G  21% /opt
/dev/sda1              99M   19M   76M  20% /boot
tmpfs                  48G  8.2G   39G  18% /dev/shm
/dev/mapper/vg3-KPGBKUP4
                   10T  7.6T  2.5T  76% /KPGBKUP4

I want the output as below:

20G   15G  4.2G  78% 
68G   39G   26G  61% 
30G  5.8G   22G  21% 
99M   19M   76M  20% 
48G  8.2G   39G  18% 
10T  7.6T  2.5T  76%

Why I want this? I have several servers and have to make report everyday. This will reduce my work to one tenth. Any suggestion is highly appreciated.

share|improve this question

5 Answers 5

up vote 2 down vote accepted

Assuming filesystems and mount points don't contain blank characters, try:

df -hP | awk 'NR>1 { $1=$6="" ; print }' | column -t

df -hP lists the filesystem statistics without linebreaks for long filesystem names.

awk 'NR>1 { ... }' restricts the given action to 2nd and following lines to skip df's header line...

The awk-action { $1=$6="" ; print } zeroes the unwanted fields and prints the rest.

Using { print $2,$3,$4,$5 } would do the same job.

column -t arranges the whole output as clean table.


$ cat x
#!/bin/sh
echo '<testing inside a script>'
df -hP | awk 'NR>1 { $1=$6="" ; print }' | column -t
echo '</testing inside a script>'
$ chmod +x x
$ ./x
<testing inside a script>
71G    39G   29G    58%
1006M  0     1006M  0%
10M    208K  9,8M   3%
1006M  0     1006M  0%
</testing inside a script>
$ _

(I'll remove this again when the problem mentioned in the comments has been solved...)

share|improve this answer
2  
You can (and should) use df -P to suppress line-breaks. –  n.st Mar 31 at 12:16
    
Yes... I remember to have known this when my brain was young... cough! cough! :-D Thanks^n! –  yeti Mar 31 at 12:49
    
Your concern was right, last line is not as I wanted. My last line is creating problem. it is giving me "7.8T 2.3T 78% /CGFBKUP1" instead of "10T 7.8T 2.3T 78%". That is my main problem, otherwise I could have used "df -h | awk {'print $2, $3, $4, $5'}" –  Abhishek dot py Mar 31 at 12:52
    
Excellent. My question was quite out of the box, but you have improvised well. But I am not very good with awk, can you explain in short what you have done here? –  Abhishek dot py Mar 31 at 12:57
    
While your command is working perfectly on command line, it is not working on script. It is declaring error near ';'. –  Abhishek dot py Apr 1 at 8:22

As of GNU coreutils 8.21 (changelog), df has a --output option. Using sed to trim the header:

df -h --output=size,used,avail,pcent | sed 1d
share|improve this answer
    
At least the df from coreutils 8.12.197-032bb does not have that option, so it must be pretty new. (That's the version Debian Wheezy currently ships.) Looks useful, though. –  Michael Kjörling Mar 31 at 14:49
    
@MichaelKjörling, yes never realised this. Since version 8.21, I have added this to my answer. –  Graeme Mar 31 at 14:58
findmnt -Do SIZE,USED,AVAIL,USE%

Here's my output:

 SIZE   USED AVAIL USE%
11.8G      0 11.8G   0%
11.8G  63.1M 11.7G   1%
11.8G   920K 11.8G   0%
11.8G      0 11.8G   0%
  12G   8.9G  2.7G  74%
11.8G 410.6M 11.4G   3%
   3G 584.4M  2.4G  19%
   3G 584.4M  2.4G  19%
 2.4G     4K  2.4G   0%
   0      0     0    -

So if you're not already using findmnt for your report, you probably should. You can get a lot more specific than that - but in the above case I specify -Do to imitate df (as the option is described) and to limit the output columns. To remove the column headings, just add -n. You don't need any string parsing; it's atomic to the output.

But don't take my word for it:

man mount

... For more robust and definable output use findmnt(8), especially in your scripts...

share|improve this answer
    
I can found findmnt on my system. Is it needs to be installed separately. –  Abhishek dot py Mar 31 at 12:54
    
It shouldnt - what kernel version do you have? –  mikeserv Mar 31 at 13:02
1  
@Abhishekdotpy On Debian at least, it's provided by the mount package which should definitely be installed. –  Michael Kjörling Mar 31 at 14:50
    
@Abhishekdotpy findmnt is part of util-linux (as is mount). Note that this is the source package for the mount package in Debian. Other distros might break things down differently (or just use util-linux for everything). –  Graeme Mar 31 at 15:06
    
Its util-linux here, but these tools - the new lsblk, blockdev, findmnt - all rolled in around 3.6 or so i think. Still theyve been around long enough for mount's manpage to tell us to use them - instead of it, no less - so maybe you need to update? –  mikeserv Mar 31 at 15:11

Try This command

df -Ph | awk '{print $2, $3, $4, $5}'
share|improve this answer
1  
You can (and should) use df -P to suppress line-breaks. –  n.st Mar 31 at 12:17
    
Thanks for your valuable information @n.st –  tejas Apr 1 at 5:19

Try this:

$ df -h | awk 'NR==1{next}{for(i=2;i<NF;i++){printf("%s\t",$i)};print""}'
92G     5.2G    82G     6%  
2.9G    4.0K    2.9G    1%  
1.2G    932K    1.2G    1%  
5.0M    0       5.0M    0%  
2.9G    700K    2.9G    1%  
360G    53G     289G    16% 
share|improve this answer
    
I appreciate your answer, but my last line is creating problem. it is giving me "7.8T 2.3T 78% /CGFBKUP1" instead of "10T 7.8T 2.3T 78%". That is my main problem, otherwise I could have used "df -h | awk {'print $2, $3, $4, $5'}" –  Abhishek dot py Mar 31 at 12:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.