Create file with content linux

Содержание
  1. How to Create a File in the Linux Using the Terminal?
  2. 1. Create a file in the Linux/Unix system using the touch command.
  3. Create a file using the touch command in Linux/Unix system.
  4. 2. Create a File in the Linux/Unix system using the cat command.
  5. Syntax
  6. Create a file with some content using the cat command in Linux/Unix system.
  7. Example :
  8. Display contents of the files using the cat command in the Linux system.
  9. 3. Create a file in the Linux/Unix system using a redirection operator.
  10. Example :
  11. 4. Create a file in the Linux/Unix system using the echo command.
  12. Create a new file without contents in the Linux system using the echo command.
  13. Example :
  14. Create a new file with some contents in the Linux system using the echo command.
  15. 5. Create a file in the Linux/Unix system using heredoc.
  16. Create a file with multiple lines of contents using a heredoc delimiter in the Linux system.
  17. Example :
  18. 6. Create a file in the Linux/Unix system using the dd command.
  19. Create a large file in the Linux system using the dd command.
  20. How to Create a file in Ubuntu Linux using command & GUI
  21. Create a file in Ubuntu 20.04 using GUI & right-click
  22. Command-Line to create a new document on Ubuntu Linux
  23. 5 Best Ways to create a new file on Linux
  24. 1. Using the Touch command
  25. 2. Nano Editor
  26. 3. Vim or Vi text editor
  27. 4. Echo command using Redirect operator
  28. 5. Cat command
  29. How to Create a Text File Using the Command Line in Linux
  30. 1) touch command
  31. 2) Standard Redirect Symbol(>)
  32. 3) CAT Command
  33. 4) Using echo / printf
  34. 5) Any command-line text editor(Vim, nano)

How to Create a File in the Linux Using the Terminal?

In this article, we will learn to create a file in the Linux/Unix system using the terminal. In the Linux/Unix system, there are the following ways available to creating files.

  1. Using the touch command
  2. Using the cat command
  3. Using redirection operator
  4. Using the echo command
  5. Using the heredoc
  6. Using the dd command

1. Create a file in the Linux/Unix system using the touch command.

The touch command is used to create file/files without any content and update the access date or modification date of a file or directory in the Linux system. This is the simplest way to create a file in Linux/Unix using the terminal.

Syntax:

The general syntax of the touch command is as follows:

A brief description of options available in the touch command.

Option Description
-a Change the access time of a file
-c, –no-create Check file is available or not, if not available then prevent creating a file
-f ignored
-m Change the modification time of a file
-t STAMP Use specified time instead of the current time
–help Display help and exit
–version Display the version information and exit.

Create a file using the touch command in Linux/Unix system.

In this example, using the touch command we can create a file in the Linux system. Before executing the touch command, we will check that how many files available in our current directory using the below command.

After using the below command a new file created newfile.txt in the current directory.

Example :

To ensure that the file is created or not we will again execute the ls command to list the directory contents.

2. Create a File in the Linux/Unix system using the cat command.

The cat (concatenate) command is used to create, view, concatenate files in the Linux operating system. The touch command is also used to create a file in a Linux system without content whereas the cat creates files with some content. The cat command reads the content of a file and prompts it.

Syntax

The general syntax of the cat command is as follows:

A brief description of options available in the cat command.

Option Description
-A, –show-all Show all content of a file
-b, –number-nonblank Display number of non-empty lines overrides -n
-n, –number Display number of all output lines
-T, –show-tabs Display this help and exit
–help Display this help and exit
–version Display version information and exit

Create a file with some content using the cat command in Linux/Unix system.

To create a file with some content, we use the cat command and file name after that write some content and press CTRL + C when writing is complete as shown below.

Example :

Display contents of the files using the cat command in the Linux system.

The cat command is also used to view the contents of the file. After using the cat command along with the file name contents of the file will be prompt as shown below.

3. Create a file in the Linux/Unix system using a redirection operator.

In the Linux/Unix system a redirection operator is also used to create a file.

Example :

4. Create a file in the Linux/Unix system using the echo command.

The echo command is also used to create a new file in the Linux system.

Create a new file without contents in the Linux system using the echo command.

To create a file without contents, we use the echo command with a redirection operator followed by the file name as shown below.

Example :

Create a new file with some contents in the Linux system using the echo command.

To create a file with some contents, we use the echo command followed by the text, a redirection operator, and the file name as shown below.

5. Create a file in the Linux/Unix system using heredoc.

heredoc stands for here document. The heredoc delimiter is a type of redirection. It allows passing multiple lines of input to a command.

The general syntax of heredoc. Important

Create a file with multiple lines of contents using a heredoc delimiter in the Linux system.

To create a file using heredoc, we use the cat command with heredoc delimiter in the Linux system as shown below.

Example :

6. Create a file in the Linux/Unix system using the dd command.

The dd command is mainly used to converts and copy files. To check more details about the dd command. We can also create a large file using the dd command.

Create a large file in the Linux system using the dd command.

To create a large file, we use the dd command as shown below.

Источник

How to Create a file in Ubuntu Linux using command & GUI

Creating files on Linux is not a cumbersome task, however, those who are new to it or just shifting from Windows to Ubuntu-like systems may face some problems with creating files using command line especially.

Well, even on Linux anybody can create files and folders using a graphical user interface that works just like a charm. Simply right-click and select the New folder or New Document for text files. However, this is not true with every Linux system. For example on Ubuntu right-clicking will give you the only option to create a new folder, thus when it comes to creating a text using GUI you will get stuck.

Therefore, to create a file on Ubuntu Linux, there are two options either using the command terminal or enabling the “new document” option in the right-click context menu of Ubuntu. We will show both the methods.

Create a file in Ubuntu 20.04 using GUI & right-click

Although here we are using Ubuntu 20.04 LTS, the steps given below are applicable for Ubuntu 19.04/18.04 and previous versions.

By default, when we right-click inside anywhere in the Ubuntu Nautilus file manager, it will not give us the “New document” option. Thus, to get this missing option, we need to run a command.

  • Open Ubuntu command terminal.
  • Run command-
  • Now, go to your Linux File Manager and right-click where you want to create a new file
  • Select New Document and then Text Document
  • This will instantly create a new text file on your Linux OS.

For example, I want to create some text document files on my Linux Desktop, then inside the Desktop folder I will right-click, rest of the below picture can describe.

Command-Line to create a new document on Ubuntu Linux

There are a couple of ways to create an empty file without any content or one with some on Linux, here we show all the best possible ways to do that using the command terminal. The below-given commands are applicable for all types of Linux distros.

5 Best Ways to create a new file on Linux

  1. Using Touch command
  2. Nano Text editor
  3. VIM/VI text editor
  4. Echo command
  5. Cat command

Note: Inside any folder that was created with root rights, create a file there. we have to use sudo with every command given below.

1. Using the Touch command

As we already have seen in the GUI method, how we have created an empty document using the Touch command in the terminal. Thus, in the same way anywhere in any directory, we can use the touch tool to create an empty file.

The command syntax is:

touch file-name

Now, here we can define the extension to let the system what kind of file we want to create such as a txt, docx.

For example, I want to create a Text file or Docx, thus the command will be:

or

Here is the example screenshot, where we have created a file on the Ubuntu Desktop using command terminal and touch command:

2. Nano Editor

Nano is the popular and easy-to-use command-line visual text editor that not allows users to edit any existing file on the system using the terminal but also lets us create a new file to add some content and save it anywhere on Linux.

Some Linux may not have nano editor by default, thus to install it run:

For Ubuntu/Debian – sudo apt install nano

For REHL based Linux- sudo yum install nano

Now, how to use a nano text editor to create a new file? Well, it is quite simple, on command terminal type- nano along with the filename.

for example, I want to create a text file then the command will be:

Add some lines or just press the Enter key. Then save the file by pressing Ctrl+X , type Y , and hit the Enter key.

3. Vim or Vi text editor

Vi is a command-line text editor that is available to use on every Linux including Ubuntu out of the box, thus no need to install anything, just use its command to create or edit an existing file on your system.

Now, what is the difference between VIM and VI text editors? Vi is the early implementation of a visual text editor and that is why it is available on all Linux systems, whereas VIM that stands for Vi IMproved, is the enhanced version for Vi text editor with many additions. Thus, you may not find VIM by default on all Linux distros and need to install it manually.

To install VIM on Ubuntu/Debian type- sudo apt install vim

For RHEL based– sudo yum install vim

Nevertheless, the syntax to create a new file on Linux using both the editors will be the same. However, they are slightly complex to use as compared to nano .

To create a file using Vim or Vi type

vi filename.txt

Example:

To add the content, press the “INSERT” key on your keyboard. Once you are done and want to save the created file’s content press the Esc key and then type :wq , and finally hit the Enter key to exit.

4. Echo command using Redirect operator

The echo command is another one available on all Linux distros to not only create a new file but also add the text in the same. I mean you can add the text into your file right the moment you are creating it.

echo > file-name

Let’s see some examples using the Echo command:

So, if I want to create a new file on Linux with echo and at the same time I also want to add some text into it, let’s say “My name is Linux” thus the command will be like this:

echo «My name is linux» > h2s.txt

In the above, the command h2s.txt file will be created having the content “My name is Linux“.

If you want to create a blank file with echo then don’t type anything before Redirect > operator i.e >

5. Cat command

Cat is also easy to use Linux command that can create a file using a command-line interface.

Example

Add the text you want in your file and hit the Enter button. To save the file, press- Ctrl+D .

Note: For empty files don’t add any text and use only the shortcut to save it.

If you want to read the text of any existing file without opening it, then cat can be used.

For example, let’s create a file demo.txt and then add the text “My name is Linux” into it. Once done, save it and again call the same using the cat to read its text directly inside the terminal but without actually opening the file.

Add text and hit the Enter key

Save the file – Ctrl + d

If you want to read the text of the file using the cat command then type:

Источник

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

There are a couple of quick ways to create a text file from the Linux Command Line or Terminal. Some of them have been explained in the following article.

1) touch command

This is the most standard command to quickly create an empty text file. The command is quite simple to type and makes it quite easier to create several text files at once. The commands are as follows:

As simple as that, just type the word touch followed by the name of the file you like to give it, and Voila! you have created an empty text file inside of a terminal. You can add the file names of the file you would like to create at once with space in between each filename. The command below creates three empty files at once using the touch command and you can create as many files as you like.

touch command for creating multiple files

2) Standard Redirect Symbol(>)

It is also quite easy to understand the command to create a text file in the terminal with the minimum effort. This works really very well for creating a single text file quickly, but for creating several text files at once it becomes a bit tedious. The command is simply using the standard redirect symbol (>) spacebar followed by the file name.

If you want to create several text files at once, then you can add the redirect symbol after the previous filename and chain the command repeatedly to create multiple empty files.

The above command creates three empty text files. The redirect symbol is quite time-saving if you just want to create a single text file. It gets quite longer than the touch command to create multiple empty text files.

Using the redirect symbol for creating files.

3) CAT Command

Now, this method is also quite simple and easy to use. Simply type in CAT with two redirect symbols (>>) and the file name( It is not mandatory to use >> symbols, a user can also use > symbol, but if the user types a pre-existing file by mistake, the existing content in the text file will be overwritten using a single > symbol). This method is a kind of combination of touch and the redirect symbol commands. This method is a bit quirky, so you only prefer using the above two commands if you want to create an empty never edited file. If you want to create and type in the text file straight away, by far this is quite a brilliant method. This will save you time to open up an editor and the command is also quite easy.

The below command creates an empty yet edited file as it prompts the user to create a text file and type in the file at the same time. So, if you do not want to edit the file, simply press CTRL+C and it will simply exit and create an empty file.

But, if you would like to add some text to the file, you can type in after this, like this:

To stop editing and saving in the file, simply type CTRL+C, it will create, save and exit the file. So, this method is quite a time-saving method if you want to edit the text file very quickly. The following command will append the text to the pre-existing file. On the other hand, if you use a single redirect symbol(>) it will overwrite the content of the file, so you only prefer using double redirect symbols for safety reasons.

Using cat command to create the file.

Using cat command to create and write a file.

4) Using echo / printf

This is also similar to cat command, but it is very flexible comparatively. The following command is usually used for printing text on the terminal, but we can also use it to write to a file or make an empty file. The echo command is used along with the double redirect symbols (single > will also work) followed by the filename.

If you want to create multiple files at a time, you can chain up the command as in previous methods.

We can also add functionality to the echo command to quickly create and write to the text file just like cat command.

echo -e ‘This will be the text in the file \n this is the new line’ >> file.txt

The above command can be highly customizable as it uses the properties of the echo command to make it quite versatile to write the text in the file, but using a new line character every time can be annoying as well.

using the echo command to create files.

Using echo command to create and write to a file.

Similar to the echo command, we have the printf command as well. The print command does the same thing as the echo command but in a C style rather than shell-style editing.

printf “This is some text here \n The second line \n The third line” >> file.txt

The print command does some pretty C-like things, such as the newline character and the variable names can be used as well, but that is not for a simple text file. But still, the printf command can be useful in a lot of cases to edit files on the go.

Using printf command to create files.

Using printf to create and write to files.

5) Any command-line text editor(Vim, nano)

This is the most time-consuming method and not the fastest, yet the method can be useful for Linux beginners. If you want to heavily edit a text file, you can use command-line text-editors such as Vim, nano, and there are other options as well. But most people use nano as it is simple to use and quick to go. Vim can also be used but most beginners find it difficult to use, so we’ll stick with nano for this example.

We are now in the nano editor(or vim). You can type in the stuff you require and simply type CTRL+S to save and CTRL+X to exit. In Vim it is a bit different. We won’t make a vim guide here, so you can check out the ‘Nano text editor in Linux‘ or ‘Getting started with vim‘ article from geeks for geeks.

Using Nano to create and write files.

So that wraps up the methods for quickly creating a text file or writing to the file. Each method can be used differently depending on the situation and the case used. Not every method will be the fastest, yet these were some of the fastest ways to create a Text File Using the Command Line in Linux.

Источник

Читайте также:  Name server на freebsd
Поделиться с друзьями
КомпСовет
Adblock
detector