The touch command in Unix and Linux is used to create empty files and update timestamps of existing files. It is a simple yet versatile command that allows you to interact with files by modifying their timestamps or creating new ones.
The basic syntax for the touch command is:
touch [option] file...option: Optional command-line options to control the behavior oftouch.file: The name(s) of the file(s) to be created or updated.
To create a new empty file:
touch newfile.txtThis command creates a new file named newfile.txt in the current directory.
To update the access and modification times of a file to the current time:
touch existingfile.txtThis command updates the timestamp of existingfile.txt to the current time. If existingfile.txt doesn't exist, it creates an empty file with that name.
To create multiple files at once:
touch file1.txt file2.txt file3.txtThis command creates three empty files named file1.txt, file2.txt, and file3.txt in the current directory.
To update only the access time of a file:
touch -a file.txtThis command updates the access time of file.txt to the current time.
To update only the modification time of a file:
touch -m file.txtThis command updates the modification time of file.txt to the current time.
To prevent touch from creating a new file if it doesn't exist:
touch -c file.txtThis command updates the timestamp of file.txt only if it already exists. If file.txt doesn't exist, touch does nothing.
To create placeholder files for testing or organizational purposes:
touch README.md LICENSE.txt TODO.txtThis command creates empty files named README.md, LICENSE.txt, and TODO.txt in the current directory.
To update the modification time of a script file to reflect recent changes:
touch script.shThis command updates the modification time of script.sh, which can be useful for tracking when changes were last made.
The touch command is a straightforward tool for creating empty files and modifying timestamps in Unix and Linux environments. Its simplicity and various options provide flexibility for tasks such as file creation, timestamp manipulation, and script management. Understanding these options and practical use cases can help you efficiently manage files and timestamps on your system.
Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.
A FILE argument that does not exist is created empty, unless -c or -h
is supplied.
A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.
Mandatory arguments to long options are mandatory for short options too.
-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)
-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: equivalent to -a
WORD is modify or mtime: equivalent to -m
--help display this help and exit
--version output version information and exit
Note that the -d and -t options accept different time-date formats.