Overview of touch
The primary function of the `touch` command is to update the timestamps of files. If the file does not exist, `touch` creates a new empty file with that name. This command is utilized to manipulate file system metadata without directly affecting the file content.
Main Roles of touch
`touch` command is mainly used for the following purposes:
Key Use Cases
- File Creation: Quickly creates non-existent files.
- Timestamp Update: Updates the access time (atime) and modification time (mtime) of files to the current time.
- Set Specific Time: Sets the timestamps of files to a specific point in the past or future.
- Script Automation: Used in shell scripts to create temporary files or to determine if files have changed in a build system.
- Backup and Synchronization: Utilized in backup scripts to determine whether to back up based on file timestamps.
Understanding File Timestamps
In the Linux file system, files have several types of timestamps:
Key Timestamps
- Access time (atime): The time when the file was last read. (Modified with `-a` option)
- Modification time (mtime): The time when the contents of the file were last changed. (Modified with `-m` option, default behavior)
- Change time (ctime): The time when the inode information (permissions, owner, number of hard links, etc.) or contents of the file were last changed. Using the `touch` command will always change `ctime` along with `mtime`. `ctime` cannot be changed directly with the `touch` command.
Key Options for the touch Command
`touch` command provides various options to change only specific timestamps of files or to set timestamps to a specific date and time.
1. Default Action and Creation
2. Select Timestamp Type
3. Set to Specific Time
Generated command:
Try combining the commands.
Description:
`touch` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Learn how to effectively create files and manage timestamps through various usage examples of the `touch` command.
Create an Empty File
touch new_document.txt
Creates an empty file named `new_document.txt` in the current directory.
Update Modification Time of an Existing File
touch report.log
Updates the last modification time (mtime) of the `report.log` file to the current time. The contents of the file remain unchanged.
Set File Timestamps to Yesterday's Date
touch -d "yesterday" old_data.txt
Sets the modification and access times of the `old_data.txt` file to yesterday's date.
Set Timestamps to Specific Date and Time
touch -t 2301010930.00 meeting_notes.txt
Sets the timestamps of the `meeting_notes.txt` file to January 1, 2023, at 9:30 AM.
Copy Timestamps from Another File
touch -r source_file.txt target_file.txt
Applies the modification and access times of `source_file.txt` to `target_file.txt`.
Create or Update Multiple Files at Once
touch file1.txt file2.txt file3.txt
Creates or updates the timestamps of three files: `file1.txt`, `file2.txt`, and `file3.txt` at once.