Solved

Doubt with Unix Command

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

14 Experts available now in Live!

Get 1:1 Help Now