In this tutorial we’ll cover how to open, edit, move, and copy a file within Linux using the terminal window and a few basic commands.
Use the the table of contents below to jump to a specific section or read on to learn more!
Absolute and Relative Paths
When using commands for moving, reading, copying, and deleting files in Linux, there are two ways to type a file path, an absolute path and relative path.
An absolute path is the specific location of the file you are editing irrespective of your current working directory or combined paths. An absolute path must be written with reference to the root directory, starting with a forward slash /. An example of using an absolute path would look like: cd /usr/bin
A relative path on the other hand is the path relative to your current working directory using period and double periods to indicate the current and parent directories. An example of using a relative path when the current working directory is /usr/ would look like: cd bin
Both types of path will take you to the same place but require different steps and criteria to do so.
How to Open a File in Linux
While there are multiple ways to open a file in Linux, the easiest way to display the contents of a file is using the cat command. For example, let’s say you have a text file named lorem.txt, which contains a paragraph of text. Use the cat command followed by the name of the file you want to open, like this:
While the example used above contains only one paragraph of text, for large or multipage documents, it may be cumbersome to attempt to open their entire contents in the terminal at once. The less command comes in handy in situations where large text is present. By using the less command, you can have Linux display the contents of your file one page at a time. Use it the same way you would use the cat command:
This command would then output the contents of the longlorem.txt file, one page at a time, allowing you to scroll to view more. Note that the colon highlighted at the bottom of the screenshot above is the indication that you can scroll up and down the file.
How to Edit a File in Linux
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 must enter the nano command followed by the path of the file you are attempting to open. You may also first navigate to the proper directory using the cd command, then open the file for editing just using nano followed by the filename. For example:
If the file specified already exists, it will be opened for editing. If no file exists with this name at this location, a new file will be created.
One of the advantages of Nano is, it features a list of shortcuts at the bottom of its interface. These shortcuts allow users to use the tool without having to memorize every command, making it ideal for those who are newer to Linux. Arrow keys can be used for navigation, and the backspace key is used to delete. All-in-all, Nano works like a simplified but familiar text editor.
Common nano commands can be found in the table below.
|Ctrl + G||Display help|
|Ctrl + X||Close the current file|
|Ctrl + O||Save the current file|
|Ctrl + W||Search for a string or a regular expression|
|Ctrl + K||Cut the current line|
|Ctrl + U||Uncut from the cut buffer|
|Ctrl + J||Justify the current paragraph|
|Ctrl + T||Check the spelling of the current file|
|Alt + U||
Undo the last operation
|Alt + E||
Redo the last operation
The other popular option for editing a file in Linux is to use the vi command. Like nano, vi must be followed either by a specific file path, or if you’re already within the desired directory, just the file name can be used.
The primary difference between Vi and Nano is that Vi features different modes, allowing users to interact with the document in different ways. While this feature gives a greater degree of control over the document, it can also be very confusing and somewhat counter-intuitive for those new to the tool.
The default mode that you enter Vi in is the Command mode, used for navigation and entering commands. Like Nano, Vi uses the arrow keys for navigation. However unlike Nano, any text entered into Vi won’t be treated as a string of text being added to the document, but rather a command being relayed directly to Vi.
To add text to the document, you must first enter Insert mode. To enter Insert mode, press the i key. You’ll see the phrase “INSERT” appear in the bottom left corner of your screen, as shown below.
Now, any text you enter will be treated as a string of text being added to the document. To return to Command mode, press the Esc key. You’ll know you’ve exited Insert mode when the “INSERT” phrase disappears from the bottom corner.
Unlike Nano, to delete a character in Vi, you must use the x key while in Command mode or using the backspace key while in Insert mode. This will delete whichever character is currently highlighted by the cursor.
Vi also allows you to enter commands, with each command starting with a colon “:“. For example:
- To save (or write to) a file that you’ve made edits to, use the :w command.
- To quit Vi, use the :q command.
- To save and quit all at once, combine both commands into :wq.
- *NOTE: You can add an exclamation point “!” to any command to force it. For example, :q! would force Vi to quit, overriding any confirmation screens that may otherwise be triggered.
For a list of available navigation shortcuts, check out this post on Editing Files with Vi. Scroll to the bottom for a full list of additional movement and editing options or reference the table below.
|:wq||Save and exit|
|:q!||Quit without saving|
|/pattern||Search for pattern|
|n||Repeat search forward|
|N||Repeat search backward|
How to Move a File in Linux
First, it is important to understand that moving a file in Linux and renaming it are the same action. This is because when you are moving a file to a new location within a Linux system, you are really renaming its file path to include new information.
To move a file in Linux, use the mv command followed by the source path and then the destination path. For example, if you wanted to move the file moveme.backup from its current location in the /backup directory to a new location in the /review directory, you would do so with the following command:
mv /backup/moveme.backup /review
You can even rename the file as it’s being moved. Doing so would look similar to this:
mv /backup/moveme.backup /review/newfile.backup
Or, if you wanted to rename the file without moving it to a new location, you could simply repeat the above command while omitting the “/review”.
How to Copy a File in Linux
To copy a file in Linux, use the cp command followed by the path and name of the source file and then the new file’s path and name. For example:
cp sample.txt sample2.txt
The above command would generate a new file, named sample2.txt, which contains all the contents of the previous file. By default, this new file will be created in the same directory as your current file, unless otherwise specified.
If you’d like to copy the file to a new directory, you can use the desired file path instead of the second file name, or include both to copy the file to a new location under a new name. An example is shown below.
- How to Check the Apache Version on a Linux Dedicated cPanel Server
- How to Change a Password in Linux
- How to Check if Your Linux Server is Under DDoS Attack
Don’t see what you’re looking for? Use the search bar at the top to search our entire Knowledge Base.
The Hivelocity Difference
With best-in-class customer service, affordable pricing, a wide-range of fully-customizable options, and a network like no other, Hivelocity is the hosting solution you’ve been waiting for.