What is the difference between touch file and > file?
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.
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/[email protected]_groups.xml
-rw-rw-r-- 09/29 22:08 08 /home/users/l/j/ljames/.thunderbird/lj7p62iq.default/gcontactsync/google_feed_backups/[email protected]
-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
>
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:
>file.txt
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
.