Solved

Doubt with Unix Command

Posted on 2011-09-19
7
308 Views
Last Modified: 2012-05-12
Hi Experts....
I have a unix command :-df -k /tmp | tail -1 | nawk -F' ' '{print $NF," ", $4}' | sed 's/.$//'
Can anyone explain this bit by bit.
Please help...
0
Comment
Question by:gaugeta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 21

Accepted Solution

by:
Papertrip earned 500 total points
ID: 36560463
df -k /tmp -- get file system usage in kilobytes for /tmp
tail -1 -- show last line of output only
nawk -F' ' '{print $NF," ", $4}'  -- using a space as a separator, print last column in line followed by a space followed by 4th column
sed 's/.$//' -- delete last character of line
0
 
LVL 18

Expert Comment

by:liddler
ID: 36560514
df -k /tmp shows partition usage in kilobytes for /tmp
tail -1 prints just the last line of the command above
nawk -F' ' '{print $NF," ", $4}' prints the last field then the 4th field
sed 's/.$//' converts from DOS format to Unix format i.e. from CR/LF to LF (Carriage Return / Life Feed)

It will give the mount point and the amount of 1k blocks available
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 36560528
df - displays free space on filesystem that has /tmp -k means show result in Kb

Sample:
vmplanet@ubuntu-vm:~$ df -k /tmp
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1             15775132   4076812  10896992  28% /

tail -1 will show you the last line of the output above eg:

/dev/sda1             15775132   4076812  10896992  28% /

(n)awk will use a ' for a field separator and will print the number of fields on each line (1 in this case) then a space " ", then the 4th field (10896992) - FS is not needed in my example as the default field separator (space) will work just fine

sed will search .$ and replace this with nothting (don't know why this is done here).

If you want to see what the command is doing for you, start with the first command only, then add one command at a time, like this:

df -k /tmp
df -k /tmp | tail -1
df -k /tmp | tail -1 | nawk -F' ' '{print $NF," ", $4}'
df -k /tmp | tail -1 | nawk -F' ' '{print $NF," ", $4}' | sed 's/.$//'

Compare the output of each next line with the previous.
0
 
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 36564568
you dont have to use tail and sed commnad.

$ df -k /tmp | nawk '{a=$NF;b=$4;c=$5;}END{print a, b, c}'                                                                                         
/tmp 1538016 56%

Open in new window


If you dont want percentage, then use this.

$ df -k /tmp | nawk '{a=$NF;b=$4;c=$5;}END{print a, b, substr(c,1,length(c)-1)}'                                                                   
/tmp 1538016 56

Open in new window

0
 
LVL 18

Expert Comment

by:liddler
ID: 36926298
I agree - Papertrip got there first
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question