×

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.


Related Topics

ping Command in Linux/Unix with Examples

The "ping" command in Linux (and other Unix-like operating systems) is used to test the connectivity between two networked devices. The "ping" command sends a small packet of data, called...

4 minutes read.

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...

4 minutes read.

Linux SCP Command

SCP stands for secure copy protocol. It is a command-line application for copying files and directories safely between two places. Command-line utilities are programs that operate on a computer's command...

3 minutes read.

ps Command in Linux/Unix with Examples

The 'ps' command in Linux is used to display information about the running processes on a system. The command stands for "process status" and it allows you to view details...

4 minutes read.

Rename Folder in Linux

Rename Folder in Linux In the Linux system, there is not any single dedicated command for renaming the files and directories. Mostly, the mv command is used extensively for the purpose...

3 minutes read.

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a...

3 minutes read.

Linux Admin Tutorial

In publishing and graphic design, lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document without relying on meaningful content....

1 minute read.

Difference Between Unix and Linux

Unix vs Linux Unix and Linux both have been of great use in changing the world of operating systems. Unix was among the very few operating systems which were programmed for...

4 minutes read.

Linux Distributions

Linux Distributions Linux OS is not like other operating systems. Linux can be modified according to the user’s requirements, and it is very flexible to any change. Linux distributions are often...

4 minutes read.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute read.

ssh Command in Linux/Unix with Examples

The ssh (Secure Shell) command in Linux is used to remotely access and control another computer or device over a network. It provides a secure and encrypted connection between the...

6 minutes read.

telnet Command in Linux/Unix with Examples

The telnet command in Linux is a command-line utility that allows you to connect to a remote host using the Telnet protocol. Telnet is a network protocol that provides a...

3 minutes read.

mv Command in Linux/Unix with Examples

The 'mv' command in Linux is used to move or rename files and directories. The command stands for "move" and it allows you to move files and directories from one...

3 minutes read.

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

4 minutes read.

rename Command in Linux/Unix with Examples

The rename command in Linux is used to rename multiple files at once, using a specified pattern. The command is also known as rename, ren, and mv. This command is...

3 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 minutes read.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

3 minutes read.

Linux pwd

The pwd command is used to print or display the name of the working or current directory. Some shells have their own version of the pwd with several features, in...

1 minute read.

Linux Bash

Linux Bash In this tutorial, we are going to learn what the shell is? And how many various shells we have, we have also covered most of the bash shell features....

4 minutes read.

apt-get Command in Linux with Examples

The apt-get command is a command-line tool that is used to handle packages in Linux. This command handles the processes like retrieving the packages and information from authentic sources for...

5 minutes read.