32

If mkdir creates a new directory, what creates a new file? for example "something.text".

I tried couple of commands mkdir (FileName) -- works fine. But I didn't know how to create a new file inside a directory. I know that I can always go to my project folder then create my new file but I want to know how to do that using terminal to increase productivity.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Moe
  • 365
  • 1
  • 3
  • 7

3 Answers3

57

On Linux there are mutiple options

The typical used one is touch

touch bar.txt

However you may also use echo if you want to create and write to the file right away

The following command tells to create bar.txt and put foo inside of it

echo foo > bar.txt

You may also use >> which appends to an existing file

The following command puts bar at the end of bar.txt, in a other words, bar will display after foo inside bar.txt

echo bar >> bar.txt
CTXz
  • 716
  • 5
  • 5
  • Thanks very much. i tried touch but it did not work. then tried echo and stored a value inside and it did work. – Moe Oct 05 '16 at 11:35
  • Nice, this also works for word documents like `touch bar.docx`. Would have been nice if this worked for rtf files as well in MAC instead of remembering a different command – firstpostcommenter Oct 17 '19 at 08:29
5

You can either use touch:

$ touch something.txt

or > operator to redirect nothing to a file and effectively creating it:

$ > something.txt

or

$ : > something.txt

Note that last 2 commands will truncate file contents if file already exists.

Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
  • thanks for the replay but none has worked , am using the terminal inside of code editor if it help . i appreciate it – Moe Oct 05 '16 at 11:39
0

If you mean on Linux so the command is touch.

Shay
  • 497
  • 1
  • 4
  • 10
  • thanks for the replay, but no am using a code editor in windows and touch did not work – Moe Oct 05 '16 at 11:40
  • Please tag your postings properly. When you tag it with *shell*, people will assume that you are looking for a solution running in a Posix-compliant shell, and from this will conclude that you either are on Unix, Linux, Cygwin or something similar. – user1934428 Oct 05 '16 at 12:46