How to create a file in Linux on the command line

It is useful for each user to know howuse the Terminal application (command line) in Linux to create the file. At the end of this process, you can use one of the built-in text editors of the OS to make changes to the new file.

linux create file

Where to begin?

To create a file in Linux, first of allopen the command line. To do this, press "Menu", then find the application "Terminal", the icon of which resembles a black square with a white symbol on it. Click on it. As a rule, it is located in the panel to the left of the menu window. You can also click the search button at the top of this window, and then enter the "Terminal" query to search for it.

Enter ls in the opened command line, thenpress Enter. The terminal opens as your home directory, and the above command displays each folder in your current directory. To create a text file in Linux in one of them, you will need to change the current directory.

Catalog selection

Find the directory where you want to create the text file. Any directory name specified below for the ls command (for example, "Desktop") is the place to which you can navigate.

Type in the command cd to create the file on Linux. This action will take you to the name of your preferred directory. This command changes the focus of the command line from the current directory to the one you name. For example, you must type cd Desktop to switch the location of terminal commands to the Desktop directory.

create file in linux command

If you want to create a text file infolder in the directory you select, you must specify the / after its name, and then enter the folder name. For example, for a folder named Misc in the Documents directory, you must enter cd Documents / Misc.

Then press Enter. This will execute your command, and you move the target terminal directory from your home directory to the one you specified.

How to create a file in Linux quickly?

Type cat> filename.txt in the command line. Now you must replace the "file name" with the preferred text name (for example, "sample"). For example, when creating a file called kitty, you must enter cat> kitty.txt.

Press the Enter key. This will allow you to create a new text file with the specified name in your current directory. The cursor then appears on an empty line in the terminal.

create a text file in linux

This method is good for both text documents,and for other types of data. If you want to create a batch file in Linux using the command line, applications for working with text documents will not work for you.

Enter the text of the document

You can simply enter letters and numbers likeThis is usually done for any other text document. To save the current line of text and go to the next, press Enter. If you have a text file directory open, you can also just double-click on it when it opens.

Press Ctrl + Z. This key command will save your work and return you to the command line of the terminal. This will allow you to continue entering other commands.

create a batch file in linux

Enter ls -l filename.txt into the terminal. Replace the default file name with the name you selected. This command will find your document, ensuring its safe creation in the directory.

For example, to open a file named textfile,you must enter ls -l textfile.txt. The notation in this code has a lowercase letter L, not a capital letter i, so be careful. After that, press the enter key. After that, you should see how the time, date and name of your file are displayed in the next line. This indicates exactly that you have succeeded in creating a file in the Linux terminal and saving it.

Use the text editing program

You can quickly create an empty file in Linux orAlso use Vim or Emacs to create and edit advanced. Now that you are in the directory where you want to place the text document, it's time to start creating it.

The Vim application is a clone of textvi editors for Unix. It was written based on the source for the Stevie editor's port for the Amiga and was first released publicly in 1991. The Vim editor is intended for use both from the command line interface and as a separate application in the graphical interface. This free software is open source, released under a license.

Despite the fact that the program was originallyreleased for Amiga, Vim was later developed as a cross-platform editor that supports many other distributions. To date, it is one of the most popular applications among Linux users.

create an empty file in linux

Like vi, the Vim interface is not based on a menu oricons, but on the commands specified in the text-based user interface. Its graphical interface, gVim, adds menus and toolbars to commonly used commands, but full functionality is still in command line mode.

Emacs is a family of text editors,which are characterized by their extensibility. The most advanced version of the application, GNU Emacs, is described as an extensible, customizable, self-documenting editor.

The development of Emacs first-generation software began in the mid-1970s, and work on improving its latest version of the GNU continues to this day.

Emacs has more than 10,000 built-in commands, and itsThe interface allows the user to combine these commands into macros to automate the work. In addition, the implementation of this software usually contains a dialect of the Lisp programming language, which provides deep expansion capabilities, allowing users and developers to write new commands and applications for the editor. Separate plugins were written to manage e-mail, files, paths and RSS-channels.

create a file in the linux directory

Emacs, along with vi, is one of two majortraditional editors in Unix-like systems. Both applications are one of the oldest application programs, which are still actively used and continue to develop.

Using Vim to work with a file

Type vi filename.txt on the command line. Part vi in ​​this command selects the Vim text editor as the program used. For the rest, replace filename with the name you want to assign to the new file.

For example, for a file named tamins, you mustenter vi tamins.txt. If there is a document with the same name in your current directory, this command will open it. If this name is unique, you can create a file from scratch in Linux using the above program.

Press the Enter key. This will allow you to create your new file and open it in the Vim editor. You should now see an empty terminal window with a tilde in each line, as well as the name of your text file at the bottom of the window.

Press the i key on your computer's keyboard. This allows you to place the document in the "Paste" mode, where you can enter text as needed. You should see the INSERT submenu (it will appear at the bottom of the window) when you press the I key.

Enter the text of the document. You can just type characters, as usual, for any other text document. To save the current line of text and go to the next, press Enter.

Then press the Esc key. Usually it is in the upper left corner of the keyboard of your computer. This key will place Vim in Command mode. After clicking, you should see that the cursor appears at the bottom of the window.

Enter w to the terminal and press Enter. This command will save your document as it is currently. Then, type q at the command prompt, and then press the enter key. This will lead to the exit from Vim and the return to the main interface of the terminal. Your text file is now in the specified directory.

You can check a new text file by typingls on the command line and pressing ↵ Enter. After that you can search by file name in the opened interface. You can also enter the wq command to save and exit one command. After that, you can be sure that you can create a file in the Linux directory.

create a link to the linux file

Return the file from the terminal window. As with its creation, you must type vi filename.txt to open it. This time, when you open the file, you will see all the saved changes that you made.

Using Emacs

You can also create a file on Linux usingEmacs application. To do this, enter the command emacs filename.txt in the terminal. You must replace filename with your chosen name for the text file. Press Enter. While the name of your text file is not a duplicate of an existing similar document in your current directory, this command will open a new text file in the Emacs editor.

For example, for a file named newfile, you need to enter emacs newfile.txt. If you entered an existing name for a text document, this command will open the corresponding file instead.

How to work with teams?

Familiarize yourself with the Emacs commands. This application comes with a lot of powerful commands that allow you to navigate through the document, search for related or reference information, manipulate the text and decipher your code. These commands are divided into two types: management and meta-commands.

The first group is written as C- <letter>. For example, to execute the Control command, you must hold down the Ctrl key and simultaneously press the specified letter key (for example, Ctrl and A).

The Meta (or Escape) commands are written like this: M- <letter> or ESC <letter>. "M" refers to the Alt or Esc key on the keyboard (depends on the model and the design of the computer).

A command written as C-a b (or M-a b),requires that you hold down Ctrl (or Alt or Esc) when you press the first key (for example, and then release both buttons and immediately press the second key (for example, b)).

Enter the text of the document. You can type it just like it does for any other text document. To save the current line of text and go to the next, press the enter key.

Press Ctrl + X, then S. All the actions you performed will be saved.

After that, press Ctrl + X, andthen Ctrl + C. This will allow you to exit the text editor of Emacs and return you to your directory in Terminal. Now your text document will be saved under your chosen name in this directory. To create a link to a Linux file, you will need to enter a command with a name in the terminal.

Reopen the text file. To do this, simply type emacs filename.txt on the command line. While you are in the same directory, the above command will open your text file in Emacs, after which you can continue editing it at your discretion.

Advice

The Vim application is generally available in anyversion of Linux, and Emacs is a more complex editor, which is installed in the more recent OS distributions. However, beginners may find it easier to use this program.

You can call the help screen of Emacs by pressing Ctrl+ H, then releasing both buttons and pressing T. The Help menu will display additional key commands and other editor functions that can help you when creating and writing a file.

Related news

How to create a file in Linux on the command line How to create a file in Linux on the command line How to create a file in Linux on the command line How to create a file in Linux on the command line How to create a file in Linux on the command line How to create a file in Linux on the command line