touch - Create/Update Files

1. Introduction

The touch command in Linux is a versatile utility with two primary functions: updating the access and modification timestamps of files or directories to the current time or a specified time, and creating empty files if they do not exist. Its name reflects the metaphorical act of “touching” a file to update its metadata without altering its content. Widely used in scripting, software development, and system administration, touch is essential for tasks like triggering build processes, simulating file changes, or managing file metadata.

2. Basic Syntax

The touch command follows this syntax:

# Execute the touch command with optional flags and one or more file names
$ touch [OPTION]... FILE...
  • [OPTION]...: Optional flags that modify behavior, such as specifying which timestamp to update or preventing file creation.
  • FILE...: One or more filenames or paths to files/directories whose timestamps will be updated or created if they do not exist.

Note: touch is typically an external command (/bin/touch), but some shells may include a built-in version. This guide focuses on the GNU Coreutils implementation, standard in most Linux distributions.

3. Core Use Cases with Examples

To demonstrate touch’s functionality, we set up a test environment with a basic file and directory structure:

# Change the current working directory to the user's home directory (represented by ~)
$ cd ~

# Create a new directory named 'touch_test' to serve as a test environment
$ mkdir touch_test

# Navigate into the newly created 'touch_test' directory
$ cd touch_test

# Create a file named 'existing_file.txt' and write the text "Initial content." into it
$ echo "Initial content." > existing_file.txt

# Use touch to create three empty files named 'note.txt', 'report.pdf', and 'data.csv'
$ touch note.txt report.pdf data.csv

# Create two directories named 'logs' and 'scripts' within the current directory
$ mkdir logs scripts

# Create a file named 'app.log' inside the 'logs' directory and write "Log entry" into it
$ echo "Log entry" > logs/app.log

# Create a shell script file named 'myscript.sh' in the 'scripts' directory with a bash shebang line
$ echo '#!/bin/bash' > scripts/myscript.sh

# Change the permissions of 'myscript.sh' to make it executable
$ chmod +x scripts/myscript.sh

# Create a symbolic link named 'note_link' that points to the file 'note.txt'
$ ln -s note.txt note_link

# List the contents of the current directory with classification symbols (e.g., / for directories, @ for symlinks)
$ ls -F
data.csv  existing_file.txt  logs/  note.txt  note_link@  report.pdf  scripts/

Comment: In the examples below, ls output shows directory names with a trailing / (e.g., logs/) for clarity, even though ls without -p or -F does not append /. To view directories with / in real output, use ls -p or ls -F:

# List the contents of the current directory with a trailing slash appended to directory names
$ ls -p
data.csv  existing_file.txt  logs/  note.txt  note_link  report.pdf  scripts/

3.1 Creating a New Empty File

Creates an empty file if it does not exist.

Input:

# Use touch to create a new empty file named 'new_file.log' in the current directory
$ touch new_file.log

# List the details of 'new_file.log' to confirm its creation and check its size and timestamp
$ ls -l new_file.log

Output:

-rw-r--r-- 1 user user 0 May  4 20:44 new_file.log

Comment: The file is created with zero size, ideal for placeholders or log files.

Directory Impact: Adds new_file.log to the touch_test directory.

3.2 Creating Multiple New Empty Files

Creates multiple empty files in a single command.

Input:

# Use touch to create three empty files named 'config.yaml', 'temp.tmp', and 'backup.dat' at once
$ touch config.yaml temp.tmp backup.dat

# List the details of 'config.yaml', 'temp.tmp', and 'backup.dat' to verify their creation
$ ls -l config.yaml temp.tmp backup.dat

Output:

-rw-r--r-- 1 user user 0 May  4 20:44 backup.dat
-rw-r--r-- 1 user user 0 May  4 20:44 config.yaml
-rw-r--r-- 1 user user 0 May  4 20:44 temp.tmp

Comment: Multiple filenames are processed sequentially, creating each as needed.

Directory Impact: Adds config.yaml, temp.tmp, and backup.dat to the directory.

3.3 Updating Timestamps of an Existing File

Updates both access and modification times of an existing file to the current time.

Input:

# Display the initial access time (%x) and modification time (%y) of 'existing_file.txt' using a custom format
$ stat -c "%x %y" existing_file.txt
# The command displays the last access time (%x) and last modification time (%y) of the file in a custom format.

# Pause execution for 2 seconds to ensure a noticeable difference in timestamps
$ sleep 2

# Update both the access and modification timestamps of 'existing_file.txt' to the current time
$ touch existing_file.txt

# Display the updated access time (%x) and modification time (%y) of 'existing_file.txt' to verify the change
$ stat -c "%x %y" existing_file.txt

Output:

2025-05-04 20:44:00.000000000 +0530 2025-05-04 20:44:00.000000000 +0530
2025-05-04 20:44:02.000000000 +0530 2025-05-04 20:44:02.000000000 +0530

Comment: The file’s content remains unchanged; only timestamps are updated.

Directory Impact: No new files; existing_file.txt timestamps updated.

3.4 Updating Directory Timestamps

Updates timestamps of a directory.

Input:

# Display the initial access time (%x) and modification time (%y) of the 'logs' directory
$ stat -c "%x %y" logs

# Update both the access and modification timestamps of the 'logs' directory to the current time
$ touch logs

# Display the updated access time (%x) and modification time (%y) of 'logs' to verify the change
$ stat -c "%x %y" logs

Output:

2025-05-04 20:44:00.000000000 +0530 2025-05-04 20:44:00.000000000 +0530
2025-05-04 20:44:02.000000000 +0530 2025-05-04 20:44:02.000000000 +0530

Comment: Directories are treated like files for timestamp updates.

Directory Impact: No new files; logs directory timestamps updated.

3.5 Setting Specific Timestamps

Sets timestamps to a specific date using -d.

Input:

# Use the -d option to set the access and modification timestamps of 'note.txt' to December 25, 2024, at 08:00:00
$ touch -d "2024-12-25 08:00:00" note.txt

# List the details of 'note.txt' with full timestamps to verify the specified timestamp was applied
$ ls -l --full-time note.txt

Output:

-rw-r--r-- 1 user user 0 2024-12-25 08:00:00.000000000 +0530 note.txt

Comment: The -d option supports flexible date strings, useful for testing or synchronization.

Directory Impact: No new files; note.txt timestamps updated.

3.6 Using a Reference File

Sets timestamps to match another file’s using -r.

Input:

# Create a file named 'ref_file.txt' and set its access and modification timestamps to May 1, 2025, at 00:00:00
$ touch -d "2025-05-01 00:00:00" ref_file.txt

# Display the access time (%x) and modification time (%y) of 'ref_file.txt' to confirm its timestamp
$ stat -c "%x %y" ref_file.txt

# Use the -r option to update the timestamps of 'report.pdf' to match those of 'ref_file.txt'
$ touch -r ref_file.txt report.pdf

# Display the access time (%x) and modification time (%y) of 'report.pdf' to verify the timestamp match
$ stat -c "%x %y" report.pdf

Output:

2025-05-01 00:00:00.000000000 +0530 2025-05-01 00:00:00.000000000 +0530
2025-05-01 00:00:00.000000000 +0530 2025-05-01 00:00:00.000000000 +0530

Comment: Matches report.pdf timestamps to ref_file.txt.

Directory Impact: Adds ref_file.txt; updates report.pdf timestamps.

3.7 Handling Filenames with Spaces

Creates or updates files with spaces in names.

Input:

# Use touch to create or update a file named 'file with spaces.txt', quoting the name to handle spaces
$ touch "file with spaces.txt"

# List the details of 'file with spaces.txt' to confirm its creation or timestamp update
$ ls -l "file with spaces.txt"

Output:

-rw-r--r-- 1 user user 0 May  4 20:44 file with spaces.txt

Comment: Quoting ensures proper handling of spaces.

Directory Impact: Adds file with spaces.txt.

4. Key Options Explained (with Examples)

The touch command offers options for precise timestamp control, validated against the GNU Coreutils touch documentation.

4.1 -a (Change Only Access Time)

Updates only the access time (atime).

Input:

# Display the initial access and modification times of 'existing_file.txt' by filtering stat output with grep
$ stat existing_file.txt | grep 'Access|Modify'
# The above command displays only the last access and last modification times of file by filtering the full stat output using grep.
# The grep command searches for lines in text that match a given pattern and prints them.

# Pause execution for 2 seconds to ensure a noticeable difference in the access timestamp
$ sleep 2

# Use the -a option to update only the access time (atime) of 'existing_file.txt' to the current time
$ touch -a existing_file.txt

# Display the updated access and modification times to verify that only the access time changed
$ stat existing_file.txt | grep 'Access|Modify'

Output:

  Access: 2025-05-04 20:44:00.123456789 +0530
  Modify: 2025-05-04 20:44:00.123456789 +0530
  Access: 2025-05-04 20:44:02.987654321 +0530
  Modify: 2025-05-04 20:44:00.123456789 +0530

Comment: Only last access time or atime is updated, leaving last modify time or mtime unchanged.

4.2 -m (Change Only Modification Time)

Updates only the modification time (mtime).

Input:

# Display the initial access and modification times of 'existing_file.txt' by filtering stat output with grep
$ stat existing_file.txt | grep 'Access|Modify'

# Pause execution for 2 seconds to ensure a noticeable difference in the modification timestamp
$ sleep 2

# Use the -m option to update only the modification time (mtime) of 'existing_file.txt' to the current time
$ touch -m existing_file.txt

# Display the updated access and modification times to verify that only the modification time changed
$ stat existing_file.txt | grep 'Access|Modify'

Output:

  Access: 2025-05-04 20:44:02.987654321 +0530
  Modify: 2025-05-04 20:44:00.123456789 +0530
  Access: 2025-05-04 20:44:02.987654321 +0530
  Modify: 2025-05-04 20:44:04.112233445 +0530

Comment: Only mtime is updated, useful for build triggers.

4.3 -c, --no-create (Do Not Create Files)

Prevents file creation if the file does not exist.

Input:

# Attempt to update the timestamps of 'non_existent.txt' without creating it if it doesn’t exist
$ touch -c non_existent.txt

# List 'non_existent.txt' to verify that it was not created
$ ls non_existent.txt

Output:

ls: cannot access 'non_existent.txt': No such file or directory

Comment: Ensures only existing files are modified.

4.4 -r FILE, --reference=FILE (Use Reference File’s Times)

Sets timestamps to match another file’s.

Input:

# Create or update 'ref_file.txt' and set its timestamps to May 1, 2025, at 00:00:00
$ touch -d "2025-05-01 00:00:00" ref_file.txt

# Use the -r option to update the timestamps of 'data.csv' to match those of 'ref_file.txt'
$ touch -r ref_file.txt data.csv

# Display the access time (%x) and modification time (%y) of 'data.csv' to verify the timestamp match
$ stat -c "%x %y" data.csv

Output:

2025-05-01 00:00:00.000000000 +0530 2025-05-01 00:00:00.000000000 +0530

Comment: Synchronizes timestamps across files.

4.5 -d STRING, --date=STRING (Use Specific Date)

When paired with the -d or --date option, touch allows you to set file timestamps using a variety of human-readable date strings. These strings are parsed by the underlying date command, which supports both absolute and relative date and time expressions. Beyond the basic “yesterday,” “today,” and “tomorrow,” touch offers flexibility with many other formats. Below are examples of date strings you can use with touch, organized by category.

Input:

# Use the -d option to set the access and modification timestamps of 'report.pdf' to the previous day’s date and time
$ touch -d "yesterday" report.pdf

# List the details of 'report.pdf' to verify the timestamp was set to yesterday
$ ls -l report.pdf

Output:

-rw-r--r-- 1 user user 0 May  3 00:00 report.pdf

Comment: Supports flexible date formats, e.g., “2 days ago”.

4.5.1 Basic Relative Dates

These examples use simple relative expressions based on the current date and time.

  • Current date and time

    touch -d "now" filename

    Sets the timestamp to the current date and time.

  • Yesterday

    touch -d "yesterday" filename

    Sets the timestamp to the same time yesterday.

  • Tomorrow

    touch -d "tomorrow" filename

    Sets the timestamp to the same time tomorrow.


4.5.2 Relative Days and Weeks

These examples demonstrate how to shift timestamps by a specific number of days or weeks, either in the past or future.

  • Two days ago

    touch -d "2 days ago" filename

    Sets the timestamp to two days before the current date and time.

  • Three days from now

    touch -d "3 days" filename

    Sets the timestamp to three days after the current date and time.

  • Next week

    touch -d "next week" filename

    Sets the timestamp to the same day and time one week from now.

  • Two weeks ago

    touch -d "2 weeks ago" filename

    Sets the timestamp to two weeks before the current date and time.


4.5.3 Relative Days of the Week

You can set timestamps to specific days of the week, either in the past or future.

  • Next Monday

    touch -d "next Monday" filename

    Sets the timestamp to the next occurrence of Monday at the current time.

  • Last Friday

    touch -d "last Friday" filename

    Sets the timestamp to the most recent Friday at the current time.


4.5.4 Specific Dates and Times

These examples show how to set timestamps to exact dates and times using absolute formats.

  • Specific date

    touch -d "January 1, 2020" filename

    Sets the timestamp to January 1, 2020, at midnight (00:00:00).

  • Specific date and time

    touch -d "2020-01-01 12:00:00" filename

    Sets the timestamp to January 1, 2020, at noon (12:00:00).

  • Specific time on the current day

    touch -d "14:30" filename

    Sets the timestamp to 2:30 PM on the current day.


4.5.5 Combining Relative and Absolute Dates

These examples combine relative and absolute components for precise timestamp settings.

  • Tomorrow at 9 AM

    touch -d "tomorrow 9:00" filename

    Sets the timestamp to 9:00 AM tomorrow.

  • Last Monday at 3 PM

    touch -d "last Monday 15:00" filename

    Sets the timestamp to 3:00 PM on the most recent Monday.

  • Next Friday at noon

    touch -d "next Friday 12:00" filename

    Sets the timestamp to 12:00 PM on the upcoming Friday.

  • Two days ago at 5 PM

    touch -d "2 days ago 17:00" filename

    Sets the timestamp to 5:00 PM two days ago.


4.5.6 Relative Hours and Minutes

You can adjust timestamps by hours or minutes relative to the current time.

  • Five hours from now

    touch -d "5 hours" filename

    Sets the timestamp to five hours after the current time.

  • Two hours ago

    touch -d "2 hours ago" filename

    Sets the timestamp to two hours before the current time.

  • 30 minutes from now

    touch -d "30 minutes" filename

    Sets the timestamp to 30 minutes after the current time.


4.5.7 Time Zones

The touch command supports time zones when specified in the date string.

  • Specific date and time with time zone

    touch -d "January 1, 2020 12:00:00 EST" filename

    Sets the timestamp to January 1, 2020, at noon Eastern Standard Time.

  • Relative date with time zone

    touch -d "yesterday 14:00 UTC" filename

    Sets the timestamp to 2:00 PM UTC yesterday.


4.5.8 Additional Notes

  • Quoting: Date strings with spaces (e.g., “2 days ago”) must be enclosed in quotes to ensure proper parsing.
  • Default time: If no time is provided (e.g., “January 1, 2020”), the timestamp defaults to midnight (00:00:00).
  • Locale: Date strings like month and day names (e.g., “January,” “Monday”) should match the system’s locale, though English is assumed here.
  • Error handling: If a date string is invalid or ambiguous, touch will display an error (e.g., touch: invalid date format).
  • Multiple files: You can apply the same timestamp to multiple files (e.g., touch -d "yesterday" file1 file2).

The touch command’s ability to interpret these diverse date strings makes it a flexible tool for managing file timestamps in Ubuntu.

4.6 -t STAMP (Use Specific Timestamp Format)

When you use the -t STAMP option, it allows you to set a specific timestamp for the file instead of using the current time. This is particularly useful for setting precise dates and times in scripts or when you need to manipulate file timestamps for testing or organizational purposes.

The STAMP in touch -t STAMP must follow a specific numerical format: [[CC]YY]MMDDhhmm[.ss]. Here’s what each part of this format represents:

  • CC: The century (first two digits of the year, e.g., 20 for 2020). This is optional.
  • YY: The year within the century (last two digits of the year, e.g., 20 for 2020). This is optional if CC is provided.
  • MM: The month (01–12).
  • DD: The day of the month (01–31, depending on the month).
  • hh: The hour in 24-hour format (00–23).
  • mm: The minute (00–59).
  • .ss: The second (00–59). This is optional and, if omitted, defaults to 00. Note the dot (.) before the seconds.

How the Format Works

The number of digits you provide in the STAMP determines how the timestamp is interpreted:

  • 8 digits (MMDDhhmm): Specifies month, day, hour, and minute, using the current year. For example, 01011200 sets January 1st at 12:00:00 in the current year.
  • 10 digits (YYMMDDhhmm): Includes a two-digit year, interpreted relative to a pivot (often years 00–68 are 2000–2068, and 69–99 are 1969–1999, though this can vary). For example, 2001011200 sets January 1, 2020, 12:00:00.
  • 12 digits (CCYYMMDDhhmm): Specifies the full four-digit year for precision. For example, 202001011200 sets January 1, 2020, 12:00:00.
  • 14 digits (CCYYMMDDhhmm.ss): You can also add seconds by appending .ss. For instance, 202001011200.30 sets January 1, 2020, 12:00:30.

Input:

# Use the -t option to set the timestamps of 'note.txt' to May 4, 2025, at 09:30:15 (format: CCYYMMDDhhmm.ss)
$ touch -t 202505040930.15 note.txt

# List the details of 'note.txt' with full timestamps to verify the precise timestamp was applied
$ ls -l --full-time note.txt

Output:

-rw-r--r-- 1 user user 0 2025-05-04 09:30:15.000000000 +0530 note.txt

Comment: Precise control for specific timestamps.

Other Examples

Here are some practical examples:

  • touch -t 202001011200 note.txt: Sets the timestamp of note.txt to January 1, 2020, 12:00:00 (noon).
  • touch -t 2001011200 note.txt: Sets it to January 1, 2020, 12:00:00 (assuming 20 is interpreted as 2020).
  • touch -t 01011200 note.txt: Sets it to January 1st, 12:00:00 in the current year.
  • touch -t 192001011200.30 note.txt: Sets it to January 1, 1920, 12:00:30.

Key Details

  • Both Timestamps Updated: By default, touch -t STAMP updates both the access and modification times. To update only one, combine with -a (access time) or -m (modification time). For example, touch -a -t 202001011200 myfile sets only the access time.
  • File Creation: If the file doesn’t exist, touch creates it with the specified timestamp.
  • Time Zone: The timestamp is in local time, based on your system’s time zone settings.
  • Valid Dates: The date must be valid (e.g., 202002301200 for February 30th will fail). Errors like “invalid date format” will occur if the input is incorrect.
  • Range: Most modern systems support dates before 1970 (e.g., 1920) and far into the future, thanks to 64-bit time representations.

4.7 --time=WORD (Specify Timestamp to Change)

Selects which timestamp to update (access or modify).

Input:

# Use --time=access with -d to set only the access time of 'data.csv' to the previous day’s date and time
$ touch --time=access -d "yesterday" data.csv

# Display the access and modification times of 'data.csv' to verify only the access time was updated
$ stat data.csv | grep 'Access|Modify'

Output:

  Access: 2025-05-03 00:00:00.000000000 +0530
  Modify: 2025-05-01 00:00:00.000000000 +0530

Comment: Equivalent to -a or -m when used with -d or -t.

4.8 --help (Display Help)

Shows usage information.

Input:

# Display the help information for the touch command, including usage and available options
$ touch --help

Output (abridged):

Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.
...

Comment: Quick reference for options.

4.9 --version (Display Version)

Shows version information.

Input:

# Display the version information of the touch command, useful for identifying the installed version
$ touch --version

Output (abridged):

touch (GNU coreutils) 8.32
...

Comment: Useful for troubleshooting.

5. Combining Options

Combining options enables precise timestamp control:

5.1 Update Modification Time Only, No Creation

Input:

# Attempt to update the modification time of 'non_existent.txt' without creating it if it doesn’t exist
$ touch -c -m non_existent.txt

# List 'non_existent.txt' to verify that it was not created
$ ls non_existent.txt

# Update only the modification time of the existing file 'existing_file.txt' without creating new files
$ touch -c -m existing_file.txt

# Display the modification time of 'existing_file.txt' to verify the update
$ stat existing_file.txt | grep 'Modify'

Output:

ls: cannot access 'non_existent.txt': No such file or directory
  Modify: 2025-05-04 20:44:06.000000000 +0530

Comment: Combines -c and -m for safe updates.

5.2 Set Access Time to Reference File

Input:

# Use -a and -r to set only the access time of 'report.pdf' to match that of 'ref_file.txt'
$ touch -a -r ref_file.txt report.pdf

# Display the access time of 'report.pdf' to verify it matches 'ref_file.txt'
$ stat report.pdf | grep 'Access'

Output:

  Access: 2025-05-01 00:00:00.000000000 +0530

Comment: Updates only atime to match reference.

5.3 Set Specific Date for Modification Time

Input:

# Use -m and -d to set only the modification time of 'data.csv' to December 1, 2024
$ touch -m -d "2024-12-01" data.csv

# List the details of 'data.csv' to verify the modification time was updated
$ ls -l data.csv

Output:

-rw-r--r-- 1 user user 0 Dec  1 00:00 data.csv

Comment: Combines -m and -d for targeted updates.

6. Handling Special Cases

6.1 Filenames Starting with Hyphen

Use -- to treat hyphens as part of the filename.

Input:

# Use -- to create or update a file named '-file.txt', treating the hyphen as part of the filename
$ touch -- -file.txt

# List the details of '-file.txt' to confirm its creation or timestamp update
$ ls -l -- -file.txt

Output:

-rw-r--r-- 1 user user 0 May  4 20:44 -file.txt

Comment: Prevents misinterpretation of hyphens as options.

6.2 Filenames with Spaces

Quote or escape spaces in filenames.

Input:

# Use touch to create or update a file named 'file with spaces.txt', quoting the name to handle spaces
$ touch "file with spaces.txt"

# List the details of 'file with spaces.txt' to confirm its creation or timestamp update
$ ls -l "file with spaces.txt"

Output:

-rw-r--r-- 1 user user 0 May  4 20:44 file with spaces.txt

Comment: Ensures proper handling of spaces.

6.3 Permissions

Requires write permission in the directory to create files or write permission on the file to update timestamps.

Input:

# Attempt to create or update a file named 'test.txt' in the restricted '/root' directory
$ touch /root/test.txt

Output:

touch: cannot touch '/root/test.txt': Permission denied

Comment: Check permissions before using touch.

6.4 Directories

Updates directory timestamps like files.

Input:

# Update the access and modification timestamps of the 'scripts' directory to the current time
$ touch scripts/

# Display the access and modification times of 'scripts' to verify the update
$ stat scripts | grep 'Access|Modify'

Output:

  Access: 2025-05-04 20:44:06.000000000 +0530
  Modify: 2025-05-04 20:44:06.000000000 +0530

Comment: Useful for directory metadata management.

7. Frequently Asked Questions (FAQ)

7.1 What is the main purpose of the touch command?

The primary purpose of touch is to update the access and modification timestamps of a file to the current time or a specified time. It also creates a new empty file if the specified file does not exist, making it a convenient tool for file creation.

Example:

# Use touch to create 'file.txt' if it doesn’t exist or update its timestamps if it does
$ touch file.txt

# List the details of 'file.txt' to confirm its creation or timestamp update
$ ls -l file.txt

Output:

-rw-r--r-- 1 user user 0 May  4 20:44 file.txt

7.2 How can I create a new file with touch?

Use touch filename to create a new empty file. If the file already exists, its timestamps are updated without altering its content.

Example:

# Use touch to create a new empty file named 'new_file.txt'
$ touch new_file.txt

# List the details of 'new_file.txt' to confirm its creation
$ ls -l new_file.txt

Output:

-rw-r--r-- 1 user user 0 May  4 20:44 new_file.txt

7.3 Can I update only the access time or only the modification time?

Yes, the -a option updates only the access time (atime), and the -m option updates only the modification time (mtime).

Example:

# Use the -a option to update only the access time of 'file.txt' to the current time
$ touch -a file.txt

# Display the access time of 'file.txt' to verify the update
$ stat file.txt | grep 'Access'

# Use the -m option to update only the modification time of 'file.txt' to the current time
$ touch -m file.txt

# Display the modification time of 'file.txt' to verify the update
$ stat file.txt | grep 'Modify'

Output:

  Access: 2025-05-04 20:44:02.987654321 +0530
  Modify: 2025-05-04 20:44:04.112233445 +0530

7.4 How do I set a specific timestamp for a file?

Use the -d option with a date string or -t with a numeric timestamp format (e.g., [[CC]YY]MMDDhhmm[.ss]).

Example:

# Use the -d option to set the timestamps of 'file.txt' to December 25, 2024, at 08:00:00
$ touch -d "2024-12-25 08:00:00" file.txt

# List the details of 'file.txt' with full timestamps to verify the specified timestamp
$ ls -l --full-time file.txt

Output:

-rw-r--r-- 1 user user 0 2024-12-25 08:00:00.000000000 +0530 file.txt

7.5 What happens if I use touch on a directory?

touch updates the timestamps of the directory itself, not its contents, treating it like a file for timestamp purposes.

Example:

# Update the access and modification timestamps of the 'mydir' directory to the current time
$ touch mydir/

# Display the access and modification times of 'mydir' to verify the update
$ stat mydir | grep 'Access|Modify'

Output:

  Access: 2025-05-04 20:44:06.000000000 +0530
  Modify: 2025-05-04 20:44:06.000000000 +0530

7.6 Can I use touch to create multiple files at once?

Yes, specify multiple filenames in a single command to create or update them.

Example:

# Use touch to create or update three files named 'file1.txt', 'file2.txt', and 'file3.txt' at once
$ touch file1.txt file2.txt file3.txt

# List the details of 'file1.txt', 'file2.txt', and 'file3.txt' to confirm their creation or update
$ ls -l file1.txt file2.txt file3.txt

Output:

-rw-r--r-- 1 user user 0 May  4 20:44 file1.txt
-rw-r--r-- 1 user user 0 May  4 20:44 file2.txt
-rw-r--r-- 1 user user 0 May  4 20:44 file3.txt

7.7 Why would I want to change a file’s timestamp?

Changing timestamps is useful for triggering build systems (e.g., make), simulating file changes, testing time-based logic, or sorting files by modification time with ls -t.

Example:

# Update the timestamps of 'source.c' to the current time to trigger a build process
$ touch source.c

# Run the make command to rebuild the project based on the updated timestamp
$ make

Output (example):

gcc -o program source.c

7.8 Does touch change the creation time (birth time) of a file?

No, touch does not modify the birth time (creation time), as it is not universally tracked by filesystems. It only updates access and modification times.

Example:

# Display the birth time of 'file.txt' if available (often not tracked by all filesystems)
$ stat file.txt | grep 'Birth'

Output:

  Birth: -

By default, touch updates the timestamps of the symbolic link itself. Use -h or --no-dereference to update the target file’s timestamps.

Example:

# Create a symbolic link named 'link.txt' that points to 'target.txt'
$ ln -s target.txt link.txt

# Update the timestamps of the symbolic link 'link.txt' itself to the current time
$ touch link.txt

# Use the -h option to update the timestamps of the target file 'target.txt' through the link
$ touch -h link.txt

# List the details of 'link.txt' to confirm its existence and link status
$ ls -l link.txt

Output:

lrwxrwxrwx 1 user user 9 May  4 20:44 link.txt -> target.txt

7.10 Are there any security implications of using touch?

Creating files requires write permission in the target directory, and updating timestamps requires write permission on the file or ownership. Improper use in restricted directories can lead to permission errors, but touch itself poses minimal security risks.

Example:

# Attempt to create or update a file named 'test.txt' in the restricted '/root' directory
$ touch /root/test.txt

Output:

touch: cannot touch '/root/test.txt': Permission denied

7.11 Can I use touch to create a file with specific permissions?

No, touch creates files with default permissions (typically 644, modified by umask). Use chmod to set specific permissions afterward.

Example:

# Use touch to create a new empty file named 'new_file.txt' with default permissions
$ touch new_file.txt

# Change the permissions of 'new_file.txt' to read/write for the owner only (600)
$ chmod 600 new_file.txt

# List the details of 'new_file.txt' to confirm the permission change
$ ls -l new_file.txt

Output:

-rw------- 1 user user 0 May  4 20:44 new_file.txt

7.12 How does touch behave with wildcards?

Wildcards are expanded by the shell, allowing touch to create or update multiple files matching a pattern.

Example:

# Use touch with a brace expansion wildcard to create or update files 'file1.txt', 'file2.txt', and 'file3.txt'
$ touch file{1..3}.txt

# List the details of all files matching the pattern 'file*.txt' to confirm their creation or update
$ ls -l file*.txt

Output:

-rw-r--r-- 1 user user 0 May  4 20:44 file1.txt
-rw-r--r-- 1 user user 0 May  4 20:44 file2.txt
-rw-r--r-- 1 user user 0 May  4 20:44 file3.txt

7.13 Can touch update timestamps recursively for all files in a directory?

No, touch does not support recursive updates. Use find with touch for recursive operations.

Example:

# Use find to locate all files in the current directory and subdirectories, then update their timestamps with touch
$ find . -type f -exec touch {} ;

# List the details of the current directory to observe the updated timestamps
$ ls -l

7.14 What is the difference between touch -c and touch --no-create?

They are identical; both prevent touch from creating new files, only updating existing ones.

Example:

# Attempt to update the timestamps of 'non_existent.txt' without creating it if it doesn’t exist
$ touch -c non_existent.txt

# List 'non_existent.txt' to verify that it was not created
$ ls non_existent.txt

Output:

ls: cannot access 'non_existent.txt': No such file or directory

7.15 How can I test if a file exists using touch?

Use touch -c and check the exit status ($?): 0 if the file exists, 1 if it does not.

Example:

# Attempt to update the timestamps of 'existing_file.txt' without creating it if it doesn’t exist
$ touch -c existing_file.txt

# Display the exit status of the previous command (0 indicates success, meaning the file exists)
$ echo $?

# Attempt to update the timestamps of 'non_existent.txt' without creating it if it doesn’t exist
$ touch -c non_existent.txt

# Display the exit status of the previous command (1 indicates failure, meaning the file does not exist)
$ echo $?

Output:

0
1

7.16 Why update a file’s timestamp?

To trigger build systems, signal script events, test time-based logic, or sort files.

Input:

# Use the -m option to update only the modification time of 'source.c' to the current time
$ touch -m source.c

# Run the make command to rebuild the project based on the updated modification time
$ make

Output:

gcc -o program source.c

7.17 How is touch different from mkdir?

touch manages file timestamps or creates empty files; mkdir creates directories.

Input:

# Use touch to create or update an empty file named 'dir.txt'
$ touch dir.txt

# Use mkdir to create a new directory named 'dir'
$ mkdir dir

# List the contents of the current directory with a trailing slash for directories to distinguish them
$ ls -p

Output:

dir/  dir.txt

7.18 How to create a file with content?

Use echo or editors, as touch creates empty files.

Input:

# Create a file named 'file.txt' and write the text "Content" into it using echo
$ echo "Content" > file.txt

# Display the contents of 'file.txt' to verify the text was written
$ cat file.txt

Output:

Content

7.19 Does touch change ctime?

Yes, updating atime or mtime also updates ctime due to inode metadata changes.

Input:

# Display the initial change time (ctime) of 'existing_file.txt'
$ stat existing_file.txt | grep 'Change'

# Update the timestamps of 'existing_file.txt' to the current time, which also updates ctime
$ touch existing_file.txt

# Display the updated change time (ctime) of 'existing_file.txt' to verify the change
$ stat existing_file.txt | grep 'Change'

Output:

  Change: 2025-05-04 20:44:00.000000000 +0530
  Change: 2025-05-04 20:44:06.000000000 +0530

7.20 Can I set timestamps older than creation time?

Yes, atime and mtime can be set to any time, even before birth time.

Input:

# Use the -d option to set the timestamps of 'file.txt' to January 1, 2020
$ touch -d "2020-01-01" file.txt

# List the details of 'file.txt' to verify the timestamp was set to an older date
$ ls -l file.txt

Output:

-rw-r--r-- 1 user user 0 Jan  1 00:00 file.txt

8. Conclusion

The touch command is a deceptively simple yet powerful tool in Linux, serving dual purposes: updating file and directory timestamps and creating empty files. Its flexibility shines in scenarios like triggering build systems, testing time-sensitive applications, or preparing placeholder files for scripts. Options such as -a, -m, -d, and -r provide precise control over timestamp manipulation, while -c and -h handle edge cases like non-existent files and symbolic links. By integrating touch with commands like find or chmod, users can extend its utility for complex tasks. Whether you’re a developer, system administrator, or casual user, mastering touch enhances your ability to manage Linux filesystems efficiently and effectively.


9. touch Command: Reference Table of Key Options

Option(s)DescriptionExample CommandUse Case
(none)Update atime & mtime to now, or create empty filetouch file.txtCreate file if missing, update times if exists
-aChange only access time (atime)touch -a file.txtUpdate last read time without changing modification time
-mChange only modification time (mtime)touch -m file.txtUpdate last modified time, e.g., to trigger build tools
-c, --no-createDo not create file if it doesn’t existtouch -c file.txtOnly update timestamps of existing files
-r FILE, --reference=FILEUse reference FILE’s times instead of current timetouch -r ref.log target.logSynchronize timestamps between two files
-d STRING, --date=STRINGUse specified date STRING instead of current timetouch -d "yesterday" f.txtSet timestamp to a specific or relative date/time
-t STAMPUse specific timestamp [[CC]YY]MMDDhhmm[.ss]touch -t 202501151000 f.txtSet timestamp using a precise numeric format
--time=WORDSpecify which time (access or modify) to settouch -d "now" --time=access fileExplicitly set only atime or mtime when using -d or -t
-h, --no-dereferenceAffect symbolic link instead of referenced filetouch -h link.txtUpdate the link’s timestamps, not the target’s
--Signify end of optionstouch -- -filename.txtHandle filenames that start with a hyphen
--helpDisplay help informationtouch --helpView command usage and options
--versionDisplay version informationtouch --versionCheck command version for troubleshooting