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
Solved

Doubt with Unix Command

Posted on 2011-09-19
7
304 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…

809 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