6

I'm new to Ubuntu.

If in the shell I do:

touch file

It creates a file in the current directory, and if I open this file it appears as a blank text file. Also, I can do

echo "test" > file
cat file

And what I get is a string.

So is an empty file equal to a text file?

Zanna
  • 70,465
  • 4
    I don't understand the aim of your question. An empty file is an empty file. There's no way to answer the question “What type of things are in this empty box?“ because it is undefined. Also, cd never creates new files no matter what arguments to supply to it. Which command did you actually run? – David Foerster Jan 01 '18 at 16:18
  • 2
    Sorry , code edited – Tantaros Jan 01 '18 at 16:19
  • What did you expect an empty file to be? To have it be something else (like an "empty" pdf) you would need it to hold something to identify it as such. So would need content. And empty pdf (convert xc:none -page Letter empty.pdf) is 1871 bytes for instance. I do not understand the question. My comment would be: yes, touch will create emty text files. So what now? – Rinzwind Jan 01 '18 at 16:21
  • 1
    An empty file is just an empty file. It has no designation of what it is. Once it contains text, then it is a text file. When in doubt, use the file command to see what a file is. – Terrance Jan 01 '18 at 16:28
  • 1
    On a more abstract, philosophical level, the content of an empty file is nothingness and nothingness has by its very nature no properties (except this meta-property). Thus nothingness can't have the properties of or be text and an empty file can't be a text file. – David Foerster Jan 01 '18 at 16:35
  • 1
  • 2
    In logic, this is called a vacuous truth. The answer to the question "do the elements of the empty set have property X" is always "Yes", regardless of what "X" is. So, an empty file is a text file. It is also an image file. And a sound file. And everything else. – Jörg W Mittag Jan 01 '18 at 17:57
  • 2
    @JörgWMittag: Not entirely, since many “complex” file formats require at least some kind of header and an empty file isn't exactly the same as an empty set. It's a sequence/tuple of length zero, so the statement “sequence starts with the numbers/bytes X, Y, Z…” is false. Anyway, there are multiple slightly different concepts and formal definitions of emptiness but none of them allow a meaningful interpretation of the file type of an empty file. – David Foerster Jan 01 '18 at 20:13

6 Answers6

15

It creates a file in the current directory, if I open this file it appears as a blank text file.

If you run touch non_existing_filename, it will create an empty file. The file is empty, containing nothing. This does not mean it's a text file, or any other kind of file.

If you run touch existing_file, touch will update the timestamps on the file.

echo "test" > file
cat file

And what I get is a string. So is an empty file equal to a text file?

Yes, if you write a string into an empty file, you will get a string when you read the content of the file. Note that > overwrites all content in a file, and the result would be the same if the file was empty, non-existing or some other file (like an image). You can use >> to append text to a file.

Note that tools, such as redirection (>>, > and so on), do not care about file type. It's a perfectly valid operation to append a text string to a file containing, for instance, a JPEG image. It's also a perfectly valid operation to cat some image file. Not that I recommend you try; it will not display correctly on your terminal.

The file system has no concept of file types. It doesn't care what content a file has - and there is no difference between the different file types on the file system level.

Traditionally Unix is far less dependent on file types than Windows, and it's perfectly reasonable to have a text file called README, whilst on Windows it would be common to have the extension .txt for a text file.

If you wonder what kind of file you have on your hands, the utility file is fairly useful:

$ file  auth.log
auth.log: ASCII text
$ file Dokken.mp4 
Dokken.mp4: ISO Media, MP4 Base Media v1 [IS0 14496-12:2003]

file decides the content type based on a description of common characteristics of each file, commonly called a file signature. For instance, a debian will always start with the bytes 21 3C 61 72 63 68 3E, and if we see those bytes at the beginning of the file, we probably have a debian package on our hands.

The file utility uses so called magic files to decide which filetype it is looking at. More information about this can be found in the man-page for magic (e.g. man magic on a Linux system).

Note that file is not infallible, merely a very good indicator. If I create a file that contains the bytes ed ab ee db 0d 25, file will identify this as /tmp/foobar: RPM v13.37 bin. ed ab ee db is the required header for RPM files, and 0d 25 is 1337 in hex... This file will not be a valid RPM package in any way.

vidarlo
  • 22,691
9

If you use touch on a file that does not exist it will create an empty file for you. If you use it on an existing file it will update the access times, etc. Check the type of a file with the file command. There can be null binary files.

For those files file utility returns the following:

$ touch new
$ file new
new: empty

But for file with some text it will return "ASCII text"

$ echo "text" > text
$ file text 
text: ASCII text

But some graphical applications such as Pluma or Gedit will open both files as text.

N0rbert
  • 99,918
Kyle H
  • 1,044
  • 6
  • 7
5

There is an old saying in Linux / Unix

Everything is a file

With that said, Linux identifies types of files by magic , not necessarily by content

See How does Ubuntu know what file type a file without extension is?

Panther
  • 102,067
  • 3
    It's not just an old saying, it's fundamental to the design of *nix (and existed long before Linux). Not only is everything a file, all files are just files. Whether it's a text file is simply a matter of what's in it. If it just contains ASCII characters, it's text (at least to English speakers). – jamesqf Jan 01 '18 at 18:48
  • So every file is just a text with ASCII characters. Does linux use the 7 bits or 8 bits ASCII table ? – Tantaros Jan 02 '18 at 11:05
1

TLDR: No. A text file must not be empty to be detected as such. Sure, it can be empty.

touch sets the file timestamp, and creates the file if it does not exist. So, what type of file is an empty file?

$ ls -l test
-rw-r--r-- 1 user users 0  1. Jän 18:36 test
$ file test
test: empty

The file utility tells it as a separate file type. This is because an empty file could be anything that makes sense to a number of applications: It can be an empty plain text file, an empty CSV table, an empty raw data stream (it cannot be an empty file of type GZ, JPEG etc, because these files do not have 0 bytes even if the applications treat them as empty, as they all contain headers and other stuff). So, the type cannot be any better defined as empty.

1

An empty file is just an empty file - it has no contents, so it's not a text file. You can fill an empty file with anything you want (to make it a text file, or a binary file, or whatever other type of file you want). Here's a simple test:

Create an empty file: $ touch somefile

See what the file type is:

$ touch somefile
$ file somefile
$ somefile: empty

Append some text to "somefile" and see what type of file it is:

$ echo "Some text..." >> somefile
$ file somefile
somefile: ASCII text

Create a small binary:

#include <iostream>
int main(int argc, char *argv[]) {
    std::cout << "Hello, cruel world!" << std::endl;
    return 0;
}

$ g++ test.cpp -o test

Delete "somefile", touch it again, then append contents of binary to "somefile":

$ rm somefile
$ touch somefile
$ cat test >> somefile
$ file somefile
somefile: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=18c391d1f1bba8069fe85962458c7609bd8f516e, not stripped
0

In short, empty file is called zero-byte file. This file has no data nor even just header so you cannot tell type though some file managers and other programs may treat them as plain text file (this is not true but good choice to treat). Read more from here.

WailenB
  • 11