touch

>Usage

The touch command is primarily used to create empty files and update the timestamps of existing files.

This command doesn't give an output but it generates a message in case of an error.

>Synopsis

touch [option] file

>Options

Creating Files: The touch command is commonly used to create empty files. When this command run in order to create a new file, there is no return output in the terminal except in cases of error.

Example:

Dots

-zsh

> touch newfile.txt


Updating Timestamps: If the specified file already exists, touch updates its access and modification timestamps to the current time. This command updates the timestamps of a file to the current date and time. There will be no output in the terminal. The command completes silently.

Example:

Dots

-zsh

> touch newfile.txt


-a

Updates only the access time of the file, leaving the modification time unchanged.

Example:

Dots

-zsh

> touch -a myfile.txt


Example: Before and After Timestamps

Dots

-zsh

> stat myfile.txt


> -rw-r--r-- 1 user user 0 Nov 16 09:00 myfile.txt


> touch -a myfile.txt
> stat myfile.txt


> -rw-r--r-- 1 user user 0 Nov 16 09:05 myfile.txt

-t

You can set a specific timestamp using the -t option followed by a timestamp in the format ; YYYYMMDDhhmm. Creates the file if it doesn't exist. No output unless there's an error.

Example:

Dots

-zsh

> touch -t 202301011200.00 newfile.txt


-m

Updates only the modification time of the file, leaving the access time unchanged

Example:

Dots

-zsh

> touch -m myfile.txt


-r

You can also use another file's timestamp as a reference with the -r option. This command sets the timestamps of a file to match those of another. If referencefile.txt does not exist, the command will return an error, indicating that the reference file cannot be found.

Example(Before and After):

Dots

-zsh

> stat referencefile.txt
> -rw-r--r-- 1 user user 0 Nov 15 10:00 referencefile.txt
> stat targetfile.txt
> -rw-r--r-- 1 user user 0 Nov 16 09:00 targetfile.txt



> touch -r referencefile.txt targetfile.txt
> stat referencefile.txt
> -rw-r--r-- 1 user user 0 Nov 15 10:00 referencefile.txt
> stat targetfile.txt
> -rw-r--r-- 1 user user 0 Nov 15 10:00 targetfile.txt

>Quiz

Progress: Question ? out of ?

Question Title

Page written by Sweetmoreton Beryl