I am kind of newbie to Linux, and I wonder whether or not > and touch have the same effect. I mean, do the following two commands have the same effect?:

$ > file.txt

$ touch file.txt

If so, it would be great to know that I can just type `> file.txt' to create new files.

  • 5
    If file.txt already exists and is not empty, they certainly don't. – fkraiem Sep 30 '16 at 2:27
  • 1
    @fkraiem, could you please make an elaborated answer? – lmiguelvargasf Sep 30 '16 at 2:28
  • 4
    He's saying if you touch a file that contains data nothing will happen but if you > the file it will be erased. – WinEunuuchs2Unix Sep 30 '16 at 2:29
  • I got it. Thank you so much for that clarification @WinEunuuchs2Unix – lmiguelvargasf Sep 30 '16 at 2:32
  • Would you like to answer the question, have me answer the question, have @fkraiem answer it, or delete it? – WinEunuuchs2Unix Sep 30 '16 at 2:34

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. Typically you would see it used like cat source.fil > target.fil.

A long form to empty a file would be:

cat /dev/null > emptyme.fil

Using > emptme.fil accomplishes the same thing in a compact format of redirecting nothing to the file.

  • Use case: > file.txt is good for clearing out log files. – Rinzwind Sep 30 '16 at 10:16
  • 8
    It's not actually re-created. The existing file is truncated, which is different from unlinking the old file and creating a new one. The biggest observable difference is that if a process still has the old file open or mapped, it would still exist. But truncating would affect the file that other processes have open. – Peter Cordes Sep 30 '16 at 11:20
  • 1
    Not sure about "biggest observable" -- it's easy to observe whether hardlinks are broken (if you ln foo bar, and then delete and recreate foo, bar still has its prior contents). – Charles Duffy Sep 30 '16 at 16:50
  • 1
    Oh yes, that's an even more easily observable difference. I should have thought of that :P – Peter Cordes Oct 1 '16 at 6:43
  • @Charles-Duffy Sure, when there are 2 links to a fie that would be obvious I guess, that has nothing to do with what Peter Cordes was saying though. When you delete/unlink a file that is currently in use/has open filehandle it isn't deleted. for example, say you rm /var/log/nginx/access.log while nginx had that file open it would have 0 links but the inode would still exist. lsof -c nginx | grep access.log then with the pid, ls -l /proc/<pid>/fd/ | grep access.log and finally cat /proc/<pid>/fd/<filename from previous command>`. Commenting even though this is old. Hope it made sense – skrewler Mar 19 '18 at 21:29

There's a substantial difference between the two. Touch is a command used to time stamp a file. The > symbol is a standard output redirector.

Usage of the two explained

Usage of Touch

If the file doesn't exist it will create the file.

Touch is commonly used to create flags. For instance if you want to find all files between two times on your system you could use this sequence of commands:

$ touch -t 201609292200 starttime 
$ touch -t 201609292300 endtime

This will create the following files (or change the time-stamp if they exist):

-rw-rw-r-- 1 ljames ljames 0 Sep 29 23:00 endtime
-rw-rw-r-- 1 ljames ljames 0 Sep 29 22:00 starttime

Now you can use find to display or process all files that were modified on that hour (time between those two files time stamps, i.e. between 10 PM and 11 PM of September 29, 2016).

$ find ~/ -newer starttime ! -newer endtime -printf "%M %Tm/%Td %TH:%TM %TM %p\n"

This is a partial output of the above find command:

-rw-rw-r-- 09/29 22:08 08 /home/users/l/j/ljames/.thunderbird/lj7p62iq.default/gcontactsync/google_feed_backups/apollothethird@gmail.com_groups.xml
-rw-rw-r-- 09/29 22:08 08 /home/users/l/j/ljames/.thunderbird/lj7p62iq.default/gcontactsync/google_feed_backups/apollothethird@gmail.com.xml
-rw-rw-r-- 09/29 22:08 08 /home/users/l/j/ljames/.thunderbird/lj7p62iq.default/gcontactsync/address_book_backups/abook.mab.bak
-rw-rw-r-- 09/29 23:00 00 /home/users/l/j/ljames/endtime
drwx------ 09/29 22:44 44 /home/users/l/j/ljames/.config/google-chrome/Default/Extensions/Temp
drwx------ 09/29 22:44 44 /home/users/l/j/ljames/.config/google-chrome/Default/Extensions/fahmaaghhglfmonjliepjlchgpgfmobi
drwx------ 09/29 22:44 44 /home/users/l/j/ljames/.config/google-chrome/Default/Extensions/fahmaaghhglfmonjliepjlchgpgfmobi/1.337.0_0

Usage of Output redirection

The redirecting Symbol will redirect standard output to the specified file. Depending on your clobber/noclobber settings if could fail. Noclobber will only create the file if it doesn't exist.

There are a number of versions of the redirection output which includes:

>   - Redirect standard output. Create or overwrite the existing file.
>>  - Redirect append.  Create or append to existing file.
>&  - Redirect both standard output and standard error.
>>& - Redirect/Append both standard output and standard error

We could use our find command above to redirect the contents of all files created within the specified hour to a file that we could log and study later:

$ find ~/ -newer starttime ! -newer endtime -printf "%M %Tm/%Td %TH:%TM %TM %p\n" > ~/files.log
  • Many people have mentioned that, if file.txt exists and is not empty, then > file.txt will clear out its contents but touch file.txt will not.   But nobody has mentioned that >> file.txt is a good substitute for touch file.txt — it will create the file if it doesn't exist (assuming that you have permission to do so), but it will not truncate an existing file.   See What is the difference between “>” and “>>”? – G-Man Oct 1 '16 at 2:42
  • This is explained in the section Usage of Output redirection. Notice is says >> = Redirect append>. – L. D. James Oct 1 '16 at 4:41
  • Yes, and that's perfectly clear — to people who already know it. Put yourself in the shoes of the OP — you've posted a wall of text that's Too Long Too Read; he's not going to understand from your answer that >> file.txt is a workable substitute for touch file.txt. (The fact that you're the only person who mentioned >> at all is why I posted my comment under your answer, rather than under the question.) – G-Man Oct 1 '16 at 7:52
  • 1
    @G-Man Some people confuse the redirect symbol to be a substitute for touch. However it's not a substitute. The details of my answer is to clarify this mistake. The examples of using the two is to help clarify it. Please notice the first couple line answers the initial specifics of your current comment. It says there is a significant difference between the two. The OP is interested in the difference. So the other parts of the answer is to respond to the OP's interest in the two. There are different effects and there is a reasons for the difference. – L. D. James Oct 1 '16 at 7:59

> is the shell output redirection operator, used to redirect STDOUT of the preceding command(s) to the file descriptor mentioned after >. If the file is present, it's content will be truncated, as file with be open(2)-ed with O_TRUNC flag, and if the file doesn't exist, it will be created.

Important thing to note that, this redirection operator will be handled by shell before the preceding command runs, so this doesn't depend on any command, and handled by shell itself.

Note that, some shells (e.g. zsh) will behave differently with empty redirection like:


A typical workaround is to add the : (true) no-op command:

 : >file.txt

Traditionally, the main purpose of touch is to change the timestamp of a file, not creating a file.

touch creates a file, only when the file(s) mentioned in the argument doesn't exist, otherwise it changes the modification time of the file to current timestamp. You can also change access time, and inode creation time using touch.

  • zsh does support empty redirection - it acts quite similarly to redirecting cat with no arguments to a file. Basically whatever you type will be written to the file until an EOF character or an interrupt. This mirrors zsh's behavior with just <file, which simply prints the file contents, as cat would do. These may be configurable, I have seen <file open the file in a pager. – 8bittree Sep 30 '16 at 16:33
  • @8bittree What i meant is you need to input EOF (like doing cat >file.txt as you have said), where in bash just doing >file.txt would suffice. – heemayl Sep 30 '16 at 16:36
  • Fair enough, I suggested an edit that I think gets the idea across more clearly. – 8bittree Sep 30 '16 at 16:40
  • @8bittree Approved. – heemayl Sep 30 '16 at 16:41
  • (1) By default, touch updates both the modification time and the access time of the file.   There are options to update only one or the other.   (2) Unix / Linux / POSIX do not support “inode creation time”, in general.   Some file system types support it, but there is no universal way to look at it, let alone modify it.   Can you provide a reference that says that there is an incarnation of touch that allows the user to change inode creation time? – Scott Apr 27 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.