LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 07-26-2007, 01:11 PM   #1
geekpie
LQ Newbie
 
Registered: Oct 2006
Posts: 24

Rep: Reputation: 0
[not strictly linux] why is file 2 bytes not 1 byte?


If I open a new file in vi, type 1 character (no new-line), and save, the filesize is 2 bytes. Why isn't it 1 byte?

Is it even possible to create a file with size 1 byte?
 
Old 07-26-2007, 01:45 PM   #2
Brydon
Member
 
Registered: Dec 2006
Location: Lancaster, Ontario
Distribution: Fedora 6-7, CentOS 5, Red Hat 9, Ubuntu
Posts: 36

Rep: Reputation: 15
End of file character which is hidden, control-z if I remember it correctly.

Or it's the way the file is stored in the filesystem, more likely the answer. Let's find out what everyone else thinks.

Last edited by Brydon; 07-26-2007 at 01:48 PM.
 
Old 07-26-2007, 01:50 PM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
FWIW, I can create a file that's 1 byte in size using vi (Elvis).
 
Old 07-26-2007, 01:55 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
My editor (nano) also throws in a "newline" (0x0a) for free.

If you really, really want a 1-byte file, you can delete the newline char. thusly:
cat filename|tr -d '\n' > newfilename

I could have sworn there was also a way to do this with sed, but I can't find it.

To see what actually gets into a file:
cat filename| hexdump -C|more
 
Old 07-26-2007, 02:15 PM   #5
Brydon
Member
 
Registered: Dec 2006
Location: Lancaster, Ontario
Distribution: Fedora 6-7, CentOS 5, Red Hat 9, Ubuntu
Posts: 36

Rep: Reputation: 15
That's it a 'new-line' character not 'end of file'...
 
Old 07-26-2007, 02:16 PM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Another way to create a 1-byte file could be
Code:
echo -n A > onebytefile
A quick way to verify (dump) the content of a file is
Code:
od -c onebytefile
0000000   A
0000001
For a 2-byte file, you will see
Code:
0000000   A  \n
0000002
which reveals the unwanted presence of the newline character. Nothing hides from the good ol' od command!
 
Old 07-26-2007, 02:27 PM   #7
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Geekpie - What do you get from 'echo $LANG'? Are you using UTF16?

Edit: Never mind. No difference.

Last edited by ilikejam; 07-26-2007 at 02:29 PM.
 
Old 07-26-2007, 02:42 PM   #8
geekpie
LQ Newbie
 
Registered: Oct 2006
Posts: 24

Original Poster
Rep: Reputation: 0
yes od and hexdump reveal what is going on.

I hadn't realised you get a \n at the end of the final line of your file in vi, but I suppose it makes sense.
 
  


Reply

Tags
byte, filesize


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why I haven't gone strictly to linux. Haniblectre General 27 02-20-2008 05:02 PM
C: a float from 2 bytes of a 4-byte array carcassonne Programming 8 01-17-2007 06:23 PM
I want to change one byte in a large file, pajout Linux - Newbie 4 03-23-2006 10:53 AM
Not strictly linux -Azureus fraz Linux - Software 8 11-14-2004 03:43 PM
Why I haven't gone strictly to linux. Haniblectre Linux - General 2 08-25-2004 06:31 AM


All times are GMT -5. The time now is 07:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration