- How to Create and Edit Text File in Linux by Using Terminal
- Using Nano
- Using Vi or Vim
- Community Q&A
- 3 Ways to Create a Text File Quickly Through the Linux Terminal
- The cat Command
- The touch command
- Create multiple files at once through the touch command
- Using the Standard Redirect Symbol
- Search
- About This Site
- Latest Tutorials
How to Create and Edit Text File in Linux by Using Terminal
This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions.
The wikiHow Tech Team also followed the article’s instructions and verified that they work.
This article has been viewed 1,676,376 times.
This wikiHow teaches you how to create and edit a text file in two popular Linux text editors. Nearly all Linux systems come preinstalled with Nano, a straight-forward, easy-to-use text editor. If you don’t like (or don’t have) Nano, you can also use Vi (or Vim, depending on the system) to edit text files. Vi and Vim are a bit more challenging to use, as there are a multitude of commands and two different modes.
Using Nano
- You can also double-click the Terminal icon in your list of Applications, or by clicking your Dash menu (if you’re using GNOME) and searching for terminal .
- Nano is a very easy-to-use text editor that comes preinstalled on all Ubuntu-based Linux distributions. [1] X Research source If you don’t have Nano, you can get it by running sudo apt install nano (Ubuntu and Debian) or sudo yum install nano (CentOS and Fedora).
- If you’ve ever used the Pico text editor, you’ll find that Nano is essentially identical. And unlike Vi and Vim, you don’t have to switch between command and input modes while using it.
- To view all folders in the current directory (your home directory), type ls and press Enter.
- To go to a directory inside of your home directory, type cd directoryname and press Enter (replace directoryname’ with the directory’s name).
- If you want to create a new directory, run makedir directoryname (replace directoryname with the name you want to give your new directory. Then, use cd directoryname to enter that directory.
- You CAN create and edit files outside of your home directory, but you’ll need root access to do so.
- For example, if you want to create a file called «testfile,» type nano testfile and press Enter.
- It may be helpful to add «.txt» to the end of your filename so you know it’s a text file.
- If your current directory has a file by the same name, this command will instead open that file.
- The commands either start with a carat (^) or an M. The carat represents the Control key, while the M represents the Alt key. [2] X Research source
- For example, ^U is the command to paste. To paste something you’ve copied, you’d press Control + U.
- M-U is the command to undo the last action. To undo, you’d press Alt + U.
- You can use the mouse to highlight text that you want to copy and/or paste. To copy highlighted text, press Alt + 6. Then, use the arrow keys to move to another location in the file and press Control + U to paste. [3] X Research source
- Avoid the temptation to press Control + S to save, as that will just freeze your terminal window!
- You can reopen the file you created in Nano by typing nano filename just as you did before.
Using Vi or Vim
- You can also double-click the Terminal icon in your list of Applications, or by clicking your Dash menu (if you’re using GNOME) and searching for terminal .
- Vi is one of the oldest and most standardized Unix-based text editors. Vim stands for «Vi iMproved,» which means it’s like Vi but with more features. On most modern versions of Linux, running vi at the prompt will actually launch Vim instead. The basic commands are the same for both editors.
- Vi has more of a learning curve than Nano, but once you get the hang of it, it’s pretty easy to use.
- To view all folders in the current directory (your home directory), type ls and press Enter.
- To go to a directory inside of your home directory, type cd directoryname and press Enter (replace directoryname’ with the directory’s name).
- If you want to create a new directory, run makedir directoryname (replace directoryname with the name you want to give your new directory. Then, use cd directoryname to enter that directory.
- You CAN create and edit files outside of your home directory, but you’ll need root access to do so.
- For a file named «sample.text», for example, you’d type vi sample.txt .
- If your current directory has a file by the same name, this command will instead open that file.
- You should see — INSERT — pop up at the bottom of the window when you press the I key.
Type your text. While you’re in Insert mode, you can simply type as you typically would for any other text document. To go to the next line, just press Enter.
- You can use the arrow keys to move around the document while you’re in Command mode in both Vi and Vim. Vim also lets you use the arrow keys to move in Insert mode.
- Return to Insert mode at any time by pressing the i key.
- If you created a file without a name (or want to save the current edits to a new file), type :w filename instead, replacing filename with the name you want to give this file.
- To get help and learn more about Vi/Vim commands, type :help in Command mode and press Enter.
- To reopen the file, just type vi filename or vim filename .
- You can also save and quit at the same time by typing :wq in Command mode.
Community Q&A
First, get access to the server. You can either use SSH or physically access it. Follow the steps in the article to write a file with the code, but save it as .java instead of .txt. After that, type «javac yourFileName.java» and then «java yourFileName», with «yourFileName» being the actual name of your file. (If a Java compiler isn’t installed, this will not work. You can install a Java compiler from Ubuntu’s APT repository, but you need administrator rights for that, so you won’t necessarily be able to do it yourself.)
Thanks! We’re glad this was helpful.
Thank you for your feedback.
As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow3 Ways to Create a Text File Quickly Through the Linux Terminal
Being a Terminal-savvy person, you may always be looking for ways to ditch the mouse. Creating a text file is one task for which you can depend only on your keyboard on an Ubuntu system. Three commands from the Linux command line are at your service for creating text files. These include:
Let us explore these commands in this article to create some sample text files. The commands and procedures mentioned in this article have been run on an Ubuntu 20.04 LTS system. Since we will be creating the text files using Ubuntu command line-the Terminal; you can open it either through the system Dash or the Ctrl+Alt+T shortcut.
The cat Command
The cat command is very helpful when dealing with text files in Linux. It helps you in achieving three basic purposes:
- Creating a text file
- Printing contents of a text file in your Terminal
- Printing contents of a text file to another text file
Here, we will explore the first use of the cat command; creating a text file through the command line.
Enter the following command in your Terminal:
After entering this command, the next prompt will not appear; rather the cursor will display for you to enter the text for the file you just created.
In this example, I have created a text file through the following command and then entered some sample text:
Once you have entered all the text, hit enter to move to the next line and then use the Ctrl+D control to tell the system that you are done with entering the text. The usual command prompt will then appear for you to move on with further operations.
You can then use the ls command to see that your newly created text file will be there in the system.
Through the cat command, you can then view the contents of the file as follows:
You can see that the cat command shows the text I wrote while creating my sample file:
The touch command
Another way of quickly creating a text file through the Terminal is by using the touch command. The touch command, however, does not let you enter text in the file at the time of creation. After creating the file, you can enter the text through your favorite text editor. You might prefer the touch command over the cat command in one scenario; when you want to create multiple files at once through one command.
Let us first see how to create a single file first through the Linux touch command:
Use the ls command to see if the recently created file now exists on your system.
Create multiple files at once through the touch command
As mentioned above, the touch command takes the lead on the cat command on the basis that you can create multiple files simultaneously through the former. Use the following syntax to do so:
$ touch filename1.txt filename2.txt filename2.txt ….
For example, in the following command, I have created three files at once through the touch command:
$ touch sampletouchfile1.txt sampletouchfile2.txt sampletouchfile2.txt
I also checked the presence of the three files through the ls command in the above example.
If you want to edit any of the files you created through the touch command, you can use any of your favorite text editors. Here I am using the Nano editor to enter text to one of the files I created. I used the following command to open the file through the Nano editor.
I then entered the text and saved it by pressing Ctrl+X and then by hitting Enter.
The touch command can also be used to change the access and modification time of a file.
Change the access time of a file:
Set the modification time of a file:
You can view the access and modification time of files with the stat command:
Using the Standard Redirect Symbol
The standard redirect symbol is usually used when redirecting the output of a command to a file. However, it can also be used to create a single text file. The only difference is that while creating a new file we do not specify any command before the redirect symbol.
The difference between using the standard redirect symbol for creating a text file is that, unlike the cat command, you can not enter text this way. Also, unlike the touch command, you can only create one file at a time through the redirect symbol.
Use the following syntax in order to create a text file through this symbol:
When you save and exit the file, your text file will have those contents saved.
Through this article, we have learned three basic ways to create text files quickly through the Linux command line. You can now avoid the mouse and use only the keyboard in order to perform the simple task of creating a text file in Ubuntu.
Search
About This Site
Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.
Latest Tutorials