Windows OS

How to Use Touch Command in Windows

The touch is a command line Linux and Unix-based utility. It is primarily used to create and modify files in the system. A blank file can also be created with it. Many Windows commands are equivalent to Linux touch commands that can be usable in the same manner. We can also use touch on Windows by installing it manually.

This study will explain the use of the touch command in Windows. The following learning outcomes are expected:

  • Install Touch Command on Windows
  • Use Touch Command on Windows
  • Windows Equivalent of Touch Command

Prerequisites: Install NPM

NPM is a famously used package manager that supports web applications and web APIs services. NPM can be automatically installed by installing Node.js on Windows. To install Nodejs and NPM we have provided the link to the article here:

To install NPM, navigate to the “how to install Nodejs on Windows” link.

How to Install Touch Command on Windows?

We can install the touch command on Windows using NPM (Node Package Manager). NPM is a widely used package manager that contains a massive variety of packages. To install the touch command on Windows, utilize the listed steps.

Step 1: Open Command Prompt

Open Windows Command Prompt by typing “Command Prompt” in the Start menu:

Step 2: Install Touch Command

To install the touch, you can utilize the NPM on Windows. For this purpose, take a look at provided command:

> npm install touch-cli -g

 

The above output indicates that we have successfully installed the touch command on Windows.

How to Use Touch Command on Windows?

Touch is Linux based utility and not available on Windows operating system. The touch command is basically used to create and modify files and timestamps of files. To use the touch command on Windows we need to install it first, we have explained the installation procedure of the touch command in the first section. In order to use the touch command on Windows follow the listed steps.

Open Directory to Access File

Open the directory from where you want to access the file using the “cd” command:

> cd C:\Users\anuma\Downloads\MY-Files

 

Using Touch Command to Check File Existence

Use the touch command to check if the file is present or not. For this purpose, check out the mentioned command:

> touch file.txt

 

Using Touch Command to Create a File

Open the desired place first, then use the “touch” command for creating a new file while also providing the file type and name. As we have created the text file “MYfile.txt“:

> touch Myfile.txt

 

To list all files in the directory that is now open, use the “dir” command:

> dir

 

You can see that we have successfully created a new text file using the “touch” command:

Bonus Tip: Touch Equivalent Commands in Windows

As we have mentioned, the touch command is a Linux and Unix-based command line tool. But in Windows, some commands exist that are equivalent to the touch command such as “type” and “copy” commands of CMD are used to create a new file. Moreover, the “Get-Item” cmdlet of PowerShell command used to create, modify, and access timestamps of the files just like touch command:

Let’s use these commands in windows:

Command 1: Using “type” Command to Create Empty File

The “type” command creates empty files in a manner similar to the Linux touch command

> type nul >>"file.txt"

 

The output indicates that we have successfully created an empty file “file.txt

Command 2: Using “copy” Command to Create file

In Windows, the copy command is used to copy or create new files. Due to this particular feature, it is comparable to the Linux touch command:

Command 3: Using PowerShell’s “Get-Item” cmdlet to Access Timestamps of File

Use the “Get-Item” command to access all details about the specified file. To view the timestamps of the file, follow the provided command:

> Get-Item file1.txt | Format-List CreationTime, LastAccessTime, LastWriteTime

 

The provided file’s timestamp can be accessed using the “Get-Item” command, and the “Format-List” command can be used to display a list of the specified properties. The properties on which data is accessible are “CreationTime“, “LastAccessTime“, and “LastWriteTime“:

Command 3: Use “Get-Item” Command to Modify the Timestamp of the File

Let us update the Timestamp of the specified file using provided command:

> (Get-Item "file1.txt").CreationTime=("10 August 2022 11:00:00")

 

We have produced a new timestamp for the property “CreationTime” indicated in the above command.

It will alter “file1.txt” creation time:

Additionally use can alter the “LastWriteTime” and “LastAccessTime” also in the same way:

> (Get-Item "file1.txt").LastWriteTime=("8/26/2022 10:58:54")
> (Get-Item "file1.txt").LastAccessTime=("8/26/2022 10:58:54")

 

Check whether the Timestamp is modified or not by again executing “Get-Item” to Access the timestamp of file:

> Get-Item file1.txt | Format-List CreationTime, LastAccessTime, LastWriteTime

 

Here you can see we have successfully modified the CreationTime of specified file:

We have offered the method to install and use the touch command in Windows. We have also described the touch equivalent command in Windows.

Conclusion

The Touch command is not already pre-installed in Windows. Execute the “npm install touch-cli -g” command in the command prompt to install the touch command. The “type” and “copy” commands in Command Prompt and “Get-Item” in PowerShell are the windows equivalent of the touch command. In this post, we have demonstrated the use of touch commands in windows. Moreover, we have also offered the usage of the windows equivalent of the touch command.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.