How to Create a File in Linux from Terminal Window

How to create a file in Linux from terminal window?

Depending on what you want the file to contain:

  • touch /path/to/file for an empty file
  • somecommand > /path/to/file for a file containing the output of some command.

      eg: grep --help > randomtext.txt
    echo "This is some text" > randomtext.txt
  • nano /path/to/file or vi /path/to/file (or any other editor emacs,gedit etc)

    It either opens the existing one for editing or creates & opens the empty file to enter, if it doesn't exist

What command makes a new file in terminal?

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

What is the quickest / easiest way to create a new file in the command line?

Very easy is to use touch :

touch myfile

How to create a file in the terminal of VSC?

Ctrl + ` to open the terminal in Visual Studio Code.

echo > "File.txt" to create a new .txt file in the current directory.

How can I create an empty file at the command line in Windows?

Without redirection, Luc Vu or Erik Konstantopoulos point out to:

copy NUL EMptyFile.txt
copy /b NUL EmptyFile.txt

"How to create empty text file from a batch file?" (2008) also points to:

type NUL > EmptyFile.txt
# also
echo. 2>EmptyFile.txt
copy nul file.txt > nul # also in qid's answer below
REM. > empty.file
fsutil file createnew file.cmd 0 # to create a file on a mapped drive

Nomad mentions an original one:

C:\Users\VonC\prog\tests>aaaa > empty_file
'aaaa' is not recognized as an internal or external command, operable program or batch file.

C:\Users\VonC\prog\tests>dir

Folder C:\Users\VonC\prog\tests

27/11/2013 10:40 <REP> .
27/11/2013 10:40 <REP> ..
27/11/2013 10:40 0 empty_file

In the same spirit, Samuel suggests in the comments:

the shortest one I use is basically the one by Nomad:

.>out.txt

It does give an error:

'.' is not recognized as an internal or external command

But this error is on stderr. And > only redirects stdout, where nothing have been produced.

Hence the creation of an empty file.

The error message can be disregarded here. Or, as in Rain's answer, redirected to NUL:

.>out.txt 2>NUL

(Original answer, November 2009)

echo.>filename

(echo "" would actually put "" in the file! And echo without the '.' would put "Command ECHO activated" in the file...)

Note: the resulting file is not empty but includes a return line sequence: 2 bytes.


This discussion points to a true batch solution for a real empty file:

 <nul (set/p z=) >filename

dir filename
11/09/2009 19:45 0 filename
1 file(s) 0 bytes

The "<nul" pipes a nul response to the set/p command, which will cause the
variable used to remain unchanged. As usual with set/p, the string to the
right of the equal sign is displayed as a prompt with no CRLF.

Since here the "string to the right of the equal sign" is empty... the result is an empty file.


The difference with cd. > filename (which is mentioned in Patrick Cuff's answer and does also produce a 0-byte-length file) is that this "bit of redirection" (the <nul... trick) can be used to echo lines without any CR:

<nul (set/p z=hello) >out.txt
<nul (set/p z= world!) >>out.txt
dir out.txt

The dir command should indicate the file size as 11 bytes: "helloworld!".

Cannot create file on ubuntu bash shell in my window machine

You may not have access to the /mnt/c/Users/Public/Desktop directory as default

Run:

ls -ld /c/mnt/Users/Public/Desktop

to see whether you have write permissions as default. If you don't run:

sudo chmod +w /mnt/c/Users/Public/Desktop

This will then allow you write permissions to the directory and allow you to create files.

NOTE - Please ensure that the initial bash executable is run as administrator at Windows level



Related Topics



Leave a reply



Submit