Need to check file space requirements prior to operation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to check file space requirements prior to operation
# 1  
Old 01-29-2011
Need to check file space requirements prior to operation

I'm all done with a script that relocates some files from different directories to another, just one caveat; I need it to check if i have enough space to complete the operation. On my commandline i normally use
Code:
df -h /dir/im/checking | awk '{print $4}' | grep -v Avail

I'd like to have my script check how much space I have left and if its adequate, then continue. The script uses 'cp' commands. I'd like to a: possibly estimate necessary requirements (i guess i could use the same for loop to case through the files and return their size instead of just copying but i need some help in doing so) and b: if at all possible, show progress once i have started copying. Wget has a purty nurfty progress bar "====> ". Any way of doing this? Thanks guys
# 2  
Old 01-29-2011
I wrote a script to show the "===>" and copy specified files to somewhere which has enough space,maybe helpful to you.I tested the script and it run okay.Hope it works correctly to your problem.
Code:
#! /bin/bash
#cd to the directory which you want to be copy
#DISTDIR is the distination directory you want the files copied to.
#usage: ./sh DISTDIR

declare -i totalspace=0
declare -i freespace

FILELIST=`ls -a $PWD`
DIST=$1

showbar()
{
	total_bar=80 #set to COLUMNS
	dist=$2
	declare -i total=$1
	declare -i already_copy=`du -bsc $dist | awk '{print $1}' | tail -1`
	declare -i bar=1
	declare -i div=already_copy/total  
	declare -i t
	
	echo -n "=>"
	while ((already_copy+4096 < total));do
		t=already_copy*total_bar/total
		div=already_copy/total
		if [ $t -le $bar ];then
			sleep 0.1
			already_copy=`du -bsc $dist | awk '{print $1}' | tail -1`
			continue
		fi
		while ((t>bar));do
			echo -ne "\b=>"
			((bar++))
		done
		already_copy=`du -bsc $dist | awk '{print $1}' | tail -1`
	done
}

for file in $FILELIST;do
	[ "$file" = "." -o "$file" = ".." ] && continue
	[ -f $file ] && totalspace+=`ls -l $file | awk '{print $5}'`
	[ -d $file ] && totalspace+=`du -bsc $file | tail -1 | awk '{print $1}'`
done

freespace=`df -k $DIST | awk '{print $4"000"}'| tail -1`
echo "freespace=$freespace"
[ $totalspace -lt $freespace ] || exit 1


for file in $FILELIST;do
        [ "$file" = "." -o "$file" = ".." ] && continue
	cp -R "$file" "$DIST/$file" &> /dev/null
done &

showbar $totalspace $DIST


Last edited by homeboy; 01-29-2011 at 10:18 AM..
# 3  
Old 01-31-2011
Is this portable to a sh only shell? no bash no korn just sh. Spits out errors at me saying
Code:
: command not found 4:
")syntax error: invalid arithmetic operator (error token is "
': not a valid identifierclare: `freespace
: command not found 7:
: command not found 10:
'/copyProgBar: line 11: syntax error near unexpected token `
'/copyProgBar: line 11: `showbar()

# 4  
Old 01-31-2011
Quote:
Originally Posted by DC Slick
Is this portable to a sh only shell? no bash no korn just sh. Spits out errors at me saying
I don't know about shSmilie.and the error message is self-explain,i think it's not a hard task to make it portable.maybe the declaration of variable between bash and sh is different.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

File validation prior to continuing script

Hi Guys, I am trying to find a way within a bash script to check a file that exists in the same directory to ensure every line starts with 44 and is 12 digits long. If it doesn't then print some sort of text advising of the error and stop the script from going any further. If all lines... (1 Reply)
Discussion started by: mutley2202
1 Replies

2. Shell Programming and Scripting

How to check if the file is empty or has blank space.?

Hi, I am using KSH. I am trying to check if the output file is empty or not. I tried with ] but what i see is my file is empty but still manages to have a size of 1 instead of 0. But my file doesnot have anything its empty. I am not sure how to check this. can any one help? (10 Replies)
Discussion started by: Sharma331
10 Replies

3. Shell Programming and Scripting

How to suppress the file name when we check for disk space

How to supress the file name when we check for disk space ? I used this command : du -ks /home/dir1/dir2/file.csv it returns 13 /home/dir1/dir2/file.csv Please explain the options too (7 Replies)
Discussion started by: arukuku
7 Replies

4. Shell Programming and Scripting

How to check the result of an operation?

Hi, This is probably very basic but I couldn't find an answer looking around. In a Linux script how to get the result of an operation? I want to create a folder if it does not exist. That I found I can do: test -e folder_name || mkdir folder_nameNow if the folder was created how can I get a... (1 Reply)
Discussion started by: gunsnrails
1 Replies

5. Shell Programming and Scripting

Removing all lines prior to the last pattern in a file/stream

Hi all, I didn't find anything that specifically answers this after searching for a bit, so please forgive me if this has been covered before. I'm looking to delete all lines prior to the last occurrence of a string in a file or stream from within a shell script (bash.) A bit of... (4 Replies)
Discussion started by: LivinFree
4 Replies

6. Shell Programming and Scripting

Column operation : cosne and sine operation

I have a txt file with several columns and i want to peform an operation on two columns and output it to a new txt file . file.txt 900.00000 1 1 1 500.00000 500.00000 100000.000 4 4 1.45257346E-07 899.10834 ... (4 Replies)
Discussion started by: shashi792
4 Replies

7. Shell Programming and Scripting

script to retrieve file for prior day

Is there a way to Create file name for the prior Sunday of the run_date if my Run_date =05262009, i need to the file with the File name= filename.Y20090524.zip Thanks in advance (1 Reply)
Discussion started by: aronmelon
1 Replies

8. UNIX for Advanced & Expert Users

File disk utilization for 10 days prior

Hi I have a requirement to list the files & the total disk utilization they have which are 10 prior to current date. I tried couple of options in combinations of find mtime, ctime with du -m, but no luck. Could you please help me in this ? (2 Replies)
Discussion started by: videsh77
2 Replies

9. Shell Programming and Scripting

Delete lines prior to a specific date in a log file.

Hi all. I have a database log file in which log data get appended to it daily. I want to do a automatic maintainence of this log by going through the log and deleting lines belonging to a certain date. How should i do it? Please help. Thanks. Example. To delete all lines prior to Jun... (4 Replies)
Discussion started by: ahSher
4 Replies

10. Shell Programming and Scripting

Help with file system requirements

Hi, I need to give the file system requirements for our project and i don't know how to proceed. From where do i stand? Our box is SUNos and there are 20 to 30 application that we should support on the box. Can any one get me started in a direction. Thanks (6 Replies)
Discussion started by: dsravan
6 Replies
Login or Register to Ask a Question