DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Trending

  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  • Create Your Own AI-Powered Virtual Tutor: An Easy Tutorial
  • How the Go Runtime Preempts Goroutines for Efficient Concurrency

How the touch Command Works on Linux

Let's look at how the touch command works on Linux and MacOS.

By 
Johnny Simpson user avatar
Johnny Simpson
DZone Core CORE ·
Apr. 19, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
3.0K Views

Join the DZone community and get the full member experience.

Join For Free

The touch command lets us create files or update the access or modified date of a file. It was first created in 1979 by AT&T Bell Laboratories. Today, touch can be accessed via the terminal on Linux and Unix-based systems.

In this guide, let's look at how touch works. The syntax for touch can be seen below, where x is the name of the file we want to interact with or create, and [OPTIONS] are optional settings we can include:

touch [OPTIONS] x


Creating a File on Linux or Mac From Terminal

If you want to make a file on Linux or macOS from the terminal, we have to use the touch command by itself. For example, touch my-file.txt will create a file called my-file.txt in whatever directory you are currently in.

If you need help changing directories, read our article on the cd command.

touch my-file.txt


If you want to make multiple files on Linux or macOS, use the touch command, and separate each by spaces.

touch my-file.txt my-file-2.txt


If you want to create multiple numbered files in a particular file format on Linux or macOS, use the touch and use curly braces for the numbering. For example, touch my-file-{1..5}.txt will create my-file-1.txt through to my-file-5.txt.

touch my-file-{1..5}.txt


Changing the Access/Modification Time of a File on Linux or Mac

If you need to change an existing file's access or modification time to the current time, you can run the same command on an existing file. This will update the access and modification times of the file:

touch my-file.txt


If we want to update the access time and modification time separately, we use the -a option for access time and the -m option for modified time.

for example, the below code will update the modification time only:

touch -m my-file.txt


Updating the Timestamp of a File Only if It Exists on Linux or Mac

If we only want to update the timestamps of a file if the file exists on Linux or Mac and not create it if it doesn't, we need to use the -c option.

For example, the below code will update a file's timestamp should it exist to the current time, but it will do nothing if it doesn't:

touch -c my-file.txt


This can be combined with the -a and -m options to only update access or modification time, respectively:

touch -ca my-file.txt


Setting a Specific Timestamp for a File on Linux or Mac

If we want to set a particular time and date on a file, we can use the -d command (or --date=).

touch Time Format on MacOS

On the latest versions of macOS, the time format has to be in the form YYYY-MM-DDThh:mm: SS. For example, 1993-03-2609:44:00 sets the access and modified date to 26 March 1993 at 9:44:00:

touch -d '1993-03-2609:44:00' my-file.txt


touch Time Format on Linux

The touch time format on Linux-based systems is [YY]YYMMDDhhmm[.ss]. We can set a file's modified and access dates by using touch -d '[YY]YYMMDDhhmm[.ss]' file.txt. The first two digits of the year and the seconds are optional.

Typically it is easier to set the year as the complete, four-digit year code. However, the first two digits of the year are optional. If only two-year numbers are given, then most systems assume 70 to 99 means 1970 - 1999, and 00 to 37 means 2000 - 2037. This will presumably change as time goes on, so it is best to stick to 4 digits years.

For example, the below code sets my file's access and modified dates to 26 March 1993 at 9:44:00.

touch -d '199303260944.00' my-file.txt


Combining specific timestamps with other options in the touch command

As you might expect, you can combine this with other commands. For example, touch -ad sets the access date, and touch -md sets the modified date of a file.

Updating the Access/Modification Time of a Symlink in Linux or Mac

By default, if you try to update the access or modification time of a symlink, it will also update the reference file. To only update the symlink itself, use the -h option. For example, the below code updates both the access and modification time for a symlink called mySymLink:

touch -h mySymLink


This can be combined with other options, too. For example, this will update only the symlink access time to the current time:

touch -ha mySymLink


Setting the Timestamp of a File to Match Another File on Linux or Mac

You can also set a file's timestamp to match another file's timestamp using the -r option. For example, if you want new-file.txt to have the same timestamp as old-file.txt, we would write the following:

touch -r old-file.txt new-file.txt

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!