touch Command in Linux
Build an AI-First Career, Master the Complete Skillset
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
AI Forward Deployed Engineer Program
Full-stack engineering, production AI and client-facing consulting
+1000 moreOverview
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.
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.
How Scaler Transformed Careers in Different Fields
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
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.
Turn Learning into Career Growth
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.