Sponsored Content
Top Forums UNIX for Dummies Questions & Answers determine the size of a file??? Post 45748 by Perderabo on Tuesday 30th of December 2003 02:19:37 PM
Old 12-30-2003
Things are not quite this simple. There are two different concepts of size here.

The 1558 does mean that a program can read 1558 bytes from this files. An attempt to read byte 1559 will fail with an EOF being returned. This is one concept of size.

But I think that Alan is actually interested in the second concept which is how much disk space is consumed by the file. The answer is that 8 * 512 = 4096 bytes of disk space is being using by this file. And that 8 came from the first column of the "ls" listing.

So if a program adds a byte to the file, making that 1558 to be a 1559, no additional disk space is needed.

This difference becomes very important because unix supports sparce files. If Alan wrote a program that seeks to byte 1,999,999,999 and writes a single byte, he will see something like this:
16 -rwx------ 1 root sys 2000000000 Dec 30 14:06 sparsefile

(Hmmmm... I would have predicted 8. Apparently a full block was allocated instead of a fragment. This was on HP-UX 11.00 on a vxfs filesystem.)

Database programs like Oracle will do this so it happens more often than you may think.

Here is my program in case you'd like to try it...
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

main()
{
        int fd;
        char byte=0;
        fd=open("sparsefile", O_CREAT|O_RDRW, 0700);
        lseek(fd, 1999999999, SEEK_SET);
        write(fd, &byte, 1);
        close(fd);
        exit(0);
}

 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to determine the max file size

Does anyone know a way to determine the maximum filesize on a file system on Solaris, HP-UX, AIX, Linux, and OSF1 using the command line? TIA (2 Replies)
Discussion started by: dknight
2 Replies

2. 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

3. HP-UX

determine the physical size of the hard disk

Hi is there a cmd in hpux 11 to determine the physical size of the hard disk. not bdf command. i have searched the other threads here but cant find an answer. thank you guys (4 Replies)
Discussion started by: hoffies
4 Replies

4. 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

5. UNIX for Dummies Questions & Answers

Determine switch buffer size

Hi everybody, I need to calculate the tcp buffer size of a network switch, since it's not specified in the manual; how do I do this? I have some machines connected to the switch and I can run some socket tests written in C between these machines (I can choose how many bytes to send and... (0 Replies)
Discussion started by: dimpim
0 Replies

6. 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

7. Programming

Perl : Inline program to determine file size

Hi, I have 5 files as below $ ll sam* -rw-rw-rw- 1 sam ugroup 0 Mar 21 06:06 sam3 -rw-rw-rw- 1 sam ugroup 0 Apr 3 22:41 sam2 -rw-rw-rw- 1 sam ugroup 17335 Apr 10 06:07 sam1 -rw-rw-rw- 1 sam ugroup 5 Apr 10 07:53 sam5 -rw-rw-rw- 1 sam ugroup 661 Apr 10 08:16 sam4 I want to list out... (4 Replies)
Discussion started by: sam05121988
4 Replies

8. 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

9. Shell Programming and Scripting

Script to determine Date,TotalFile,total size of file based on date

I have file listed like below -rw-r--r--+ 1 test test 17M Nov 26 14:43 test1.gz -rw-r--r--+ 1 test test 0 Nov 26 14:44 test2.gz -rw-r--r--+ 1 test test 0 Nov 27 10:41 test3.gz -rw-r--r--+ 1 test test 244K Nov 27 10:41 test4.gz -rw-r--r--+ 1 test test 17M Nov 27 10:41 test5.gz I... (5 Replies)
Discussion started by: krish2014
5 Replies

10. UNIX for Beginners Questions & Answers

How do I determine the best number to use for the bs (block size) operand of the dd command?

When I create a bootable Linux distro installation USB drive, I use this command: sudo dd if=/Path/to/linux_distro.iso of=/dev/rdisk<disk number> bs=<number of bytes> When I look it up, I've seen variations of people choosing 4M, and I think 8M, 2M, and maybe even 1M. If I leave the operand... (4 Replies)
Discussion started by: Quenz
4 Replies
lseek(2)							System Calls Manual							  lseek(2)

Name
       lseek, tell - move read or write pointer

Syntax
       #include <sys/types.h>
       #include <unistd.h>

       pos = lseek(d, offset, whence)
       off_t pos;
       int d, whence;
       off_t offset;

       pos = tell(d)
       off_t pos;
       int d;

Description
       The system call moves the file pointer associated with a file or device open for reading or writing.

       The descriptor d refers to a file or device open for reading or writing.  The system call sets the file pointer of d as follows:

       o    If whence is SEEK_SET, the pointer is set to offset bytes.

       o    If whence is SEEK_CUR the pointer is set to its current location plus offset.

       o    If whence is SEEK_END, the pointer is set to the size of the file plus offset.

       Seeking beyond the end of a file and then writing to the file creates a gap or hole that does not occupy physical space and reads as zeros.

       The system call returns the offset of the current byte relative to the beginning of the file associated with the file descriptor.

Environment
   System Five
       If  you	compile  a  program  in  the System Five environment, an invalid whence argument causes SIGSYS to be sent.  This complies with the
       behavior described in the System V Interface Definition (SVID), Issue 1.

Return Values
       Upon successful completion, a long integer (the current file pointer value) is returned.  This pointer is measured in bytes from the begin-
       ning  of  the file, where the first byte is byte 0.  (Note that some devices are incapable of seeking.  The value of the pointer associated
       with such a device is undefined.)  If a value of -1 is returned, errno is set to indicate the error.

Diagnostics
       The system call fails and the file pointer remains unchanged under the following conditions:

       [EBADF]	      The fildes is not an open file descriptor.

       [EINVAL]       The whence is not a proper value.

       [ESPIPE]       The fildes is associated with a pipe or a socket.

See Also
       dup(2), open(2)

																	  lseek(2)
All times are GMT -4. The time now is 02:23 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy