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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
07-26-2007, 01:11 PM
#1
LQ Newbie
Registered: Oct 2006
Posts: 24
Rep:
[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?
07-26-2007, 01:45 PM
#2
Member
Registered: Dec 2006
Location: Lancaster, Ontario
Distribution: Fedora 6-7, CentOS 5, Red Hat 9, Ubuntu
Posts: 36
Rep:
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 .
07-26-2007, 01:50 PM
#3
LQ Addict
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,371
Rep:
FWIW, I can create a file that's 1 byte in size using vi (Elvis).
07-26-2007, 01:55 PM
#4
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: back to Arch
Posts: 16,687
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
07-26-2007, 02:15 PM
#5
Member
Registered: Dec 2006
Location: Lancaster, Ontario
Distribution: Fedora 6-7, CentOS 5, Red Hat 9, Ubuntu
Posts: 36
Rep:
That's it a 'new-line' character not 'end of file'...
07-26-2007, 02:16 PM
#6
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 9,005
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!
07-26-2007, 02:27 PM
#7
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
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 .
07-26-2007, 02:42 PM
#8
LQ Newbie
Registered: Oct 2006
Posts: 24
Original Poster
Rep:
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.
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 11:40 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News