In Ubuntu, is an empty file the same as a text file? [closed]
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?
Solution 1:
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.
Solution 2:
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.