File size and dir size


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File size and dir size
# 1  
Old 02-14-2011
File size and dir size

How to use 'df' only to get the 'Available' space for a specific dir, and then compare with a specific file size using stat -c %s file.txt to see if the file actually can be copied into the dir. Is there any quick way to see if a file can fit into a dir?
# 2  
Old 02-14-2011
Code:
df /dir/you/want | awk '{print $4}' | grep -v Avail

or
Code:
df /dir/you/want | awk '{print $4}' | tail -1

Will return just the Available space for 'df'. You can then you comparisons using tests or if commands
Code:
 freespace=`df /dir/you/want | awk '{print $4}' | tail -1`
myfile=`stat -c %s /path/to/file.txt`
[ "$freespace" -gt "$myfile" ] && echo "The file will fit"


Last edited by DC Slick; 02-14-2011 at 06:10 PM.. Reason: haste makes waste
# 3  
Old 02-14-2011
DC_Slick, don't forget that df usually returns free size in 512 or 1K byte blocks.

Emilywu, does your df support the -k option to output sizes in 1024 byte blocks?

If so you might be able to do somthing like this:

Code:
freespace=$(( $(df -k /dir/you/want | awk '{S=$4} END{print S}') * 1024 ))


Last edited by Chubler_XL; 02-14-2011 at 07:08 PM.. Reason: Use awk features to remove need for tail command
# 4  
Old 02-15-2011
Thanks for all your answers. They worked fine.
Chubler XL, Yes, -k is ok for me. do you mean that we can't compare the file size using stat and the freespace suggested by DC since the block size used by stat is also 1024 byte?
# 5  
Old 02-15-2011
DC was using %s in the stat command, which reports size in bytes. This is the best way to go as the blocksize %b uses can vary from filesystem to filesystem, where df will use the same blocksize for all filesystems. So you won't be comparing apples with apples.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Ls directory size reporting byte size instead of file count

I have been searching both on Unix.com and Google and have not been able to find the answer to my question. I think it is partly because I can't come up with the right search terms. Recently, my virtual server switched storage devices and I think the problem may be related to that change.... (2 Replies)
Discussion started by: jmgibby
2 Replies

2. Programming

[c] How to calculate size of the file from size of the buffer?

Hi, Can I find size of the file from size of the buffer written? nbECRITS = fwrite(strstr(data->buffer, ";") + 1, sizeof(char), (data->buffsize) - LEN_NOM_FIC, fic_sortie); Thank You :) (1 Reply)
Discussion started by: ezee
1 Replies

3. Shell Programming and Scripting

Script to read file size and send email only if size > 0.

Hi Experts, I have a script like $ORACLE_HOME/bin/sqlplus username/password # << ENDSQL set pagesize 0 trim on feedback off verify off echo off newp none timing off set serveroutput on set heading off spool Schemaerrtmp.txt select ' TIMESTAMP COMPUTER NAME ... (5 Replies)
Discussion started by: welldone
5 Replies

4. UNIX for Dummies Questions & Answers

How to list all files in dir and sub-dir's recursively along with file size?

I am very new to unix as well as shell scripting. I have to write a script for the following requirement. In have to list all the files in directory and its sub directories along with file path and size of the file Please help me in this regard and many thanks in advance. (3 Replies)
Discussion started by: nmakkena
3 Replies

5. Shell Programming and Scripting

The scripts not able to make the file to size 0, every times it go back to its original size

#!/bin/sh ########################################################################################################## #This script is being used for AOK application for cleaning up the .out files and zip it under logs directory. # IBM # Created #For pdocap201/pdoca202 .out files for AOK #1.... (0 Replies)
Discussion started by: mridul10_crj
0 Replies

6. Solaris

Directory size larger than file system size?

Hi, We currently have an Oracle database running and it is creating lots of processes in the /proc directory that are 1000M in size. The size of the /proc directory is now reading 26T. How can this be if the root file system is only 13GB? I have seen this before we an Oracle temp file... (6 Replies)
Discussion started by: sparcman
6 Replies

7. Shell Programming and Scripting

find with file size and show the size

Hi All... is the below command be modified in sucha way that i can get the file size along with the name and path of the file the below command only gives me the file location which are more than 100000k...but I want the exact size of the file also.. find / -name "*.*" -size +100000k ... (3 Replies)
Discussion started by: rpraharaj84
3 Replies

8. Shell Programming and Scripting

to write a script to compare the file size in the current directory and previous dir

hi, i am new to this site. i want to write a script to compare the file size of the files in the current dir with the files in the previous directory. the files name will be same, but the filename format will be as xyzddddyymm.txt. the files will arrive with the month end date(i want to... (5 Replies)
Discussion started by: tweety
5 Replies

9. Shell Programming and Scripting

How to check the file size in a dir

Hi all, I need to check the size of all files in a DIR.Can any one help me out from this? This is my code: filenames=`ls -l | cut -c 55-90` for f in $filenames do if then echo $f done Output: file access denied. *files have read permission alone. (6 Replies)
Discussion started by: bsathishmca
6 Replies

10. Solaris

command to find out total size of a specific file size (spread over the server)

hi all, in my server there are some specific application files which are spread through out the server... these are spread in folders..sub-folders..chid folders... please help me, how can i find the total size of these specific files in the server... (3 Replies)
Discussion started by: abhinov
3 Replies
Login or Register to Ask a Question