Linux 'Touch' Command

Basic Touch Command Examples in Linux

basic_touch_command_examples_in_linux

In Linux every single file is associated with timestamps, and every file stores the information of last access time, last modification time and last change time. So, whenever we create new file, access or modify an existing file, the timestamps of that file automatically updated.

The touch command is a standard program for Unix/Linux operating systems, that is used to create, change and modify timestamps of a file. Before heading up for touch command examples, please check out the following options.

Touch Command Options

  1. -a, change the access time only
  2. -c, if the file does not exist, do not create it
  3. -d, update the access and modification times
  4. -m, change the modification time only
  5. -r, use the access and modification times of file
  6. -t, creates a file using a specified time

1. How to Create an Empty File

The following touch command creates an empty (zero byte) new file called file1.

# touch file1

2. How to Create Multiple Files

By using touch command, you can also create more than one single file. For example the following command will create 3 files named, file1, file2 and file3.

# touch file1 file2 file3

3. How to Change File Access and Modification Time

To change or update the last access and modification times of a file called file3, use the -a option as follows. The following command sets the current time and date on a file. If the file3 file does not exist, it will create the new empty file with the name.

# touch -a file3

The most popular Linux commands such as find command and ls command uses timestamps for listing and finding files.

4. How to Avoid Creating New File

Using -c option with touch command avoids creating new files. For example the following command will not create a file called file3 if it does not exists.

# touch -c file3

5. How to Change File Modification Time

If you like to change the only modification time of a file called file3, then use the -m option with touch command. Please note it will only updates the last modification times (not the access times) of the file.

# touch -m file3

6. Explicitly Set the Access and Modification times

You can explicitly set the time using -c and -t option with touch command. The format would be as follows.

# touch -c -t YYDDHHMM file3

For example the following command sets the access and modification date and time to a file file3 as 17:30 (17:30 p.m.) December 10 of the current year (2012).

# touch -c -t 12101730 file3

Next verify the access and modification time of file file3, with ls -l command.

# ls -l

total 2
-rw-r--r--.  1 root    root   0 Dec 10 17:30 file3

7. How to Use the time stamp of another File

The following touch command with -r option, will update the time-stamp of file file2 with the time-stamp of file3 file. So, both the file holds the same time stamp.

# touch -r file3 file2

8. Create a File using a specified time

If you would like to create a file with specified time other than the current time, then the format should be.

# touch -t YYMMDDHHMM.SS deexams

For example the below command touch command with -t option will gives the deexams file a time stamp of 18:30:55 p.m. on December 10, 2012.

# touch -t 201212101830.55 deexams

We’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.


If you like dEexams.com and would like to contribute, you can also write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Python if , elif and else

Python Conditions and If statements

  • 0
Python for beginners

Learning Python Part 1

  • 3
Struct Alignment and Padding

Struct Alignment and Padding in C++ And C

  • 0
Friend function

Friend function C++

  • 0
Pointers

C++ Pointers

  • 0
Structures

C++ Structures

  • 0
Types of Inheritance in C++

Inheritance and access specifiers C++

  • 0
Java date pattern

Java Date Pattern Syntax

  • 0
Java Date and Calendar

Java Date formats

  • 0
JAVA Data Type

Data types in Java

  • 0
Java unreachable code

Unreachable Code Error in Java

  • 0

Post Comment

Comments(0)

WEB TECHNOLOGY

Articles

×

Forgot Password

Please enter your email address below and we will send you information to change your password.