Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line - Why cant' I use the 'touch' command?

I'm using Codecademy to learn command-line. When I tried to use the command touch file.txt, it says,

'touch' is not recognized as an internal or external command, operable program or batch file.

I searched on the forum and found out that I can use echo.>file.txt, and it worked.

I would like to know why the touch command doesn't work. Is this because I'm using Windows and not Mac? Can someone explain to me? Thanks!

This is the screenshot on my laptop...... enter image description here

like image 865
Jimmy Wong Avatar asked Aug 26 '16 09:08

Jimmy Wong


People also ask

How do I enable touch command in Windows?

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.

Does touch work in CMD?

Windows does not natively include a touch command. It will iterate over it argument list, and for each element if it exists, update the file timestamp, else, create it.

What is the touch command in CMD?

The touch command's primary function is to modify a timestamp. Commonly, the utility is used for file creation, although this is not its primary function. The terminal program can change the modification and access time for any given file. The touch command creates a file only if the file doesn't already exist.


2 Answers

touch is not a command in the Windows shell, it is in the shells of Unix-like operating systems (Mac, Linux, etc) which provide derivatives of the original sh shell.

For Windows you can either use the workaround you found, or install a terminal emulator like Cygwin. In fact, Windows 10 is currently having a Linux environment being built in, meaning you could use it like Ubuntu, which will provide the right shell for touch.

like image 56
Billy Brown Avatar answered Oct 23 '22 04:10

Billy Brown


I've encountered this today as well, but on my case while trying Node.js. Yup, as they mentioned touch is for Unix. It seems most tutorials out there were created while using Macs. Anyway, I found another workaround, install touch via NPM...just make sure you are connected to the internet to download the package.

 C:\npm install touch-cli -g

I was able to use touch after.

Another issue I've encountered while working with Windows 10 is I cannot change to a different directory. If you encounter it as well while following the tutorials, try this instead of "cd ".

cd /D <directory>
like image 36
Debra Bula Avatar answered Oct 23 '22 03:10

Debra Bula