In this tutorial we will cover how to open, edit, move and copy a file within Linux using the terminal window and some basic commands.
Use the table of contents below to jump to a specific section or read on to learn more!
How to open a file on Linux To learn how to open a file on Linux
using the terminal, simply follow these simple steps:
- While there are several ways to open a file on Linux, the easiest way to display the contents of a file is to use the cat command. For example, suppose you have a text file named SampleText.txt, which contains a single text string that says “This is a test file.” Use the cat command followed by the name of the file you want to open, like this:
You should receive an output to the terminal containing the text, “This is a test file.” If the file had contained more text than this, everything would have been displayed.
- While the example used above contains only one line of text, for large or multi-page documents, it can be cumbersome to try to open all of its contents in the terminal at once. This is where the least command comes in handy. Using the less command, you can have Linux display the contents of the file page by page. Use it the same way you would use the cat: minus SampleLongText command.txt
This command would generate the contents of the SampleLongText.txt file, one page at a time, allowing you to scroll to see more.
How to edit a file
in Linux To learn how to edit a file in Linux using the terminal, simply follow these steps:
- There are several different tools within Linux that can be used to edit files. The two most popular are Vi (or Vim) and Nano. While each has its advantages, the biggest differences between the two are ease of use and functionality. Vi is a more powerful and complicated tool, and Nano is simpler but can do less. We’ll start with Nano.
- To open a file in Nano, you need to enter the nano command followed by the path of the file you are trying to open. You can also navigate to the proper directory first using the cd command, then open the file for editing simply using nano followed by the file name. For example:
If the specified file already exists, it will open for editing. If no file with this name exists in this location, a new file is created.
One of the
- advantages of Nano is that it presents a list of shortcuts at the bottom of its interface. This allows users to use the tool without having to memorize every command, making it ideal for those who are new to Linux. The arrow keys can be used for navigation and the backspace key is used to delete. Overall, Nano works as a simplified but familiar text editor.
- The other popular option for editing a file on Linux is to use the vi command. Like nano, vi must be followed by a specific file path, or if it is already within the desired directory, only the file name can be used. For example:
The main difference between Vi and Nano is that Vi features different modes, allowing users to interact with the document in different ways. While this gives a greater degree of control over the document, it can also be very confusing and somewhat counterintuitive for those new to the tool.
- The default mode in which Vi is entered is Command mode, which is used for navigation and entering commands. Like Nano, Vi uses arrow keys for navigation. However, unlike Nano, any text entered into Vi will not be treated as a text string that is added to the document, but as a command that is transmitted directly to Vi. We’ll cover some of these commands below.
- To add text to the document, you must first enter Insert mode. To enter Insert mode, move the cursor to the location where you want to enter new text, and then press the i key. You will see the phrase “INSERT” appear in the lower left corner of the screen. Now, any text you enter will be treated as a text string that is added to the document. To return to Command mode, press the Esc key repeatedly. You’ll know you’ve exited Insert mode when the phrase “INSERT” disappears from the bottom corner.
- Unlike Nano, to delete a character in Vi, you must use the x key while in Command mode. This will remove any characters that are currently highlighted by the cursor.
- To enter a command in Vi, you must first start with a colon “:”. For example, to save (or write to) a file to which you have made edits, use the :w command. To exit Vi, use the :q command. To save and exit at once, combine both commands into :wq. You can add an exclamation mark “!” to any command to force it. For example, :q! would force Vi to exit, overriding any confirmation screens that might otherwise be triggered.
- For a list of available navigation shortcuts, see this post on Editing files with Vi. Scroll to the bottom for a full list of additional motion and editing options.
How to move a file in Linux To learn how to move a file in Linux
using the terminal, simply follow these simple steps: First,
it is important to understand that
moving a file in Linux
- and renaming it is the same action. This is because when you’re moving a file to a new location within Linux, you’re actually renaming its file path to include new information.
- To move a file on Linux, use the mv command followed by the source location, and then the intended destination. For example, if you want to move the SampleText.txt file from its current location in the home directory to a new location in the /tmp directory, you would do so with the following command:
mv SampleText.txt /var/tmp
You can even rename the file as it moves. Doing so would look something like this:
mv SampleText.txt /var/tmp/
Or, if you want to rename the file without moving it to a new location, you can simply repeat the above command by omitting the “/var/tmp/”.
How to copy a file on Linux To learn how to copy a file on Linux using the terminal, simply
follow these steps: To
- on Linux, simply use the cp command followed by the source file name and then the new file. For example:
cp SampleText.txt SampleText_2.txt
The above command would generate a new file, named SampleText_2.txt, that contains all the contents of the previous file. By default, this new file will be created in the same directory as the current file, unless otherwise specified. If you want to copy the file to a new directory, you can use the desired path instead of the second file name, or include both to copy the file to a new location with a new name. To get an idea of what this would look like, see the section above that covers the mv command, but use the cp command instead.
Looking for more information about Linux? Search our knowledge base!
Interested in more articles about operating systems? Navigate to our Categories page using the left bar or check out these popular articles
: How to check Apache version on a
- dedicated Linux cPanel
- change a password on
- How to check if your Linux server is under DDoS attack Popular
server How to
tags within this category include: Linux, Windows, Apache, CentOS, Debian, Fedora, RedHat and more.
Can’t find what you’re looking for? Use the search bar at the top to search our entire Knowledge Base.
The speed difference
Looking for a better dedicated server solution? In the market for private cloud services or colocation? Check out Hivelocity’s extensive product list for great deals and deals.
With best-in-class customer service, affordable prices, a wide range of fully customizable options, and a network like no other, Hivelocity is the hosting solution you’ve been waiting for.
Not sure which of our services is best for your particular needs? Call or live chat with one of our sales agents today and see the difference Hivelocity can make for you.