Solved

Doubt with Unix Command

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

Expert Comment

by:Gerwin Jansen
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 …
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…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now