Preface
Introduction
Practice Setup
Linux Architecture
Basic Commands
Basic file management or operations on file
User administration
Networking
Linux Interview Questions

 

 

Touch

Touch command is one of the basic commands in Linux/UNIX flavor machines for working with files.

 

Touch command practical usages:

To create empty files in a directory.

To update the timestamp of the file without changing the content.

 

Creation of empty file:

Syntax:

touch file-name1

 

Command example output:

[admin@linbox1 ~]$ touch file1

 

[admin@linbox1 ~]$ ls -l
total 0
-rw-rw-r--. 1 admin admin  0 Jul 25 15:23 file1

 

If you observe that created file size is “0”. i.e, nothing is there in the file.

[admin@linbox1 ~]$ cat file1

 

Updating the modified time of the file:

If the file is already created then with touch command, its modified time is updated.

 

Example:

[root@linbox1 admin]# ls -l file1
-rw-rw-r--. 1 admin admin 12 Jul 25 15:28 file1

 

[root@linbox1 admin]# touch file1

 

[root@linbox1 admin]# ls -l file1
-rw-rw-r--. 1 admin admin 12 Jul 25 16:55 file1

 

Practical usage,

Sometimes, an automated script missed a file to consider, then we want to modify the file timestamp for getting picked up by script.

 

Creating multiple empty files with touch command:

With touch command, we can create multiple files at a time in directory.

Why we need multiple empty files? Any business case here?

Practical usage example…

 

Syntax:

touch file-name1 file-name2

 

 Command  Example Output:

[admin@linbox1 ~]$ touch abcfile1 abcfile2

 

Listing the files:

[admin@linbox1 ~]$ ls -l abc*
-rw-r--r--. 1 root root 0 Jul 25 17:13 abcfile1
-rw-r--r--. 1 root root 0 Jul 25 17:13 abcfile2

 

Topics Summary