Solved

Doubt with Unix Command

Posted on 2011-09-19
7
297 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I agree - Papertrip got there first
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

17 Experts available now in Live!

Get 1:1 Help Now