Linux Tutorial
Linux File/Directory Management
Linux Packaging And Compression
Vim Text Editor
Linux Text Processing
Linux Software Installation
Linux User/User Group Management
Linux Permission Management
Linux Filesystem Management
Linux Advanced Filesystem Management
Linux System Management
Linux Backup and Recovery
Linux System Service Management
Linux System Log Management
Linux Boot Management
LAMP/LNMP Environment
SELinux Management
In this tutorial, we will cover the touch
command in Linux. The touch
command is a utility that allows you to create new empty files, change file timestamps, or both. It is frequently used to create placeholder files or update the access and modification times of existing files.
Basic Usage of the touch Command
The basic syntax for the touch
command is as follows:
touch [OPTIONS] FILE...
Creating a New File
To create a new empty file, simply use the touch
command followed by the file name:
touch example.txt
If the file does not exist, it will be created as an empty file. If the file already exists, its access and modification timestamps will be updated to the current time, but its contents will remain unchanged.
Creating Multiple Files
You can create multiple files at once by specifying multiple file names:
touch file1.txt file2.txt file3.txt
This command will create three new empty files: file1.txt
, file2.txt
, and file3.txt
.
Changing File Timestamps
By default, the touch
command updates the access and modification timestamps of the specified files to the current time. You can also set the timestamps to a specific date and time using the -t
option:
touch -t YYYYMMDDhhmm.ss example.txt
For example, to set the timestamps of example.txt
to January 1, 2023, at 12:00:00, run:
touch -t 202301011200.00 example.txt
Changing Only Access or Modification Timestamp
To update only the access timestamp, use the -a
option:
touch -a example.txt
To update only the modification timestamp, use the -m
option:
touch -m example.txt
Using a Reference File
You can set the timestamps of a file to match those of another file using the -r
or --reference
option, followed by the reference file name:
touch -r reference.txt example.txt
This command will update the timestamps of example.txt
to match those of reference.txt
.
Summary
The touch
command in Linux is a useful utility for creating new empty files, changing file timestamps, or both. By using various options, such as -t
, -a
, -m
, and -r
, you can set specific timestamps, update only access or modification times, and set the timestamps of one file to match those of another.
How to use the Linux touch
command:
touch
command in Linux is used to update the access and modification timestamps of files or create empty files if they do not exist.# Example: Using touch to update file timestamps touch filename
Creating empty files with touch
in Linux:
touch
can be used to create empty files. If the file already exists, it updates the timestamps.# Example: Creating an empty file with touch touch newfile.txt
Modifying file timestamps using touch
:
touch
updates the access and modification timestamps of a file. It can also create a new file with specified timestamps.# Example: Modifying file timestamps with touch touch -t YYYYMMDDHHMM.SS filename
Changing access and modification times with touch
:
touch
allows explicitly setting access and modification times using the -t
option.# Example: Changing access and modification times with touch touch -t 202201011200.00 filename
Updating file timestamps without modifying content:
touch
updates timestamps without changing file content, useful for scenarios where only timestamp modification is required.# Example: Updating file timestamps without modifying content touch filename
Batch updating file timestamps in a directory:
touch
can be used to update timestamps for multiple files in a directory, either individually or collectively.# Example: Batch updating file timestamps with touch touch /path/to/files/*
Preserving existing timestamps with touch
:
touch
preserves existing timestamps if the -c
option is used, preventing accidental modifications.# Example: Preserving existing timestamps with touch touch -c filename
Using touch
for future-dated files in Linux:
touch
can create or update files with future timestamps, allowing for scenario simulation or testing.# Example: Creating a file with a future date touch -t 209901011200.00 futurefile.txt
Troubleshooting common issues with touch
command:
touch
may involve incorrect timestamp formats, permission errors, or issues with file paths.# Example: Troubleshooting with touch touch -t invalidtimestamp filename