How to create a command line file on Linux

It is useful for each user to know how to use the Terminal application (command line) in Linux to create a file. Upon completion 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 on Linux, first open a command prompt. To do this, press “Menu”, then find the “Terminal” application, whose icon 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.

Type ls in the command line that opens, then press 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.

Directory Selection

Find the directory where you want to create the text file. Any directory name given below the ls command (for example, “Desktop”) is a place you can navigate to.

Type to create a file in Linux, the cd command. This 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 would 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 in a specific folder in the directory of your choice, you must specify the / icon after its name and then enter the name of the folder. For example, for a folder named Misc in the Documents directory, you would enter cd Documents / Misc.

After that, press Enter. This will execute your command and you will move the terminal’s destination directory from your home directory to the one you specified.

How to create a file quickly on Linux?

Type cat> filename.txt at the command prompt. You should now replace the “file name” with your preferred text name (for example, “pattern”). For example, when creating a file called kitty, you would 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 will then appear on an empty line in the terminal.

create text file in linux

This method is good for text documents as well as for other data types. If you want to create a batch file on Linux using the command line, text-based applications will not work for you.

Enter the text of the document

You can simply enter letters and numbers, as 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 terminal command line. This will allow you to continue to enter other commands.

create batch file in linux

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

For example, to open a file called textfile, you would enter ls -l textfile.txt. Designations in this code have a lowercase letter L, not an uppercase 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 managed to create the file in the Linux terminal and save it.

Use a text editing program

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

Vim is a clone of vi text editors for Unix. It was written based on a source for the Stevie port editor for 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 is a free open source software licensed.

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

create empty file in linux

Like vi, the Vim interface is not based on menus or icons, but on the commands specified in the text user interface. Its graphical interface, gVim, adds menus and toolbars for commonly used commands, but full functionality is still provided in command line mode.

Emacs is a family of text editors that 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 first-generation Emacs software began in the mid-1970s, and work on improving its latest version of GNU continues to this day.

Emacs has over 10,000 built-in commands, and its interface allows the user to combine these commands into macros to automate the work. In addition, implementations of this software typically contain a dialect of the Lisp programming language, which provides in-depth extension capabilities, allowing users and developers to write new commands and applications for the editor. Separate plugins have been written to manage email, files, contours and RSS feeds.

create file in linux directory

Emacs, along with vi, is one of the two main traditional editors on Unix-like systems. Both applications are among the oldest applications that are still heavily used and continue to evolve.

Using Vim to work with a file

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

For example, for a file named tamins, you would enter 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 keyboard. This will allow you to place the document in the "Paste" mode, in which you can enter text as necessary. You should see the INSERT submenu (it will appear at the bottom of the window) when you press I.

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

After that, press the Esc key. It is usually located in the upper left corner of your computer keyboard. This key will put Vim in Command mode. After clicking, you should see the cursor appear at the bottom of the window.

Type w into the terminal and press Enter. This command will save your document in the form in which it is currently located. After that, enter q at the command prompt and then press enter. This will exit Vim and return you to the main terminal interface. Your text file is now in the specified directory.

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

create link to linux file

Return the file from the terminal window. As with creating it, you must enter the vi filename.txt command 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 using the Emacs application. To do this, enter the emacs filename.txt command in the terminal. You must replace filename with your chosen name for the text file. Press Enter. As long as the name of your text file is not a duplicate of an existing similar document in your current directory, this command will open the new text file in the Emacs editor.

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

How to work with teams?

Check out the Emacs commands. This application comes with many powerful commands that allow you to navigate through a document, search for related or reference information, manipulate text and decrypt your code. These commands are divided into two types: controls and meta-teams.

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

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

A command written as Ca b (or Ma b) will require you to hold Ctrl (or Alt, or Esc) while pressing the first key (for example, then release both buttons and immediately press the second key (for example b)).

Enter the text of the document. You can type it in the same way as 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 your actions will be saved.

After that, press Ctrl + X, and then Ctrl + C. This will allow you to exit the Emacs text editor 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 its name in the terminal.

Reopen the text file. To do this, simply enter emacs filename.txt at 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 to edit it as you like.

Advice

The Vim application is usually available on any version of Linux, and Emacs is a more sophisticated editor that installs into more recent OS distributions. However, for beginners, it may be easier to use this particular program.

You can call up the Emacs help screen 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 create and save the file.

Warnings

If you do not save the document before closing it, a warning about this may not appear, especially in the Vim editor. Therefore, always save the document before closing it.

Source: https://habr.com/ru/post/K19334/


All Articles