The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

If you can read me, I'm broken!

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.

⇧ Can I use cron to chime at top of hour like a grandfather clock? Laptop lid close doesn't suspend then the battery dies  β‡©