How to Quickly Create a Text File Using the Command Line in Linux

Contents

If you like keyboards, you can achieve a lot of things just using the Linux command line. As an example, there are some easy to use methods to create text files, in case you need to.

Create a text file using the Cat command

Our first method for creating text files uses the cat command. It is useful if you want to immediately add text to your new file.

Just type the following command at the terminal prompt (replacing “sample.txt” with whatever name you want for your file), and then press Enter:

cat > sample.txt

After hitting Enter, will not return to the terminal prompt. Instead, the cursor is placed on the next line and you can start entering text directly into your file. Write your lines of text, pressing Enter after each line. When it's over, presione Ctrl + D to exit the file and return to the prompt.

To verify that your file was created, you can use the ls command to display a list of directories for the file:

ls -l sample.txt

You can also use the cat command to view the contents of your file. Just type the following command at the command prompt and then hit Enter:

cat sample.txt

Create a text file with the touch command

You can also create a text file with the touch command. A difference between using this command and the cat command that we cover in the last section is that, Meanwhile he cat The command enables you to enter text into your file immediately, using the touch the command doesn't. Another big difference is that touch command enables you to create multiple new files with a single command.

the touch The command is useful for quickly creating files that you want to use later..

To create a new file, type the following command at the terminal prompt (replacing “sample.txt” with the file name you want to use), and then press Enter:

touch sample.txt

Note that you are not told that the file was created; just returned to the indicator. You can use the ls command to verify the existence of your new file:

ls -l sample.txt

You can also create several new files at the same time with the touch command. Just add as many additional file names (separated by spaces) as you wish at the end of the command:

toque sample1.txt sample2.txt sample3.txt

One more time, no indication that the file was created is shown, but emitting a simple ls command shows files are there:

And when you're ready to add text to your new files, you can use a text editor like Vi.

Create a text file with the standard redirect symbol (>)

Additionally you can create a text file using the standard redirect symbol, which is generally used to redirect the output of a command to a new file. If you use it without a previous command, the redirect symbol just creates a new file. As the touch command, creating a file this way does not allow you to enter text into the file right away. Unlike touch Despite this, creating a file with the redirect symbol only enables you to create one file at the same time. We include it so that it is complete, and also because if you are only creating a single file, offers the least amount of writing.

To create a new file, type the following command at the terminal prompt (replacing “sample.txt” with the file name you want to use), and then press Enter:

> sample.txt

You are not given any indication that the file was created, but you can use the ls command to verify the existence of your new file:

ls -l sample.txt


These three methods should allow you to quickly create text files in the Linux terminal, whether you need to enter text into them immediately or not.

setTimeout(function(){
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq = n;n.push=n;n.loaded=!0;n.version=’2.0′;
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s) } (window, document,’script’,
‘https://connect.facebook.net/en_US/fbevents.js’);
fbq(‘init’, ‘335401813750447’);
fbq(‘track’, ‘PageView’);
},3000);

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.