touch

file managementLinux/Unix
The touch command is one of the most frequently used commands in Linux/Unix-like operating systems. touch Change file timestamps or create empty files

Quick Reference

Command Name:

touch

Category:

file management

Platform:

Linux/Unix

Basic Usage:

touch filename.txt

Common Use Cases

    Syntax

    touch [OPTION]... FILE...

    Options

    Option Description
    -a Change only the access time
    -c, --no-create Do not create any files
    -d, --date=STRING Parse STRING and use it instead of current time
    -f (Ignored) Historic option for compatibility
    -h, --no-dereference Affect each symbolic link instead of any referenced file (useful only on systems that can change the timestamps of a symlink)
    -m Change only the modification time
    -r, --reference=FILE Use this file's times instead of current time
    -t STAMP Use [[CC]YY]MMDDhhmm[.ss] instead of current time
    --time=WORD Change the specified time: WORD is access, atime, or use (same as -a); WORD is modify or mtime (same as -m)
    --help Display help and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the touch command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Create a new empty file touch new_file.txt
    # Create multiple files at once touch file1.txt file2.txt file3.txt
    # Update the access and modification times of an existing file touch existing_file.txt
    # Advanced Examples Advanced
    # Change only the access time touch -a file.txt
    # Change only the modification time touch -m file.txt # Set specific timestamp (format: [[CC]YY]MMDDhhmm[.ss]) touch -t 202301010830.00 file.txt # Use timestamp from reference file touch -r reference_file.txt target_file.txt # Don't create new files if they don't exist touch -c nonexistent_file.txt # Create empty files with specific permissions touch new_file.txt && chmod 644 new_file.txt # Create files in multiple directories touch dir1/file.txt dir2/file.txt dir3/file.txt # Use with find to update timestamps of multiple files find . -name "*.txt" -exec touch {} \; # Create a file with timestamp from yesterday touch -d "yesterday" file.txt # Create a file with specific date and time touch -d "2023-06-15 14:30:00" file.txt # Use with wildcard to update all files of a certain type touch *.html # Create a file that's one hour newer than a reference file touch -r reference_file.txt -d "+1 hour" target_file.txt

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `touch` command is a versatile utility in Unix and Linux systems primarily used for two purposes: creating empty files and updating file timestamps. Despite its simplicity, it has numerous practical applications in file management, scripting, and system administration. At its most basic, running `touch filename` will create an empty file if it doesn't exist, or update the access and modification timestamps to the current time if the file already exists. This behavior makes it one of the quickest ways to create empty files from the command line. Beyond creating files, `touch` is particularly useful for manipulating file timestamps. Every file in Unix/Linux has three timestamps: 1. **Access time (atime)**: When the file was last read or accessed 2. **Modification time (mtime)**: When the file's content was last modified 3. **Change time (ctime)**: When the file's metadata (permissions, ownership, etc.) was last changed The `touch` command can update the access and modification times, but not the change time directly (the change time will automatically update when you modify the other timestamps). Some common use cases for `touch` include: - Creating placeholder files for later use - Updating timestamps to trigger actions in systems that monitor file changes - Creating files with specific timestamps for testing or organizational purposes - Forcing file timestamp-based routines (like backup systems) to include specific files The command offers several options for fine-grained control: - Using `-a` or `-m` to change only the access or modification time respectively - Setting a specific timestamp with `-t` or `-d` - Using a reference file's timestamps with `-r` - Preventing the creation of new files with `-c` The `touch` command respects file permissions, so you need write permission to a file or its containing directory to update its timestamps. When creating new files, they will inherit the default permissions modified by your umask setting. While `touch` may seem like a simple utility, its ability to create files and manipulate timestamps makes it an essential tool for many system administration tasks and scripting operations.

    Related Commands

    These commands are frequently used alongside touch or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the touch command works in different scenarios.

    $ touch
    View All Commands