df -h if freespace equals then bash

0 votes

The error that I am getting is with the df command on mac, I would like to specify a value in gigabytes and perhaps allow the user to choose a different drive if / is full.

destination="$HOME/Desktop/sandbox"

if [ $(df -h --output=avail /|tail -n1) -lt 300000 ]; then

    echo "There is less than 300GB available..." ;
    exit

else

    for files in *.tar ; do echo copying "$files" ; cp "$files" "$destination" ; read -n 1 -p "Press any key..." ; done
fi

Not sure if df -h / | tail -1 | awk '{print $4}' | sed 's/..$//' is a good option

Jun 13, 2022 in Linux Administration by Rahul
• 3,380 points
581 views

1 answer to this question.

0 votes

Try this out:

destination="$HOME/Desktop/sandbox"

freespace="$(df -h / | tail -1 | awk '{print $4}' | sed 's/..$//')"

if  [ "$freespace" -lt 300 ]; then

    echo "There is less than 300GB available..." ;
    exit

else

    for files in *.sh ; do echo copying "$files" ; cp "$files" "$destination" ; read -n 1 -p "Press any key..." ; done
fi

answered Jun 13, 2022 by Korak
• 5,820 points

Related Questions In Linux Administration

0 votes
1 answer

Comparing two string variables in an 'if' statement in Bash

1. For string comparison, use: if [ "$s1" ...READ MORE

answered May 15, 2019 in Linux Administration by Shubham
• 13,490 points
1,713 views
0 votes
0 answers

if, elif, else statement issues in Bash

I can't seem to work out what ...READ MORE

Jun 22, 2022 in Linux Administration by Korak
• 5,820 points
500 views
+1 vote
3 answers
0 votes
1 answer

How to take input from user in bash script?

You can use if-else branch to check ...READ MORE

answered Jan 31, 2019 in Linux Administration by Omkar
• 69,220 points
1,117 views
0 votes
1 answer

setting a variable output from a bash command

You can use $(command), which in my ...READ MORE

answered Feb 15, 2019 in Linux Administration by DareDev
• 6,890 points
2,474 views
0 votes
2 answers

how to loop through the content of a file using bash

#!/bin/bash for i in  `cat peptides.txt` do echo $i done READ MORE

answered Sep 5, 2020 in Linux Administration by Prakash K. Aithal
2,934 views
0 votes
1 answer

redirecting stdout and stderr to file using bash

cmd >>file.txt 2>&1 Bash always executes and redirects ...READ MORE

answered Mar 19, 2019 in Linux Administration by ajs3033
• 7,300 points
22,200 views
0 votes
1 answer

How to extract file base name without path and extension in bash?

You don't actually have to call the ...READ MORE

answered May 31, 2019 in Linux Administration by Shubham
• 13,490 points
5,884 views
0 votes
1 answer

How to check if a string contains a substring in Bash?

You ought to recollect that shell prearranging ...READ MORE

answered May 27, 2022 in Linux Administration by Korak
• 5,820 points
1,162 views
0 votes
1 answer

How do I check if a directory exists in a Bash shell script?

To check if a directory exists in ...READ MORE

answered Jun 7, 2022 in Linux Administration by Korak
• 5,820 points
1,784 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP