Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16
Hello I have on equestion that i want to check the disk space of mounted drive df -h /path to drive and then i want to write a script which ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2014
    Posts
    24

    IF statemnet with DF -h command


    Hello
    I have on equestion that i want to check the disk space of mounted drive

    df -h /path to drive and then i want to write a script which will genrate a messsage if drive space will be above 80% .

    means
    script how i can make if condition which for drive space .

    if (df -h /path to drive <= 80 %); then
    echo "it has only 20 % left"
    else
    null
    fi
    can some one help me with this ?


    thanks in advanced

  2. #2
    Linux Engineer
    Join Date
    Dec 2013
    Posts
    1,456
    Here's the basic structure:

    Code:
    a=$( df -h --output='pcent' . | tail -n +2  | tr '%' ' ' | tr -s '') 
    
    if [[ $a -gt 80 ]]; then 
        echo $a; 
    fi

  3. #3
    Just Joined!
    Join Date
    May 2014
    Posts
    24
    Quote Originally Posted by gregm View Post
    Here's the basic structure:

    Code:
    a=$( df -h (path to directory) --output='pcent' . | tail -n +2  | tr '%' ' ' | tr -s '') 
    
    if [[ $a -gt 80 ]]; then 
        echo $a; 
    fi
    i am just little bit confussed that how can i calculate for specific mounted directory.Will paht of directory will come after df -h command.?

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    May 2014
    Posts
    24
    diskusage= df -h | grep /path to directory | cut -c 41-43

    if [ $diskusage==12% ]; then
    echo "your drive is under 10 % "
    else
    echo "you have enough space"
    fi

    this i am trying but i am little bit confussed. how i can calculate column for particual mounted drive
    Last edited by kunalks; 05-31-2014 at 04:07 PM.

  6. #5
    Linux Engineer
    Join Date
    Dec 2013
    Posts
    1,456
    In the example I gave the . represents the current directory - replace it with the path you'd like to use. i.e.
    Code:
    a=$( df -h --output='pcent' /the/path/you/want | tail -n +2  | tr '%' ' ' | tr -s '')

  7. #6
    Just Joined!
    Join Date
    May 2014
    Posts
    24
    Quote Originally Posted by gregm View Post
    In the example I gave the . represents the current directory - replace it with the path you'd like to use. i.e.
    Code:
    a=$( df -h --output='pcent' /the/path/you/want | tail -n +2  | tr '%' ' ' | tr -s '')
    df: unrecognized option '--output=pcent'
    Try `df --help' for more information.
    i got this error

    thanks a lot man

  8. #7
    Linux Engineer
    Join Date
    Dec 2013
    Posts
    1,456
    You may have an older version of df (check 'man df' to see). You should be able to run:
    Code:
    df -h /dev/sda1 | tail -n +2 | tr -s ' ' | tr '%' ' ' | cut -d ' ' -f 5
    Replace /dev/sda1 with your hard drive if neccessary.

  9. #8
    Just Joined!
    Join Date
    May 2014
    Posts
    24
    Quote Originally Posted by gregm View Post
    You may have an older version of df (check 'man df' to see). You should be able to run:
    Code:
    df -h /dev/sda1 | tail -n +2 | tr -s ' ' | tr '%' ' ' | cut -d ' ' -f 5
    Replace /dev/sda1 with your hard drive if neccessary.
    df -h | grep /mnt/smb | cut -c 55-60

    i tried this code also but your help was really appreciable thanks a lot.this is working fine now with you code

    thanks
    kunalks

  10. #9
    Just Joined!
    Join Date
    May 2014
    Posts
    24
    Quote Originally Posted by gregm View Post
    Here's the basic structure:

    Code:
    a=$( df -h --output='pcent' . | tail -n +2  | tr '%' ' ' | tr -s '') 
    
    if [[ $a -gt 80 ]]; then 
        echo $a; 
    fi
    a=$( df -h /mnt/smb | tail -n +2 | tr -s ' ' | tr '%' ' ' | cut -d ' ' -f 5)

    if [[ $a -gt 80 ]]; then
    cat /etc/diskspace/text.txt | sendmail test@gmail.com
    fi
    also i want to include drive space ($a) percentage into email.

  11. #10
    Just Joined!
    Join Date
    May 2014
    Posts
    24

    Answer=whole path of network drive except only number! :(

    Quote Originally Posted by gregm View Post
    You may have an older version of df (check 'man df' to see). You should be able to run:
    Code:
    df -h /dev/sda1 | tail -n +2 | tr -s ' ' | tr '%' ' ' | cut -d ' ' -f 5
    Replace /dev/sda1 with your hard drive if neccessary.
    a=$ ( df -h /dev/sda1 | tail -n +2 | tr -s ' ' | tr '%' ' ' | cut -d ' ' -f 5)
    echo $a

    because of some reason! this command has started to give me the whole path of directory and the drive space!

    I am only trying to get how many % drive has been occupied and because then i need to use this number in if else statement!

    but this code is only giving me the full string with path and occuiped drive spac!"!

    can someone please help ?

    thankms

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •