What is touch command in linux: 4 Unlock Best Secrets

what is touch command in linux

The `touch` command in Linux allows you to quickly create a new empty file without launching a bloated text editor, or to update the timestamps on an existing file for use in a deployment script.

For us at Coding Journey, understanding the basics of `touch` and the functions it offers goes a long way in determining the difference between beginner & pro-level developer.

In this article series, Coding Journey walks you through all aspects of this powerful tool, beginning with its simplest syntax and moving on to more advanced usage including `touch` with TOC (Time of Change).

Whether automating your workflow or simply organizing your project file structure, comprehending how to “touch” a file is an essential capability you must develop now!

What is Touch Command in Linux?

In Linux, the touch command is the official method used to create an empty file or to update the last modification timestamp of an existing file. It is one of the essential commands available in the Linux environment and is known for its reliability and flexibility.

As you progress on your journey to becoming a proficient coder, you will realize that creating files by typing touch in the terminal is far more efficient than using a mouse. The touch command helps you work faster than the traditional right-click method and can be easily automated while working in a development environment.


How to Use the Touch Command in Linux

The formatting for linux’s touch command is as follows touch [options] filename

How to create a single file

To make an empty .txt file, use:

touch my-first-file.txt

How to create many files at once

You can make multiple files at once by separating the filenames by spaces:

touch index.html style.css script.js

How to create bulk files

If you wanted to generate 100 different files to test, you can quickly generate them using bracket expansion instead of manually typing each filename out one by one:

touch test_file{1..100}.txt


Understanding the Touch Command with TOC

In Linux, the touch command is used with the TOC to indicate how a timestamp or “time of change” is kept with the files. Each file in Linux is associated with three time attributes:

  • atime – access time – the last time an application or user accessed the file.
  • mtime – modification time – the last time a file was modified.
  • ctime – change time – the last time a file’s metadata changed (such as permissions or owner).

The touch command defaults to updating all three timestamps to the current time. However, specific flags allow you to modify or control which of these three timestamps you would like to update.


Best Practices for Using the Touch Command

Knowing how the Linux touch command works is not enough to be successful. It’s important to also be able to take full advantage of its capabilities in a way that uses it properly in the real world and follows current best practices as defined by the IT industry.

Here are some best practices when using touch:

  1. Use the touch command to help you automate your builds and backups, by assigning timestamps to files as needed, to manually trigger the script to be re-run.
  2. When you want to quickly set up your projects folder hierarchy for a new project, use bracket expansion. Use this command:
    touch index.html style.css README.md
  3. Verify changes made to the timestamps (TOC) of files when using the touch command. Always check the validity of your changes using the stat command to verify the timestamps of your files and determine whether any timestamps have been incorrectly modified by accident.
  4. If you need to create multiple configuration files that share the exact same timestamp, you can do so by using the reference flag (-r):
    touch -r master-file.conf new-file.conf.
  5. Avoid overwriting the permission attributes of files when using the touch command. Keep in mind that while you can modify the time stamp of a file without changing its contents, you are still interacting with the filesystem. Using sudo to run touch on a file located in your home directory could potentially overwrite its permissions.

Common Errors and How to Fix Them

  1. “Permission Denied” Error:

    This error will occur when you attempt to use the touch command to create or alter a file in a directory for which you lack write permission (ie: /root, /etc).

    Solution: Use the sudo command in front of the touch command.

  2. Incorrectly Specifying the Date/Time Format:

    If you use the -t flag with touch to set the date and time for the file’s TOC management, it is CRUCIAL that you input the date/time in the specification as exact:
    [[CC]YY]MMDDhhmm[.ss]. If you omit a digit from the date/time, you will receive an “invalid date format” error.

    Solution: Verify your date and time input before executing the touch command. For example, if you want your new file to be dated January 11, 2026, at 9 PM, you would input:
    touch -t 202601112100 filename.

  3. Creating a New File Instead of Updating an Existing File:

    Many people think that touch can only be used to update existing files. However, if you type the name of a file incorrectly, touch will not provide an error warning; instead, it will create a new empty file with the incorrect name.

    Solution: Use the -c (no-create) option with the touch command if you only want to modify an existing file’s timestamps without creating a new empty file.

  4. Confusing Touch with Mkdir:

    When new users try to create a new folder (directory) using the touch command and specifying the directory name, touch will create a new 0-byte file with the name specified instead of creating a new directory.

  5. Unintended ctime Updates:

    Many users are caught off guard when they see that the ctime of a file changed after they only wanted to change a timestamp (atime or mtime). In Linux, any alteration made to a given file will update that file’s ctime.

Beyond the Touch Command: Powerful Alternatives

The touch command is the most common and trusted way to create empty files and update timestamps in Linux. However, Linux offers several other powerful methods to achieve the same result. Choosing the right method depends on what you want to do—whether you need speed, content, formatting, or file size control during your Coding Journey.

1. Shell Redirection (>)

One of the fastest ways to create a file in Linux is by using shell output redirection. This method does not require any command name.

Command:

> new_file.txt

Best Use: Ideal when you want to create a file with the least number of keystrokes.

Caution: If the file already exists, this method will overwrite it and reduce its size to zero bytes, deleting all existing content.

2. The echo Command

If you want to create a file and write content to it at the same time, the echo command is more practical than touch.

Command:

echo "Welcome to Coding Journey" > readme.md

How to Create an Empty File:

echo -n > empty.txt

Append Mode: Use >> to add content without overwriting existing data.

3. The cat Utility

Although commonly used to read files, cat becomes a powerful file creation tool when combined with redirection.

  • Manual Input: Type cat > script.sh, enter your content, and press Ctrl + D to save.
  • Why use it? It allows you to write multiple lines into a file without opening editors like Nano or Vim.

4. Using printf for Formatted File Creation

For developers who need precise formatting—such as line breaks or tabs—printf is more reliable than echo.

  • The Command:
    printf "Line 1\nLine 2\nLine 3" > list.txt
  • The Benefit: It handles escape characters consistently across all Linux systems.

5. The dd Command (For Large Test Files)

When you need to create a file of a specific size—such as for disk or performance testing—dd is a professional alternative to touch.

  • The Command:
    dd if=/dev/zero of=test_1GB.img bs=1G count=1
  • How it works: It copies null bytes from /dev/zero to instantly create a file of an exact size.

6. The fallocate Command

fallocate is a modern and faster alternative to dd when you simply want to reserve disk space.

Command:

fallocate -l 500M dummy_file.bin

Key Difference: The touch command creates a 0-byte file, while fallocate allocates disk space immediately. Use fallocate when you need to reserve large storage quickly and efficiently.


Comparison Summary

Method Safety Level Can Add Content? Key Strength
Touch High (Safe) No Updating Timestamps
Redirection (>) Low (Overwrites) No Speed
Echo / Printf Medium Yes Quick Data Entry
fallocate Medium Space Only Size Allocation

The Quick Cheatsheet

Command Result
touch file.txt Creates file or updates current time.
touch -a file.txt Changes ONLY Access Time.
touch -m file.txt Changes ONLY Modification Time.
touch -r ref.txt file.txt Copies time from ref.txt to file.txt.
touch -d "2 days ago" Sets time to 48 hours in the past.

20 Frequently Asked Questions

  1. What is touch command in linux used for? Creating empty files and modifying timestamps.
  2. Does touch delete data? Never. It is safe to use on existing files.
  3. Can I create hidden files? Yes, use a dot: touch .hidden.
  4. What is the size of a new file? Exactly 0 bytes.
  5. How do I check the timestamp? Use the stat command.
  6. Can I use touch on a folder? It updates the folder’s timestamp but won’t create a new folder.
  7. Does it work on Ubuntu? Yes, and all other Linux distributions.
  8. What is -c flag? It stands for “no-create.”
  9. Can I create a Python file? Yes: touch main.py.
  10. How to create 50 files? touch file{1..50}.txt.
  11. What is atime? The Access Time of a file.
  12. What is mtime? The Modification Time of a file.
  13. Can I set a future date? Yes, Linux allows future timestamps.
  14. Why use touch instead of cat? Touch is faster for just creating the file without writing.
  15. Do I need root access? Only for directories you don’t own.
  16. Does touch work on Windows? Only through WSL or PowerShell.
  17. Can I change the year? Yes, with the -t flag.
  18. What is -r flag? It uses a reference file’s timestamp.
  19. How to create a file with spaces? Use quotes: touch "my file.txt".
  20. Where can I learn more? Keep following Coding Journey!

Final Note: Master the touch command in linux early in your career. It is the building block for shell scripting and server management. Happy coding!

Linux basic commands with example are the foundation for anyone starting their journey in Linux, cybersecurity, or software development. Commands such as pwd, ls, cd, mkdir, touch, and rm help users navigate the file system, manage files, and work efficiently from the terminal. Understanding these Linux basic commands with example improves accuracy, speed, and confidence while working in real-world Linux environments.

At Coding Journey, we have created a beginner-friendly and professionally structured guide that explains Linux basic commands with example in simple and practical language. This guide is ideal for students, professionals, and ethical hacking enthusiasts who want to build a strong Linux foundation.

Explore the complete guide here:

Linux Basic Commands with Example

Stay Connected with My Coding Journey

Don’t let scammers stop your professional growth. Join our community for more tech safety tips!

For more tutorials and guides, check out: CodingJourney.co.in

Leave a Reply

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