- 4 Ways to Create a Text File in Linux Terminal
- Create file in Linux command line
- 1. Create an empty file using touch command
- 2. Create files using cat command
- 3. Create new file using echo command
- 4. Create a new file using a text editor like Nano or Vim
- How to create a file in Linux using the bash shell terminal
- How to create a file in Linux from terminal window?
- How to create a text file using the cat command
- How to create an empty text file using the touch command
- Creating a file in Linux using the echo or printf
- Appending data
- How to create a file in Linux using joe text editor
- How to create a text file in Linux using vi / vim text editor
- Conclusion
- 7 Ways to Create a File in Linux Terminal
- 1) Create a file with touch command
- 2) Create a file with cat command
- 3) Create a file with echo command
- 4) Create a file with printf command
- 5) Create a file with nano text editor
- 6) Create a file with vi text editor
- 7) Create a file with vim text editor
- Conclusion
- 3 Ways to Create a Text File Quickly Through the Linux Terminal
- The cat Command
- The touch command
- Create multiple files at once through the touch command
- Using the Standard Redirect Symbol
- Karim Buzdar
4 Ways to Create a Text File in Linux Terminal
In this Linux beginner series, you’ll learn various methods to create a text file in Linux terminal.
If you have used the desktop oriented operating system such as Windows, creating file is a piece of cake. You right click in the file explorer and you would find the option of creating new file.
Things won’t look the same when you are in a command line environment. There is no right click option here. So how do you create a file in Linux then? Let me show you that.
Create file in Linux command line
There are various ways of creating a new file in Linux terminal. I’ll show you the commands one by one. I am using Ubuntu here but creating files in Ubuntu terminal is the same as any other Linux distribution.
1. Create an empty file using touch command
One of the biggest usages of the touch command in Linux is to create a new empty file. The syntax is super simple.
If the file doesn’t exist already, it will create a new empty file. If a file with the same name exists already, it will update the timestamps of the file.
2. Create files using cat command
Another popular way of creating new file is by using the cat command in Linux. The cat command is mostly used for viewing the content of a file but you can use it to create new file as well.
You can write some new text at this time if you want but that’s not necessary. To save and exit, use Ctrl+D terminal shortcut.
If the file with that name already exists and you write new text in it using the cat command, the new lines will be appended at the end of the file.
3. Create new file using echo command
The main use of the echo command is to simply repeat (echo) what you type on the screen. But if you use the redirection with echo, you can create a new file.
To create a new empty file using echo you can use something like this:
The newly created filename.txt file will have the following text: This is a sample text. You can view the file in Linux using cat or other viewing commands.
You are not obliged to put a sample text with echo. You can create an (almost) empty file using the echo command like this:
This will create a new file with just one empty line. You can check the number of lines with wc command.
4. Create a new file using a text editor like Nano or Vim
The last method in this series is the use of a text editor. A terminal-based text editor such as Emacs, Vim or Nano can surely be used for creating a new file in Linux.
Before you use these text editors, you should make sure that you know the basics such as saving an existing from the editor. Unlike the GUI tools, using Ctrl+S in the terminal won’t save the file. It could, in fact, send your terminal into a seemingly frozen state from which you recover using Ctrl+Q.
Let’s say you are going to use Vim editor. Make sure that you are aware of the basic vim commands, and then open a new file with it like this:
What’s your favorite command?
So, I just shared 4 different ways of creating a file in Linux. Personally, I prefer using touch for creating empty file and Vim if I have to edit the file. On a related note, you may want to learn about the file command in Linux that is helpful in determining the actual type of the file.
Which command do you prefer here? Please share your views in the comment section below.
Источник
How to create a file in Linux using the bash shell terminal
I am a new Linux system user. How do I create a file in Linux using the bash shell terminal? What is the fastest and easiest way to create a file in a Linux terminal?
Introduction – A file is nothing but a container in a Linux based system for storing information. For example, music stored in a file named foo.mp4. Similarly, my cat’s picture stored in kitten.jpg and so on. This page shows various methods to create a file in Linux using the terminal window.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Bash shell/terminal on Linux |
Est. reading time | 3 minutes |
How to create a file in Linux from terminal window?
- Create an empty text file named foo.txt:
touch foo.bar
OR
> foo.bar - Make a text file on Linux:
cat > filename.txt - Add data and press CTRL + D to save the filename.txt when using cat on Linux
- Run shell command:
echo ‘This is a test’ > data.txt - Append text to existing file in Linux:
echo ‘yet another line’ >> data.txt
Let us see some examples for creating a text files on Linux operating systems.
How to create a text file using the cat command
To create a text file named sales.txt, type the following command and then press [Enter] key:
cat > sales.txt
Now type your lines of text. For example:
When done and you need to save and exit, press Ctrl + D to return to the bash shell prompt. To view file use cat or more command/less command:
cat sales.txt
more sales.txt
How to create a file in Linux from terminal window
How to create an empty text file using the touch command
Simply type any one of the following command:
> data.txt
OR
touch test.txt
Verify that empty files are created with the help of ls command:
ls -l data.txt test.txt
Creating a file in Linux using the echo or printf
Let us create a file called quote1.txt using echo command, enter:
echo «While I thought that I was learning how to live, I have been learning how to die.» > quote1.txt
OR use the printf command printf ‘Study nature, love nature, stay close to nature. It will never fail you.\n’ > quote2.txt
Appending data
Use the the >> instead of > to append data to existing file and to avoid overwriting files. The syntax is:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
How to create a file in Linux using joe text editor
JOE is text editor. To create a file called delta.txt, type:
joe -help delta.txt
You will see help menu on screen. Next type something. To save the file and leave joe, by typing ^KX (press CTRL+K+X).
How to create a text file in Linux using vi / vim text editor
The vi / vim is another text editor. To create a file called purchase.txt, type:
vi purchase.txt
OR
vim purchase.txt
Press i to insert new text. To save the file and leave vi, type ESC + : + x (press ESC key, type : followed by x and [enter] key).
Conclusion
You learned various methods that allow you to create text files in a Linux or Unix/macOS terminal window quickly.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
7 Ways to Create a File in Linux Terminal
In this tutorial, I will show you how to create a file from a Linux terminal. There are many text editors like (vim, nano, vi) and many commands like (cat, echo, printf, touch) to create a file in the Linux operating system via command line. Here will explain the following linux tools.
1) Create a file with touch command
We will use touch command with any extension to create file, this command will create an empty file touch.txt in your current directory as an example below.
To see the file type command below.
2) Create a file with cat command
We will use cat command to create file, this command will create an empty file cat.txt in your current directory as an example below, but you must add text in the file.
Add the text below.
To save the file hit Ctrl + d , and to see the file type command below.
To open the file, we will use cat command to open it.
3) Create a file with echo command
We will use echo command to create file, this command will create a file echo.txt in your current directory as an example below, but you should add text in the line command.
To see the file,type command below.
To open the file, we will use cat command to open it.
4) Create a file with printf command
We will use printf command to create file, this command will create a file printf.txt in your current directory as an example below, but you should add text in the line command.
To see the file type command below.
To open the file, we will use cat command to open it.
5) Create a file with nano text editor
To create a file using nano text editor, first install it, after that type command below and the text editor will be opened to adding text.
Add the text below.
To save the file type Ctrl + x and type y , to see the file type command below.
To open the file, We will use nano command to open it.
6) Create a file with vi text editor
To create a file using vi text editor, type command below and the text editor will open the file, but you can’t add any text before converting it to insert mode by typing i character.
Add the text below.
To save the file and exit hit Esc after that :wq , To see the file type command below.
To open the file, we will use vi command to open it.
7) Create a file with vim text editor
To create a file using vim text editor, type command below and the text editor will open the file, but you can’t add any text before converting it to insert mode by typing i character.
Add the text below.
To save the file and exit hit Esc after that :wq , to see the file type command below.
To open the file, we will use vim command to open it.
Conclusion
In this tutorial, we learned the different ways to create a file from Linux terminal. Hope you enjoyed reading and please leave your comments in the below comment section.
Источник
3 Ways to Create a Text File Quickly Through the Linux Terminal
Being a Terminal-savvy person, you may always be looking for ways to ditch the mouse. Creating a text file is one task for which you can depend only on your keyboard on an Ubuntu system. Three commands from the Linux command line are at your service for creating text files. These include:
- The cat command
- The touch command
- The standard redirect symbol
Let us explore these commands in this article to create some sample text files. The commands and procedures mentioned in this article have been run on an Ubuntu 20.04 LTS system. Since we will be creating the text files using Ubuntu command line-the Terminal; you can open it either through the system Dash or the Ctrl+Alt+T shortcut.
The cat Command
The cat command is very helpful when dealing with text files in Linux. It helps you in achieving three basic purposes:
- Creating a text file
- Printing contents of a text file in your Terminal
- Printing contents of a text file to another text file
Here, we will explore the first use of the cat command; creating a text file through the command line.
Enter the following command in your Terminal:
After entering this command, the next prompt will not appear; rather the cursor will display for you to enter the text for the file you just created.
In this example, I have created a text file through the following command and then entered some sample text:
Once you have entered all the text, hit enter to move to the next line and then use the Ctrl+D control to tell the system that you are done with entering the text. The usual command prompt will then appear for you to move on with further operations.
You can then use the ls command to see that your newly created text file will be there in the system.
Advertisement
Through the cat command, you can then view the contents of the file as follows:
You can see that the cat command shows the text I wrote while creating my sample file:
The touch command
Another way of quickly creating a text file through the Terminal is by using the touch command. The touch command, however, does not let you enter text in the file at the time of creation. After creating the file, you can enter the text through your favorite text editor. You might prefer the touch command over the cat command in one scenario; when you want to create multiple files at once through one command.
Let us first see how to create a single file first through the Linux touch command:
Use the ls command to see if the recently created file now exists on your system.
Create multiple files at once through the touch command
As mentioned above, the touch command takes the lead on the cat command on the basis that you can create multiple files simultaneously through the former. Use the following syntax to do so:
$ touch filename1.txt filename2.txt filename2.txt ….
For example, in the following command, I have created three files at once through the touch command:
I also checked the presence of the three files through the ls command in the above example.
If you want to edit any of the files you created through the touch command, you can use any of your favorite text editors. Here I am using the Nano editor to enter text to one of the files I created. I used the following command to open the file through the Nano editor.
I then entered the text and saved it by pressing Ctrl+X and then by hitting Enter.
The touch command can also be used to change the access and modification time of a file.
Change the access time of a file:
Set the modification time of a file:
touch -m samplefile.txt
You can view the access and modification time of files with the stat command:
Using the Standard Redirect Symbol
The standard redirect symbol is usually used when redirecting the output of a command to a file. However, it can also be used to create a single text file. The only difference is that while creating a new file we do not specify any command before the redirect symbol.
The difference between using the standard redirect symbol for creating a text file is that, unlike the cat command, you can not enter text this way. Also, unlike the touch command, you can only create one file at a time through the redirect symbol.
Use the following syntax in order to create a text file through this symbol:
to create file» width=319 height=45 srcset=»https://vitux.com/wp-content/uploads/2018/10/word-image-129.png 319w, https://vitux.com/wp-content/uploads/2018/10/word-image-129-300×42.png 300w» sizes=»(max-width: 319px) 100vw, 319px»>
You can then use the ls command to see if the newly created text file now exists on your system.
You can enter text in the file through your favorite text editor. In the following example, I am using the Vim editor to edit the file through the following command:
When you save and exit the file, your text file will have those contents saved.
Through this article, we have learned three basic ways to create text files quickly through the Linux command line. You can now avoid the mouse and use only the keyboard in order to perform the simple task of creating a text file in Ubuntu.
Karim Buzdar
About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn
Источник