• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

Getting Started With The Touch Command In Linux

CJ Saathoff

Published on: January 30, 2024

Categories: Linux Command Line, VPS Hosting 0

In the realm of Linux file management, simplicity often conceals powerful tools. Understanding the significance of creating and handling files is fundamental for anyone navigating the command line. Enter the unassuming yet versatile touch command, quietly contributing to essential tasks for system administrators and users. In this post, we will explore the functionalities of the touch command, ranging from creating files to managing file timestamps.

Table of Contents
  • Introduction
  • Using Touch to Create a File
    • Basic File Creation
    • Creating Multiple Files
  • Timestamp
    • Updating Access and Modification Timestamps
    • Update Last Accessed Time
    • Update Last Modified Time
    • Preventing File Creation
    • Specifying Custom Timestamps
    • Changing The Timestamp Based On A Reference File
    • Changing Timestamps Relatively
    • Changing The Timestamp Of A Symbolic Link
  • Related Commands
    • The Stat Command Line Utility
    • The Ls Command In Linux
  • Conclusion

Introduction

If you asked a Linux system administrator to create a blank file, I would venture to say that many would instinctively turn to the simplicity and efficiency of the Linux touch command. Yet, the touch command is not confined to mere file creation, it also allows users to manipulate timestamps. This dual functionality positions touch as an indispensable tool. At the core lies timestamps – the markers that chronicle a file’s creation and modification. This aspect is crucial in system administration, scripting, and everyday computing tasks. Let’s explore the syntax, examples, and applications of the touch command as we delve into its capabilities to not only create empty files effortlessly but also to wield precise control over timestamps.

Using Touch to Create a File

While its primary function involves updating timestamps, touch also serves as a swift and efficient means to initiate new, empty files. Let’s explore how to wield the prowess of the touch command for this task.

Basic File Creation

The first thing you think when you hear about the touch command is using the touch command to create a new file. You generate an empty file with the specified name by simply typing touch filename in the terminal.

touch example.txt

Creating Multiple Files

It’s possible to create more than one file using the touch command by providing multiple filenames as arguments to the command.

touch file1.txt file2.txt file3.txt

Timestamp

While the touch command can create empty files, its core functionality lies in updating access and modification times with precision. Let’s delve into the individual options available through the touch command, a crucial aspect of Linux file management.

Updating Access and Modification Timestamps

Simply executing touch filename in the terminal on an existing file updates the access time and modification time of the specified file to the current time.

touch filename

Update Last Accessed Time

The touch command can specifically alter the last Accessed time of a file using the -a option.

touch -a filename

Update Last Modified Time

The touch command can specifically alter the last modified time of a file using the -m option.

touch -m filename

Preventing File Creation

The -c flag is particularly useful when you want to update timestamps but avoid creating new files.

touch -c existingfile.txt

Specifying Custom Timestamps

For more nuanced control, touch enables you to specify custom timestamps using the -t option followed by a timestamp in the format [[CC]YY]MMDDhhmm[.ss].

touch -t 202012251200.00 filename

In this example, the file filename receives a timestamp corresponding to December 25, 2020, at 12:00:00.

Changing The Timestamp Based On A Reference File

Leveraging a reference file, you can synchronize the timestamps of one file with another.

touch -r referencefile.txt targetfile.txt

This means the timestamp of the reference file will also be set on the target file.

Changing Timestamps Relatively

By understanding the relative nature of timestamps, you can use the -d option to change timestamps based on a specified time or date.

touch -d '2 days ago' filename

This example modifies the timestamps of the filename to be two days older than the current timestamp.

Changing The Timestamp Of A Symbolic Link

Running the command on symbolic links is possible, and it alters the timestamps of the link itself, not the linked file. The -h or –no-dereference option is particularly useful in this context, allowing you to affect each symbolic link instead of any referenced file.

touch -h symlink

This example updates the timestamps of the symbolic link named symlink without impacting the linked file. The -h option ensures that only the symbolic link’s timestamps are modified, and not those of the linked file. Note that this feature is available only on systems that support changing the timestamps of a symlink.

Related Commands

There are a handful of commands that prove to be invaluable with the touch command. As you work with timestamps you’re going to want to check to make sure any changes worked. So below let’s go over two that you can verify a file’s existence and its timestamps. These companion tools enrich your capabilities, offering nuanced perspectives and insights into your files.

The Stat Command Line Utility

The stat command provides comprehensive information about a file, including access time and modification time, permissions, and more. This is useful for verifying the changes made by the touch command.

stat filename

The Ls Command In Linux

The ls command is fundamental for listing files in a directory. When used with specific options, such as -l for a detailed list, it provides information about timestamps and permissions.

ls -l

Conclusion

In the realm of Linux file management, the touch command stands out as a versatile tool, for creating multiple files with a single command and allowing for precise timestamp manipulation. From setting custom timestamps using reference files to altering access and modification times, this tutorial has explored the touch command’s diverse capabilities. Whether you’re creating new files modifying timestamps, or synchronizing timestamps the touch command proves invaluable. This tutorial serves as a comprehensive guide, empowering both seasoned Linux users and newcomers to master the touch command.

CJ Saathoff

Embracing a lifelong passion for technology since childhood, CJ delved into the intricate workings of systems, captivated by the desire to understand the unknown. This innate curiosity led to his discovery of Linux, a revelation that resonated deeply. With more than 7 years of on the job experience, he’s honed his technical skills as a Geek and Senior Linux Systems Administrator.

Related Posts

Linux Process Management: ps, htop, kill, and nice

Linux has a sophisticated toolset to deal with processes in various circumstances from scripting, to monitoring, to control.

Nmap Scan Types: How to Scan a Server’s Network

The "nmap" command allows you to scan a remote server and determine its network configuration. Here are the various nmap scan types.

Git Rebase vs Merge vs Cherry-Pick: When to Use Each

Git has many tools for merging branches and commits. Rebasing, merging, and cherry-picking all have their uses.

Dealing with Cron Job Logs in Linux

Cron job logs keep track of which cron jobs run, and their details. This helps with troubleshooting and performance management.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA