Views:
49,104β
Votes: 31β
β
Solution
Tag :
command-line
Link:
π See Original Answer on Ask Ubuntu β§ π
URL:
https://askubuntu.com/q/831418
Title:
What is the difference between touch file and > file?
ID:
/2016/09/30/What-is-the-difference-between-touch-file-and-_-file_
Created:
September 30, 2016
Edited: January 12, 2022
Upload:
September 15, 2024
Layout: post
TOC:
false
Navigation: false
Copy to clipboard: false
Both touch
and >
will create a new file if it doesnβt exist. As the following terminal commands show when you touch
an existing file the access/last modified time are updated. But if you >
to an existing file it is truncated and the last modified time is updated (access time is not). Note that >
does not delete/unlink the file. The inode stays the same β which is why >
/ or truncate
are commonly used to clear out log files even with an open file handle.
rick@dell:~$ > EmptyFile
rick@dell:~$ touch EmptyFile2
rick@dell:~$ ls Empty*
EmptyFile EmptyFile2
rick@dell:~$ ls -l Empty*
-rw-rw-r-- 1 rick rick 0 Sep 29 20:27 EmptyFile
-rw-rw-r-- 1 rick rick 0 Sep 29 20:27 EmptyFile2
rick@dell:~$ echo Hello > EmptyFile
rick@dell:~$ ls -l Empty*
-rw-rw-r-- 1 rick rick 6 Sep 29 20:28 EmptyFile
-rw-rw-r-- 1 rick rick 0 Sep 29 20:27 EmptyFile2
rick@dell:~$ > EmptyFile
rick@dell:~$ ls -l Empty*
-rw-rw-r-- 1 rick rick 0 Sep 29 20:28 EmptyFile
-rw-rw-r-- 1 rick rick 0 Sep 29 20:27 EmptyFile2
rick@dell:~$ echo Hello > EmptyFile
rick@dell:~$ touch EmptyFile
rick@dell:~$ ls -l Empty*
-rw-rw-r-- 1 rick rick 6 Sep 29 20:32 EmptyFile
-rw-rw-r-- 1 rick rick 0 Sep 29 20:27 EmptyFile2
As mentioned in comments, touch
is an external command and only operates on files. >
is a shell built-in feature that serves many different purposes. Sometimes you would see it used like cat Source.txt > Target.txt
.
A long form to empty a file would be:
cat /dev/null > EmptyMe.txt
Using > EmptyMe.txt
accomplishes the same thing in a compact format of redirecting nothing to the file.