touch Command in Linux

Learn via video courses
Topics Covered

Overview

The touch command in Linux is used to create empty files or update timestamps on existing files. It is a simple yet powerful command-line utility that is available in almost all Linux distributions. This command can be used to change the modification and access times of a file, as well as create new files.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Linux touch Command Syntax

The syntax for the touch command is as follows:

Where:

  • touch: The command to be executed.
  • [OPTIONS]: One or more options to modify the behavior of the command.
  • FILE(s): One or more files to be created or modified.

touch Command Options:

  • -a: Change the access time of the file only.
  • -c: Do not create the file if it does not exist.
  • -d: Use a specified date and time instead of the current time.
  • -m: Change the modification time of the file only.
  • -r: Use the timestamp of another file instead of the current time.
  • -t: Use a specified timestamp instead of the current time.

Example Usages

  • Create a new file.:

    Explanation: This command will create a new file named 'newfile.txt' in the current directory.

  • Update the modification time of a file.:

    Explanation: This command will change the modification time of 'myfile.txt' to the current time without changing the access time.

  • Update the access time of a file.:

    Explanation: This command will change the access time of 'myfile.txt' to the current time without changing the modification time.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Tips

  • If the file already exists, touch will update its timestamp to the current time.

  • If the file does not exist, touch will create an empty file.

  • By default, touch updates both the access and modification times of a file.

Advanced Use Cases of touch Command in Linux

  • Change the timestamp of a file to match another file.:

    Explanation: This command will change the timestamp of 'myfile.txt' to match the timestamp of 'newfile.txt'.

  • Set a specific timestamp for a file.:

    Explanation: This command will set the timestamp of 'myfile.txt' to March 28, 2022 at 15:12.

  • Update the timestamps of all files in a directory.:

    Explanation: This command will update the timestamps of all files in the current directory.

Conclusion

  • The touch command is a simple yet powerful tool for creating or modifying files and their timestamps in Linux.

  • The command provides several options for modifying the behavior of the timestamp updates, including changing only the access or modification time and setting a specific timestamp.

  • Remember that touch can also be used to create empty files if they do not exist.

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more