Quick File Creation (touch) and About Files - Images: Japanese
Hey there, everyone! Next up — let's look at a quick way to create files.
First, let's introduce the touch command. Its primary function is to update the timestamp — the last modified time — of a file or directory. Let's experiment with it.
Start by creating a test directory with mkdir:
[root@localhost ~]# mkdir test
Now check it with ls -l:
[root@localhost ~]# ls -l total 24 drwxr-xr-x. 2 root root 4096 Jun 13 10:41 2018 test
Notice "Jun 13 10:41 2018" — that's the timestamp, set to the time the directory was created.
To change it to "January 1, 1990, 2:03 AM", run:
[root@localhost ~]# touch -t 199001010203 test/ [root@localhost ~]# ls -l total 24 drwxr-xr-x. 2 root root 4096 Jun 1 02:03 1990 test
The format is touch -t timestamp target. The timestamp format is: year (4 digits), month, day, hour, minute — all run together without separators. So "January 1, 1990, 2:03 AM" becomes "199001010203".
To include seconds as well, append a dot and the seconds after the minutes. For "January 1, 1990, 2:03:04 AM", it looks like this — don't forget the dot:
[root@localhost ~]# touch -t 199101010203.04 test/
ls also has a --full-time option that displays the timestamp with full precision. Let's see it in action:
[root@localhost ~]# touch -t 199101010203.04 test/ [root@localhost ~]# ls --full-time total 24 drwxr-xr-x. 2 root root 4096 1991-01-01 02:03:04.000000000 +0900 test
You can see the timestamp has been updated to "1991-01-01 02:03:04.000000000" — down to the nanosecond.
Running touch without -t — just touch target — updates the timestamp to the current time:
[root@localhost ~]# touch test/ [root@localhost ~]# ls -l total 24 drwxr-xr-x. 2 root root 4096 Jun 13 11:05 2018 test
The timestamp is now "Jun 13 11:05 2018" — updated to the current time.
So that's the main functionality of touch. But here's the thing — it has a bonus feature: file creation. If you pass a filename that doesn't exist yet, touch will create it as a new empty file.
Let's try it. Right now, '/root/' only contains the 'test' directory:
[root@localhost ~]# ls test
Run 'touch test.txt':
[root@localhost ~]# touch test.txt
Now check with ls:
[root@localhost ~]# ls test test.txt
'test.txt' has been created. That's the bonus file-creation feature of 'touch'.
Interestingly, the file creation use case accounts for well over 99% of real-world usage of touch. The timestamp update feature — despite being the original purpose — is used far less often. So you can approach it the other way around: think of touch as a file creation command that happens to have a timestamp-updating side feature.
Alright, let's look at the newly created file. Running 'ls -l' on 'test.txt':
[root@localhost ~]# ls -l total 24 drwxr-xr-x. 2 root root 4096 Jun 13 15:28 2018 test -rw-r--r--. 1 root root 0 Jun 13 15:33 2018 test.txt
Notice the "0" in the size column — the file has zero bytes of data. A zero-byte file is a valid file, just empty.
Now take a look at the filename — specifically the extension.
The command was touch test.txt, so a .txt file was created. But can touch also create a JPEG image? Yep:
[root@localhost ~]# touch test.jpg [root@localhost ~]# ls -l total 24 drwxr-xr-x. 2 root root 4096 Jun 13 15:28 2018 test -rw-r--r--. 1 root root 0 Jun 13 17:19 2018 test.jpg -rw-r--r--. 1 root root 0 Jun 13 15:33 2018 test.txt
What about mp3? mp4? Those work too:
[root@localhost ~]# touch test.mp3 [root@localhost ~]# touch test.mp4 [root@localhost ~]# ls -l total 24 drwxr-xr-x. 2 root root 4096 Jun 13 15:28 2018 test -rw-r--r--. 1 root root 0 Jun 13 17:19 2018 test.jpg -rw-r--r--. 1 root root 0 Jun 13 17:31 2018 test.mp3 -rw-r--r--. 1 root root 0 Jun 13 17:31 2018 test.mp4 -rw-r--r--. 1 root root 0 Jun 13 15:33 2018 test.txt
For more on file extensions, this article is a helpful reference.
If you're wondering how 'touch' can create all these different file types, it comes down to how digital data works.
All digital data — text, images, video — is binary data at its core. It's all just sequences of 0s and 1s. So a text file and a JPEG are fundamentally the same kind of thing: binary data. The file extension is mainly used to identify the file type and to select the appropriate software to open it. Every file, at the hardware level, is the same thing.
That said, each file format has specific structural rules. A JPEG file that doesn't follow JPEG format rules won't open in an image viewer as an image. So extensions and format rules matter in practice.
The reason touch can "create" any file type is that the size is 0 — there's no content, so no format rules apply. In a strict sense, a zero-byte JPEG isn't really a JPEG image. Think of it more like an empty container, or the "egg" of a file — the outer shell is there, but there's nothing inside yet.
One more thing worth knowing: in UNIX-like OSes, the concept of file extensions is weaker than in Windows. Extensions originated quite early in computing history, but they became deeply ingrained mainly through Windows. On UNIX-like systems, extensions are more of a convention — you can freely put text content inside a file named '.jpg', or binary content inside one named '.txt'. So don't place too much trust in extensions alone.
Windows users are often warned that "changing an extension could make the file unopenable!" — which can create the impression that extensions and content are tightly linked. In reality, they're not as connected as that framing suggests. Keep that in mind as you work in UNIX environments.
Alright, that covers quick file creation with touch. It's used constantly, so commit it to memory.
In the next article, we'll cover echo and cat. See you there!
This article was written by Sakurama.
Author's beloved small mammal |
桜舞 春人 Sakurama HarutoA Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him. |
If you find any errors or copyright issues, please contact us.