When navigating through the shell, I’m often logged in with dangerous privileges. I try and minimize the potential damage by logging in as a user with sudo privileges, but it can still be a nerve-wracking experience knowing that an accidental click of a key or a moment’s thoughtlessness can bring your entire system crashing down. It’s a bit like being on a bridge with a long drop. No matter how safe you are, and no matter how sturdy the guardrails, you still feel nervous. So here are three ways to read important files in Linux without accidentally overwriting them.
Option 1: cat – Small Files for Quick Viewing
If the file you’re looking at is just a few lines, then there’s nothing simpler than just using “cat”. So if you have a file called “test.txt” in your current folder, just type:
The file will display right in the terminal without any danger of modification since you’re returned immediately to the command prompt. It’s a simple way to quickly see the contents of a file. Even if the file is a few pages, it’s ok. The command “cat” will dump the contents on the shell screen, and you can use your mouse cursor to scroll up and see the contents.
However, cat isn’t a great idea if the file you’re looking for is extra large.
Option 2: “less” – For Large Files
Some files are too large for convenient display using cat. Log files are a great example of this. You might be looking at thousands of lines of text, and there’s not enough space for cat to show it all. Also, you might want the program to only load a few lines at a time instead of dumping the entire file into memory.
For such situations, the command “less” is better than cat. So if you want to see a huge log file, type:
This will display the output of the file one screen at a time. You can scroll up and down using the arrow keys on your keyboard. You can also type in a number, followed by the “f” key to scroll a specified number of lines at a time. The command “less” is useful because the entire file isn’t loaded into memory – only the data set that you’re working with is used.
To quit the “less” command, simply type “q” and you’re done! It’s a very efficient command that’s super useful when parsing the output of unwieldy log files. And of course, there’s no danger of any modifications going through.
Option 3: “vi -M” – For a Familiar Interface
Some of you might be used to the familiar “vi” interface, and want to know how to use vi without accidentally modifying anything. The answer is simple. To open a file in vi without any danger of changing it, use the following command:
vi -M somefilename
The -M parameter absolutely forbids any writing. Other methods in vi like the “-R” parameter only give you a warning if you accidentally change something. With those, you can still force the vi editor to save your changes with the exclamation (!). But when you try and modify a file that was opened with the “-M”, you get this:
You can’t override this even if you have the appropriate privileges, and even if you try and force the editor to save your changes with the (!) command. This makes it the safest way to open a file in vi since you can’t modify the file even if you want to.
So these are the three ways to read a file in Linux while protecting it from accidental modifications!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!