MTI TEK
  • Home
  • About
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All Resources
Linux-Ubuntu | touch -- change file access and modification times
  1. Notes
  2. Examples
  3. Command Help (man touch)

  1. Notes
    By default, the touch command changes the modification and access times of the file to the current time.

    If the file does not exist, it will be created as an empty file with default permissions (typically 644, subject to umask). The file owner will be the user running the command, and the group will be the user's primary group.
  2. Examples
    • Create the file if it doesn't exist.
      $ touch file1
      
      $ ls -l file1
      -rw-rw-r-- 1 mtitek mtitek 0 Jul 17 14:11 file1
      
    • Do not create the file if it doesn't exist.
      $ touch -c file2
      
      # file2 doesn't exist in my system, so because of the option "-c" it won't be created.
      
      $ ls file2
      ls: cannot access 'file2': No such file or directory
      
    • Change only the access time of an existing file.
      $ touch -a file1
      
    • Change only the modification time of an existing file.
      $ touch -m file1
      
    • Set a specific timestamp.
      $ touch -t 202507181145 file1
      
      $ ls -l file1
      -rw-rw-r-- 1 mtitek mtitek 0 Jul 18  2025 file1
      
    • Use another file's timestamp as reference.
      $ touch -r reference_file target_file
      
  3. Command Help (man touch)
    The following options can be used:
    -a
        Change only the access time of the file.
        The modification time of the file is not changed unless the -m flag is also specified.
    
    -c, --no-create
        Do not create the file if it does not exist.
    
    -d, --date=STRING
        Parse STRING and use it instead of current time.
    
    -f
        (Ignored for compatibility with BSD)
    
    -h, --no-dereference
        If the file is a symbolic link, change the times of the link itself rather than the file that the link points to.
        Note that -h implies -c and thus will not create any new files.
    
    -m
        Change only the modification time of the file.
        The access time of the file is not changed unless the -a flag is also specified.
    
    -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: equivalent to -a
        WORD is modify or mtime: equivalent to -m
    
    --help
        Display help and exit.
    
    --version
        Output version information and exit.
    
© 2025 mtitek