touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple command that is used to update the access and modification timestamps of a file. It is often used to create new empty files or to update the timestamps of existing files.

The basic syntax of the touch command is:

touch [options] [file]

When executed without any options, the touch command will create a new empty file with the specified name if it does not already exist. If the file already exists, it will update the access and modification timestamps of the file to the current time. For example, the following command will create a new empty file named "example.txt" in the current directory:

touch example.txt

If you want to create multiple files at the same time, you can use the touch command multiple times with different file names or you can use wildcard characters to specify multiple files at once.

touch file1 file2 file3

or

touch file*

The touch command also has options that allow you to specify the timestamps for the access and modification time of a file. The option -a is used to change the access time only, and the option -m is used to change the modification time only. The following command will change the access time of a file to the current time:

touch -a file.txt

and the following command will change the modification time of a file to the current time:

touch -m file.txt

Additionally, the touch command also has option -d which is used to set the timestamp of a file to a specific date and time. The option -d takes a date and time as an argument in the format of [[CC]YY]MMDDhhmm[.SS].

touch -d '2022-01-01 12:00:00' file.txt

The touch command is also often used in shell scripts and automation tasks to create new empty files or update the timestamps of existing files. For example, a script may use the touch command to create a new empty file before writing some data to it, or to update the timestamps of a file before or after performing some action.

The touch command is a simple command that can be used in many ways to create new empty files or update the timestamps of existing files. Here are some additional features and uses of the touch command:

  • The touch command can also be used to update the timestamps of a directory. By default, touch command only updates the timestamps of the file, but with the option -t, it can be used to update the timestamps of the directory.
    • touch -t 202212251200 directory
  • The touch command can be used to create a new file with specific permissions using the option -c. When this option is used, touch will not create a new file if it does not exist, but it will update the timestamp of the file.
    • touch -c file.txt
  • The touch command can also be used to change the owner and group of a file or directory. The option -r is used to reference another file and change the timestamps of the current file to match the reference file.
    • touch -r reference_file file.txt
  • The touch command can also be used with the option -h, this option is used to change the timestamps of a symlink instead of the file it points to.
    • touch -h symlink
  • The touch command can also be used to create a new file with specific size, the option -s is used to specify the size of the file in bytes.
    • touch -s 100 file.txt
  • The touch command can also be used to create a new file with specific date and time using the option -d and -t together. The option -d is used to specify the date and time in the format of [[CC]YY]MMDDhhmm[.SS], and -t is used to specify the time in the format of [[CC]YY]MMDDhhmm[.SS].
    • touch -d '2022-01-01 12:00:00' -t 202212251200 file.txt

In summary, the touch command is a versatile command that can be used in many ways to create new empty files or update the timestamps of existing files. It can be used with various options like -t, -c, -r, -h, -s, -d, -t which can be used to update timestamps of files, directories, symlinks and also to create files with specific permissions, size, and timestamps. The touch command is an essential command for system administrators, developers, and other users to create new empty files or update the timestamps of existing files, directories, and symlinks.