how to create a text file in linux from command line

Most often when you want to create a file, you will open your favorite text editor and create a new text file. You can then use the editor to add and modify content to the file and save it to the disk. This works best when you have a desktop environment on your system. If you are working using command line from a terminal or remotely then it might not be an option.

There are text editors that work in a terminal such as vi and nano, which can be used to create a text file. But it may not always be an option. There are several different other commands that can create text files quite easily. These commands can also be used quite easily inside shell scripts.

In the examples below, we will assume that you want to create a text file named mynewfile.txt in the folder ~/temp/.

touch

touch command in Linux is intended to change time stamps on files and directories. But it can also create an empty file if one does not already exist. The command below creates an empty file of zero bytes with the specified name.

bash$ touch ~/temp/mynewfile.txt

If a file or directory by the same name exist at the location, then the time stamp on the file is modified to the current time.

I/O Redirection

Another easy way to create a text file is using the I/O redirection functionality in Linux. The operator ">" is used to redirect streams. To create an empty file, you can redirect to the file name as shown below.

bash$ > ~/temp/mynewfile.txt

As with the touch command, this will create an empty file with no content. The best part about redirection is that it can be used to redirect pretty much any stream, like the standard output and this means the output of other commands as well. Another command to create an empty file is to redirect the output of echo command.

bash$ echo -n > ~/temp/mynewfile.txt

Using echo also allows you to easily create non-empty text files. You can echo any string content to the file. Although you can type in long strings, it is probably appropriate for short text as content or inside a shell script to print out variable values.

bash$ echo "This is my file content..." > ~/temp/mynewfile.txt

cp

Another way of creating a new file is to copy an existing file and then modify the contents of the new file. Granted that it is a pretty lame technique, but it does not necessarily have to use another text file. You can copy from the null device to create an empty file.

bash$ cp /dev/null ~/temp/mynewfile.txt

cat

Another command that can be used is the cat command. This is much more useful when you want to create a text file with larger piece of content than using echo but do not want to use a full-fledged editor to do so. This allows you to type in longer pieces of content into the standard input.

After executing the command, you will be able to type in the content to the standard input. When you are done with the content, you exit by pressing Ctrl-d. This will save the contents to the newly created file.

bash$ cat > ~/temp/mynewfile.txt

dd

Another option you have is the dd command. This is usually a good option to create very large files, but it can also be used to create smaller files with random text or empty files. In order to create an empty file, use the following command

bash$ dd if=/dev/zero of=mynewfile.txt count=0 bs=1

If you like to create a text file with random data, then you can use something like this

bash$ dd if=/dev/urandom of=mynewfile.txt count=1024 bs=1024

This will create a 1 MB file with random data. You can also check out on how to create extremely large text or binary files with random data.