Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unix file size 1

Status
Not open for further replies.

saltbits

Programmer
Jun 1, 2004
27
US
my requirement:
read txt file and if not empty do something.

I notice that on one unix server, i use vi to create an empty file, but the file size is 1, not 0. the bottom of the vi editor says the file has '1 line, 1 character' although i did a 'dd' and made sure the file is empty.

On another unix box, i am able to create a txt file using vi again that shows a size of 0.

is this really happening or am i doing something wrong here??!! I know this is an odd kind of question but i am a unix newbie and am hoping some expert can shed some light on this.... i cant leave it to unix servers or versions whatever being different cos in the end, it has to work 'no matter what'!

So basically, doing a "test ! -s" may not be the best way to go for me... is there a better and safer way to check for empty txt files.
 
A test like this ?
[ `wc -c </path/to/file` -gt 1 ] && echo NOT empty

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Sounds like you've got a linefeed in the "1 line, 1 character" file. How did you use 'dd' to make sure it was empty?

Are you saying that your test returns the same result for both the 0 and 1 byte files? Can you show the test in context?



Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
BTW, to create an empty file in ALL *nix flavors:
> /path/to/file

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Or if you really want to be anal:
cat /dev/nul > /path/to/file

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Thanks, all you guys!

I guess I'll just change the way the empty file is created and be sure that the size will be 0.
 
Rod,

>How did you use 'dd' to make sure it was empty?
I just opened the file and did a 'dd' vi command!

>Are you saying that your test returns the same result for >both the 0 and 1 byte files? Can you show the test in >context?

no my tests behave differently, which is why the problem.

anyways, i am going to just create the empty files like mentioned in this thread to be sure.

Thanks.
 
I'm guessing all of your boxen are not the same flavor of UNIX, or at least different flavors of vi.

From my testing, vi on AIX always inserts a linefeed at the end of a file, even an "empty" one. Neither "dd" nor "dG" commands can remove it. It's even there if no input at all has been performed on a previously non-existent file.

"vim" on Linux (which vi is usually a symbolic link to), on the other hand, can create a truly empty file.

To create your empty files for testing, use any of the techniques already described, or if they don't already exist you can use:

touch newemptyfilename

What sort of process is producing the files that this will ultimately be testing? You might want to check that it creates truly empty files before putting your script into production.



Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top