touch (Create 0 byte file)


This command creates an empty file / zero byte file with current date and time. It is generally used to create trigger file consisting zero byte file size. 


If file is already present in the directory, it may change the file creation date to current time stamp / user given time stamp.


touch filename


example:

$pwd
/home/pinku/my_dir
$

$touch vikas.txt

$

$ls -l vikas.txt
-rw-r--r--  1 pinku  guest  0 Dec 27 01:32 vikas.txt
$







Options to use with command:

 

  • touch -m filename - changes only modification time


$touch -m vikas.txt
$ls -l vikas.txt
-rw-r--r--  1 pinku  guest  0 Dec 20 00:29 vikas.txt
$


  • touch -a filename - changes only access time


$ls -l vikas.txt
-rw-r--r--  1 pinku  guest  0 Dec 20 00:29 vikas.txt
$touch -a vikas.txt
$ls -l vikas.txt
-rw-r--r--  1 pinku  guest  0 Dec 20 00:29 vikas.txt
$


to check file by last access time:


$ls -lru
total 28
-rw-r--r--  1 pinku  guest    0 Dec 20 00:33 vikas.txt
-rw-r--r--  1 pinku  guest   98 Dec 18 12:28 myfile2.txt
-rw-r--r--  1 pinku  guest   49 Dec 18 12:28 myfile1.txt
drwxrwxrwx  2 pinku  guest  512 Dec 18 05:53 G_dir
drwxr-xr-x  3 pinku  guest  512 Dec 18 05:27 D_dir
drwxr-xr-x  2 pinku  guest  512 Dec 18 05:22 C_dir
drwxr-xr-x  2 pinku  guest  512 Dec 18 03:47 B_dir
drwxr-xr-x  2 pinku  guest  512 Dec 18 02:52 A_dir


  • touch -c filename - only change the modification time for existing file, do not create a new file


$pwd
/home/pinku/my_dir/A_dir

$
$ls -l
total 4
-rw-r--r--  1 pinku  guest  48 Dec 18 11:31 myfile1.txt

$
$touch -c sky.txt

$
$ls -l
total 4
-rw-r--r--  1 pinku  guest  48 Dec 18 11:31 myfile1.txt

$
$touch -c myfile1.txt

$
$ls -l
total 4
-rw-r--r--  1 pinku  guest  48 Dec 20 00:45 myfile1.txt
$







  • touch -t timestamp filename - use specified date and time in place of taking current system date and time


timestamp pattern should be like: YYYYMMDDHHMM

YYYY - year in 4 digit

MM - month in 2 digit

DD - date in 2 digit

HH - hours in 2 digit

MM - minutes in 2 digit


$ls -l
total 4
-rw-r--r--  1 pinku  guest  48 Dec 20 00:45 myfile1.txt
$touch -t 201505150000 myfile1.txt
$ls -l
total 4
-rw-r--r--  1 pinku  guest  48 May 15  2015 myfile1.txt
$


  • touch -m -r referenceOldFile1  newFile2 - Creates a new file newFile2 with the creation / modification time of previously created file referenceOldFile1 with its time stamp


$ls -l
total 4
-rw-r--r--  1 pinku  guest  48 May 15  2015 myfile1.txt
$touch -m -r myfile1.txt myfile2.txt

​$ls -l
total 4
-rw-r--r--  1 pinku  guest  48 May 15  2015 myfile1.txt
-rw-r--r--  1 pinku  guest   0 May 15  2015 myfile2.txt
$



Questions & Answers


Qs: How to create a hidden file with zero file size ? 

touch .filename - just put a dot before the file name


Qs: In how many ways we can create a blank file ?

  • > filename
  • cat filename > filename
  • touch filename
  • cat filename (ctrl d)
  • vi filename (:wq press enter)


NEXT->

Cracking IT Interview