- How to open, create, edit, and view a file in Linux
- GUI text editors
- NEdit
- Geany
- Gedit
- Terminal-based text editors
- emacs
- Redirecting command output into a text file
- Creating an empty file with the touch command
- Redirecting text into a file
- Redirecting to the end of a file
- 6 Commands to Edit File With and Without vi, vim Linux Editor
- File handling using vi and vim editor
- 1. Using vi editor
- 2. Using vim editor
- How to Edit File without vi/vim Editor in Linux?
- 3. Using cat as a text editor
- 4. Using touch command
- 5. using ssh and scp commands
- 6. Using other Programming Language
- How to create and edit text files using command-line from Linux Terminal
- 1. Cat Command
- 2. Touch Command
- 3. Redirect command
- 4. Nano Command
- 6. Vi Command
- Conclusion
How to open, create, edit, and view a file in Linux
One thing GNU/Linux does as well as any other operating system is give you the tools you need to create and edit text files. Ask ten Linux users to name their favorite text editor, and you might get ten different answers. On this page, we cover a few of the many text editors available for Linux.
GUI text editors
This section discusses text editing applications for the Linux windowing system, X Windows, more commonly known as X11 or X.
If you are coming from Microsoft Windows, you are no doubt familiar with the classic Windows text editor, Notepad. Linux offers many similar programs, including NEdit, gedit, and geany. Each of these programs are free software, and they each provide roughly the same functionality. It’s up to you to decide which one feels best and has the best interface for you. All three of these programs support syntax highlighting, which helps with editing source code or documents written in a markup language such as HTML or CSS.
NEdit
NEdit, which is short for the Nirvana Editor, is a straightforward text editor that is very similar to Notepad. It uses a Motif-style interface.
The NEdit homepage is located at https://sourceforge.net/projects/nedit/. If you are on a Debian or Ubuntu system, you can install NEdit with the following command:
For more information, see our NEdit information page.
Geany
Geany is a text editor that is a lot like Notepad++ for Windows. It provides a tabbed interface for working with multiple open files at once and has nifty features like displaying line numbers in the margin. It uses the GTK+ interface toolkit.
The Geany homepage is located at http://www.geany.org/. On Debian and Ubuntu systems, you can install Geany by running the command:
Gedit
Gedit is the default text editor of the GNOME desktop environment. It’s a great, text editor that can be used on about any Linux system.
The Gedit homepage is located at https://wiki.gnome.org/Apps/Gedit. On Debian and Ubuntu systems, Gedit can be installed by running the following command:
Terminal-based text editors
If you are working from the Linux command line interface and you need a text editor, you have many options. Here are some of the most popular:
pico started out as the editor built into the text-based e-mail program pine, and it was eventually packaged as a stand-alone program for editing text files. («pico» is a scientific prefix for very small things.)
The modern version of pine is called alpine, but pico is still called pico. You can find more information about how to use it in our pico command documentation.
On Debian and Ubuntu Linux systems, you can install pico using the command:
nano is the GNU version of pico and is essentially the same program under a different name.
On Debian and Ubuntu Linux systems, nano can be installed with the command:
vim, which stands for «vi improved,» is a text editor used by millions of computing professionals all over the world. Its controls are a little confusing at first, but once you get the hang of them, vim makes executing complex editing tasks fast and easy. For more information, see our in-depth vim guide.
On Debian and Ubuntu Linux systems, vim can be installed using the command:
emacs
emacs is a complex, highly customizable text editor with a built-in interpreter for the Lisp programming language. It is used religiously by some computer programmers, especially those who write computer programs in Lisp dialects such as Scheme. For more information, see our emacs information page.
On Debian and Ubuntu Linux systems, emacs can be installed using the command:
Redirecting command output into a text file
When at the Linux command line, you sometimes want to create or make changes to a text file without actually running a text editor. Here are some commands you might find useful.
Creating an empty file with the touch command
To create an empty file, it’s common to use the command touch. The touch command updates the atime and mtime attributes of a file as if the contents of the file had been changed — without actually changing anything. If you touch a file that doesn’t exist, the system creates the file without putting any data inside.
For instance, the command:
The above command creates a new, empty file called myfile.txt if that file does not already exist.
Redirecting text into a file
Sometimes you need to stick the output of a command into a file. To accomplish this quickly and easily, you can use the > symbol to redirect the output to a file.
For instance, the echo command is used to «echo» text as output. By default, this goes to the standard output — the screen. So the command:
The above command prints that text on your screen and return you to the command prompt. However, you can use > to redirect this output to a file. For instance:
The above command puts the text «Example text» into the file myfile.txt. If myfile.txt does not exist, it is created. If it already exists, its contents will be overwritten, destroying the previous contents and replacing them.
Be careful when redirecting output to a file using >. It will overwrite the previous contents of the file if it already exists. There is no undo for this operation, so make sure you want to completely replace the file’s contents before you run the command.
Here’s an example using another command:
The above command executes ls with the -l option, which gives a detailed list of files in the current directory. The > operator redirects the output to the file directory.txt, instead of printing it to the screen. If directory.txt does not exist, it is created first. If it already exists, its contents will be replaced.
Redirecting to the end of a file
The redirect operator >> is similar to >, but instead of overwriting the file contents, it appends the new data to the end of the file. For instance, the command:
Источник
6 Commands to Edit File With and Without vi, vim Linux Editor
You might be knowing opening and editing file in vi/vim editor in Linux system. Do you wonder- how you can edit file without vi or vim editor?
Many times you have to create a file, write the content on your Unix-line or Linux systems. Sometimes you require to write a program and run it. For all this, you need a text editor to read and write in the file.
So let’s see different commands for creating and editing the files even if you don’t have vi or vim editor, one-by-one…
Table of Contents
File handling using vi and vim editor
The vi and vim are two widely used command line editors on the Unix-like systems (including Linux). And many of the users know about vi and vim editor.
Following are the simple commands you can use to create the file and edit it.
1. Using vi editor
There are two modes of opening file in vi editors. One is command mode and other is insert mode. As it’s not the part of this discussion. And it’s a big topic to discuss in full fledge. We will describe it in the upcoming article.
2. Using vim editor
What if you don’t have vi or vim editor installed on your system?
How to Edit File without vi/vim Editor in Linux?
You can use cat or touch command.
3. Using cat as a text editor
Using cat command to create file
Write the content and once it is done press ctrl+D.
It will save the contents to the file.
Note: Every time when you run this command, it will erase all the contents and write newly entered content. So, be careful if you have any important data in your file.
If you want to edit a file using cat, try the following the command
4. Using touch command
You can also create the file using touch command.
5. using ssh and scp commands
If you are working on the Linux server, you can see there are limited packages installed on the server. There may be a restriction of using resources and so installed packages.
If none of the commands work, follow the steps give below.
- Login into Linux server using ssh command.
- Download the file on the local machine from server.
- Update the file on the local machine.
- Copy the file back to the server. You can use the scp command (Secure Copy Paste command) to copy a file from the local machine to the server.
You can use the simple scp command to copy the file from server to your local system.
6. Using other Programming Language
If you have scripting or any other programming language installed on your system, you can use that to create and edit the file. For example…
It is pretty easy to edit the file using vi and vim editor. But sometimes you may need to edit the file without vi and vim editor. Hope these commands will help you to edit file if there is no vi, vim.
Other vi/vim editor’s commands f0r enhancing your Linux experience:
If you have any doubt or if you have any another alternative to edit file without vi/vim editor, feel free to write in a comment below.
Источник
How to create and edit text files using command-line from Linux Terminal
We shall discuss several ways of creating and editing text files using the command-line from the Linux Terminal.
I n today’s Terminal Tuts session, we shall show you several ways of creating and editing text files that can be done easily and quickly using the command line.
Here are the following commands that can be used to create a text file.
- Cat command
- Touch command
- Standard Redirect Symbol
- Nano command
- Vi command
1. Cat Command
Cat command is mainly used to preview the text file content. However, you can use it to create new files and edit them, too, by using the redirection method. For example, use the following command to create a new file:
After executing the command, a cursor will appear waiting for you to enter any text you need to edit the newly created file.
Cat Command
Once you finished editing your file and you need to exit, press CTRL+D. Now you can see the standard command prompt comes again.
To check if the file was created successfully, you can use the list command as follows:
Check Newly Created File
To ensure that the text you have entered was saved successfully, then you can use the command:
Display The Cat File Content
2. Touch Command
In this method, you will be able to create single or multiple files using the touch command.
To create a single file.
Touch Command
To check if the new file was created successfully.
Touch File
Now, in case you need to create multiple files. Then you can use the following command.
Touch Command
To check if the previous files were created or not.
Check Newly Created Multiple Touch File
3. Redirect command
In this method, we will use the standard redirect command to create a new file. Unlike the touch command, this method will be able to create one single file only at the time.
To create a new file.
Standard Redirect Symbol
To check that the file was created successfully.
Standard Redirect Symbol
4. Nano Command
Using the nano command, you will be able to create a new file and edit it too.
To create a new file.
Nano Command
A nano editor will open like the below screenshot, and you will be able to write and edit your file. Once you have finished editing your file, use the CTRL+O to save your file and use the CTRL+X to exit the nano editor.
Edit The Newly Created Nano File
To ensure that the previous file was created successfully, use the list command.
Nano Command
To display the file content, use the following command.
Display The Nano File Content
6. Vi Command
In this method, we will use the vi command to create a new file and edit it.
To create a new file.
Vi Editor
A vi editor will open then you can start editing your file. Vi is a little bit different than the nano editor, which means for every action you need to do, there is a command that you need to execute first. For example, if you need to enter the vi command mode first you need to press ESC, then one of the following commands:
Vi Editor
To check if the file was created successfully.
Vi Command
To display the file content.
Display The Vi File Content
Conclusion
That ends our guide on creating text files and editing them using command-lines via the Linux Terminal. I hope you enjoyed it.
Источник