×

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

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.

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.

df Command in Linux/Unix with Examples

The 'df' command in Linux is used to display the amount of disk space available on the file system. The command reports the amount of disk space used and available...

3 minutes read.

grep Command in Linux/Unix with Examples

The grep command in Linux is a command-line utility that searches for patterns in text files. It stands for "global regular expression print" and it is used to search for...

5 minutes read.

Difference Between Linux and Windows

Linux vs. Windows Here in this tutorial, we are going to discuss several features of Linux and Windows. These features or characteristics help us to come to a conclusion about which...

5 minutes read.

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

4 minutes read.

Linux ls

The primary use case of the ls command is to list the content available in the directory. The default directory is the current directory of the user. The below screenshot...

5 minutes read.

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

1 minute read.

cut Command in Linux/Unix with Examples

The "cut" command in Linux (and other Unix-like operating systems) is used to remove sections from each line of a file or stream of data. The cut command is often...

3 minutes read.

echo Command in Linux/Unix with Examples

The "echo" command in Linux is used to display a line of text on the terminal. It can also be used to print the value of a variable or to...

3 minutes read.

addgroup Command in Linux with Examples

What is the addgroup command? It is a command in Linux to create a new group using specified command lined and the system’s default values. How to add groups Adding a user group:...

4 minutes read.

Linux rmdir command

The default use case of the rmdir command in the Linux system is to delete an empty directory. The below screenshot shows how to delete the empty directory. First, we...

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

cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view...

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

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and...

4 minutes read.

adduser Command in Linux with Examples

What is the adduser command? The adduser command creates a new user, and this command may be used to add apre-existing user to a group that already exists. How to use the...

4 minutes read.

curl Command in Linux/Unix with Examples

The 'curl' command in Linux is a command-line tool that allows you to transfer data from or to a server using various protocols, including HTTP, HTTPS, FTP, and many more....

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