How to use touch command in Linux Guide for beginners 2021

1-How-to-use-touch-command-in-Linux

The touch command is a very common and standard command used in all UNIX/Linux based operating systems. Mostly, touch command in Linux is used to create a blank file.

But You can use to change and modify the timestamps of a file.

You can create a file with the help of other commands like a cat. I have explained how to use cat command in Linux to create a file, but not blank.

If you are looking to create a file without content you must use cat command in Linux. The file created using the touch command is empty.

This command can be used when the user doesn’t have data to store at the time of file creation.

Using touch command in Linux

In the following example, I am in the home directory and you can check what is your working directory by using pwd command.

My next task to check the existing files in the current directory by using ls command ls.

There is another option for you ll command (long listing) is used to gather more details about existing files.

As you can see in the below example.

$pwd
$ls
$ll

[[email protected] ~]$ pwd
/home/vijay
[[email protected] ~]$ ls
Desktop    Downloads  file2  newfile1  newfile3  Public     Videos
Documents  file1      Music  newfile2  Pictures  Templates
[[email protected] ~]$ ll
total 20
drwxr-xr-x. 2 vijay vijay  25 Mar 29 20:48 Desktop
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Documents
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Downloads
-rw-rw-r--. 1 vijay vijay 127 Mar 29 12:45 file1
-rw-rw-r--. 1 vijay vijay 111 Mar 29 13:08 file2
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Music
-rw-rw-r--. 1 vijay vijay 127 Mar 29 14:19 newfile1
-rw-rw-r--. 1 vijay vijay 238 Mar 29 14:27 newfile2
-rw-rw-r--. 1 vijay vijay 238 Mar 29 14:34 newfile3
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Pictures
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Public
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Templates
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Videos
[[email protected] ~]$ 

Use touch command in Linux to create blank file

The basic syntax of touch command to create a blank file is as follow:

$touch filename
$touch /path/to/directory/filename

If you want to create a file without content in the current directory then use the simple syntax “cat filename”.

when you use this command the file will be created in the current working directory.

But if you want to create a file in a different directory (desired directory) then you will have to provide the full path.

It means use touch command followed by the location path of the desired location. You can use the full path or partial path, Both will work fine.

See the example below:

I am going to create a file in current directory with name “touchfile”

[[email protected] ~]$ touch toucfile
[[email protected] ~]$ ls
Desktop    Downloads  file2  newfile1  newfile3  Public     toucfile
Documents  file1      Music  newfile2  Pictures  Templates  Videos
[[email protected] ~]$ 

Exapmle 2: Now, I am going to create a blank file into Documents folder. This folder has 2 paths, /home/vijay/Documents this is full path another is relative path Documents. Because it is related with current directory.

[[email protected] ~]$ touch Documents/file1
[[email protected] ~]$ touch /home/vijay/Documents/file2
[vija[email protected] ~]$ ls Documents/
file1  file2
[[email protected] ~]$ 

Available options in Touch command in Linux

What are the options used with touch command? Do you know?

If you don’t know about the touch command options, continue reading this post:

You can use multiple options with touch commands. See the manual page of the touch command to know more. You can use man command for it.

$man touch

I am going to describe some important options. See below table of options

OptionsDescription
-achange last access and modification time
-cCheck the file before creating
-mChange modification time only
-rUse the timestamp of another file
-tCreate a file with a specified time

Option -a with touch command

You can change or update the last access and modification times of a file by using the -a option with touch command as follows.

I will following command sets (-a) for the current time and date on a file as a timestamp. If the given file name does not exist, the command will create the new empty file with the given name.

[[email protected] ~]$ stat newfile1
  File: newfile1
  Size: 127       	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 51789940    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   vijay)   Gid: ( 1000/   vijay)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2020-03-29 14:19:45.260137110 +0530
Modify: 2020-03-29 14:19:40.669136983 +0530
Change: 2020-03-29 14:19:40.669136983 +0530
 Birth: -
[[email protected] ~]$ touch -a newfile1
[[email protected] ~]$ stat newfile1 
  File: newfile1
  Size: 127       	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 51789940    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   vijay)   Gid: ( 1000/   vijay)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2020-03-31 17:44:37.354462546 +0530
Modify: 2020-03-29 14:19:40.669136983 +0530
Change: 2020-03-31 17:44:37.354462546 +0530
 Birth: -
[[email protected] ~]$ 

Option -c with touch command

Option -c with touch command is used to check file is exist on a given location or not. If the file does not exist with the same name then don’t create the file.

Yes, -c option is used to check existing a file with the same name as well as avoid to create a new file.

See the following examples

[[email protected] ~]$ touch -c newfile1 
[[email protected] ~]$ ls
Desktop    Downloads  file2  newfile1  newfile3  Public     toucfile
Documents  file1      Music  newfile2  Pictures  Templates  Videos
[[email protected] ~]$ touch -c file3
[[email protected] ~]$ ls
Desktop    Downloads  file2  newfile1  newfile3  Public     toucfile
Documents  file1      Music  newfile2  Pictures  Templates  Videos
[[email protected] ~]$ 

Option -m with touch command in Linux

Every file has access, modify and change timestamps, You can use stat command to check the timestamp of any file.

You will see the modify timestamp of the file. -m option with touch command is used to change the modification time only.

Further mean, you can’t change the access and change the timestamp of the file.

See in the following example:

[[email protected] ~]$ stat newfile3 
  File: newfile3
  Size: 238       	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 51789939    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   vijay)   Gid: ( 1000/   vijay)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2020-03-29 14:34:42.141161869 +0530
Modify: 2020-03-29 14:34:32.970161616 +0530
Change: 2020-03-29 14:34:32.970161616 +0530
 Birth: -
[[email protected] ~]$ touch -m newfile3 
[[email protected] ~]$ stat newfile3 
  File: newfile3
  Size: 238       	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 51789939    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   vijay)   Gid: ( 1000/   vijay)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2020-03-29 14:34:42.141161869 +0530
Modify: 2020-03-31 17:50:32.727069849 +0530
Change: 2020-03-31 17:50:32.727069849 +0530
 Birth: -
[[email protected] ~]$

Option -r with touch command in Linux

touch command with -r option is used to use the timestamp of another file. In another word, you can copy timestamp from an old file to a new file.

You can use stat command to have look on timestamps of a file.

$stat

The example, I have an old_file I want to create new_file with the same timestamps, So I will use the following command:

$touch -r old_file new_file

[[email protected] ~]$ stat newfile3 
  File: newfile3
  Size: 238       	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 51789939    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   vijay)   Gid: ( 1000/   vijay)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2020-03-29 14:34:42.141161869 +0530
Modify: 2020-03-31 17:50:32.727069849 +0530
Change: 2020-03-31 17:50:32.727069849 +0530
 Birth: -
[[email protected] ~]$ touch -r newfile3 newfile4
[[email protected] ~]$ stat newfile4 
  File: newfile4
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d	Inode: 51717404    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   vijay)   Gid: ( 1000/   vijay)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2020-03-29 14:34:42.141161869 +0530
Modify: 2020-03-31 17:50:32.727069849 +0530
Change: 2020-03-31 17:52:15.394380682 +0530
 Birth: -
[[email protected] ~]$ 

Option -t with touch command in Linux

touch -t is used to create a file using a specified time, the time formate will be YYMMDDHHMM. And the command will be as follows:

$touch -t YYMMDDHHMM filename

[[email protected] ~]$ touch -t 198405101155 tfile
[[email protected] ~]$ stat tfile 
  File: tfile
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d	Inode: 51717427    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   vijay)   Gid: ( 1000/   vijay)
Context: unconfined_u:object_r:user_home_t:s0
Access: 1984-05-10 11:55:00.000000000 +0530
Modify: 1984-05-10 11:55:00.000000000 +0530
Change: 2020-03-31 17:54:39.722512665 +0530
 Birth: -
[[email protected] ~]$ 

Conclusion

I’ve almost covered all the options available in the touch command for more options use “man touch“.

If we’ve still missed any options and you would like to include in this list, please update us via comment box to contact me through email:- [email protected].

If you like our content, please consider buying us a coffee.
Thank you for your support!