How to create file ubuntu

How to create file ubuntu

How to Create a File in Linux Using Terminal/Command Line

Home » SysAdmin » How to Create a File in Linux Using Terminal/Command Line

Creating a new file in Linux is straightforward, but there are also some surprising and clever techniques.

In this tutorial learn how to to create a file from a Linux terminal.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

Creating New Linux Files from Command Line

Linux is designed to create any file you specify, even if it doesn’t already exist. One smart feature is that you can create a file directly, without needing to open an application first.

Here are a few commands for creating a file directly from the command line.

Create a File with Touch Command

The easiest way to create a new file in Linux is by using the touch command.

In a terminal window, enter the following:

This creates a new empty file named test.txt. You can see it by entering:

The ls command lists the contents of the current directory. Since no other directory was specified, the touch command created the file in the current directory.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

If there’s already a file with the name you chose, the touch command will update the timestamp.

Create a New File With the Redirect Operator

A redirection operator is a name for a character that changes the destination where the results are displayed.

Right angle bracket >

This symbol tells the system to output results into whatever you specify next. The target is usually a filename. You can use this symbol by itself to create a new file:

This creates a new empty file.
Use the ls command to list the contents of the current directory and find the file test2.txt.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

Create File with cat Command

The cat command is short for concatenate. It can be used to output the contents of several files, one file, or even part of a file. If the file doesn’t exist, the Linux cat command will create it.

Note the redirection operator. Typically, the command displays the contents of test2.txt on the screen. The redirection operator > tells the system to place it in the test2.txt file.

Verify that the file was created:

The system should now have test.txt, test2.txt, and test3.txt in the list.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

Create File with echo Command

The echo command will duplicate whatever you specify in the command, and put the copy into a file.

Enter the following:

Verify that the file was created:

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

You should see the test4.txt file added to the list. Use the cat command to display the contents of the new file:

The system should display Random sample text (or whatever you entered with the echo command.)

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

Create File with printf Command

The printf command works like the echo command, and it adds some formatting functionality. To add a single line of text, enter:

To add two lines of text, separate each line with the \n option:

You can use the cat command on either of these files to display their contents.

Note: To use several terminal instances in a single window manager, consider using Linux screen. It enables additional features and an enhanced command line for working with Linux files.

Using Text Editors to Create a Linux File

All Linux distributions have at least one text editor. Some have multiple editors. Each editor has different strengths and features. This will show you three of the most popular.

Vi Text Editor

Vi is the oldest text editor in Linux. It was created alongside the Linux operating system for directly editing text files. Since it’s unlikely you’ll see a Linux distribution without it, it’s a safe editor to know.

To create a file using Vi, enter the following:

Your screen will change. Now you’re in the text editor. Press the letter i to switch to insert mode, then type a few words to try it out.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

Vim Text Editor

You may have noticed that the Vi editor wasn’t very user-friendly. Vim is a newer version, which stands for Vi editor, Modified.

Use vim to create a new text file:

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

This screen will look similar to the Vi editor screen. Press i to insert text, and type a few words. Save file and exit by entering:

(Escape, colon wq, then Enter.)

Nano Text Editor

Nano is a newer and much easier text editor to navigate.

Create a new file by entering the command:

By default, Nano puts you directly into editing mode. It also displays a helpful list of commands at the bottom of the screen.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

Enter some text, then press Ctrl+O to save the changes.

Press Ctrl+X to exit the editor.

Note: Learn all you need about Nano in the Install and Use Nano in Linux article.

Now you have several options to create new files in Linux from the command line. Next, learn how to copy files and directories in Linux to manage your files more efficiently.

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.

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.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

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

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:

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

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:

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

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

To create a file using Vim or Vi type

vi filename.txt

Example:

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 >

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

5. Cat command

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

Example

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 file in Linux from terminal window? [closed]

Want to improve this question? Add details and clarify the problem by editing this post.

What’s the easiest way to create a file in Linux terminal?

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

17 Answers 17

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

Depending on what you want the file to contain:

somecommand > /path/to/file for a file containing the output of some command.

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

Create the file using cat

Now, just type whatever you want in the file:

CTRL-D to save and exit

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

There are several possible solutions:

Create an empty file

Create a file containing a newline and nothing else

This is a valid «text file» because it ends in a newline.

Write text into a file

Of course there are many other methods of writing and creating files, too.

How to create an empty file from command line

How can I create an empty file from the command line?

6 Answers 6

Use the touch command:

Will also create an empty file. If the file does already exist, it will be truncated (emptied). To keep the file contents, use >> for appending as in:

Even if the file exists, the contents will be untouched.

Edit: If you don’t have any content to type, this one is faster:

Note. : is the command here. It is not part of the prompt.

the exact way there is also another way

The difference is file1.ext will be zero bytes and file2.ext would be one byte. You can check this by

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

Using vim editor you can also create an empty file.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

Or you could use printf

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

Most commonly new filename is created with something like this:

Of course, all the above mentioned utilities do not exclusively create files. But they perform the necessary syscall and that allows us to adapt the commands.

Of course, at the level of programming or scripting we may want to create a file as well, especially for efficiency purposes (because calling external commands such as touch from a Perl script or Python will require additional resources).

We can make it shorter with this:

1 Other types of files such as hard/soft links,character or block special devices, directory,named pipes, or sockets require entirely different syscalls.

Different Ways to Create File in Linux

There are mainly six ways of creating files in Linux. All of them have their own purpose and benefits. They are as follows:

1. cat command

It is the most universal command/tool for creating files on Linux systems. We cannot edit a file using the cat command. Major operations that can be done using it are as follows:

To create files and write the data into them

This command creates a new file file1 (in write mode) if it doesn’t exist in the present working directory. If any file with file name file1 exists in the current directory then it is overwritten.

Note: After writing the text into the file, press ctrl+d to save and exit from the writing mode.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

To view the contents of a file

This command simply prints the contents of file1 on the terminal screen.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

To concatenate files — adding the contents of two files into a new file or existing file

This command creates a new file file3 with the contents of file1 & file2 if file3 doesn’t exist in the present working directory. If any file with file name file3 exists in the current directory then it is overwritten with the contents of file1 & file2.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

To add the contents at end of a file

This command creates a new file file2 (in write mode) if it doesn’t exist in the present working directory. If any file with file name file2 exists in the current directory then it is opened (in append mode).

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

To copy the file’s data (the content of one file into another)

This command creates a new file file2 with the contents of file1 if file2 doesn’t exist in the present working directory. If any file with file name file2 exists in the current directory then it is overwritten with the contents of file1.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

To reverse the data of the file (tac)

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

2. touch command

We can create an empty file (or multiple empty files) using this command. But its main purpose is to change or update the time-stamp of a file. Major operations that can be done using it are as follows:

Creating a file

Note: Creating a file and then using the cat command to view the data.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

To change the timestamp of the file

Note: We are using stat file_name to check the timestamp of the file. We can see the access, modify, and change is now updated.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

Change access time only

Access time is the last time when the file was accessed.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

Change modify time only

Modify time is the time when a file was modified.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

3. vi command

Its main function is to edit files. It is commonly used by programmers to edit the textual content of any file on vi text editor. Major operations that can be done using it are as follows:

Note: To save and exit from the vi text editor, press the Escape key and then type :wq and hit enter.

Create a file

This command creates a new file file_1 and opens it on the vi text editor if it doesn’t exist in the present working directory. If a file with the file name file_1 exists in the current directory then this command just opens the file on the vi text editor.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

Open and edit the file

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

4. nano command

It may/may not be found in all distributions of LINUX. We can create as well as edit files.

Note: To exit nano Text Editor press ctrl + x.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

5. gedit command

Linux’s users normally use the command line interface (CLI) for writing or editing the text files. But if we want to edit a text file graphically on Linux machines without learning about the powerful editors like vim and nano then gedit text editor makes it easier for us.

“gedit” stands for GNOME text editor, it’s a standard default text editor found in any system with a GNOME desktop environment including Ubuntu, Fedora, Debian, CentOS, and Red Hat. Using gedit we can create as well as write/edit the text files.

Create a file

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

This command creates a new file file_2 (in write mode) on the gedit text editor if it doesn’t exist in the present working directory. If any file with file name file_2 exists in the current directory then it is opened (in edit mode) on the gedit text editor.

Note: To use the terminal again, press ctrl + c.

Open and edit the file

As we create or edit a file using gedit command, by default the file gets open on the gedit text editor and wait for it to close before it returns you to the terminal prompt. If you want to use the terminal window while the gedit text editor is open, launch gedit with the following command instead.

This command opens gedit text editor as a background task. We get the command line prompt back straight away and carry on using the terminal window even when gedit text editor is running.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

6. mv command

We normally use mv command to move the files or directories from one place to another in Linux systems. But we can also use it to create new files with the contents of some other file on the system.

This command creates a new file file_3 with the contents of file_2 if file_3 doesn’t exist in the present working directory. If any file with file name file_3 exists in the current directory then it is overwritten with the contents of file_2.

How to create file ubuntu. Смотреть фото How to create file ubuntu. Смотреть картинку How to create file ubuntu. Картинка про How to create file ubuntu. Фото How to create file ubuntu

Note: This command copies the content of file_2 to file_3 and deletes file_2.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *