• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Startup Hero

The Official Blog Of Name Hero

Learn To Fly Above The Competition Get Started
  • HomeWelcome
  • CoursesVideo Training Center
  • About UsWhat is StartupHero?
  • BlogGet the latest
  • Start HereStartup 101
  • SpeakingPodcast & Media
  • ResourcesTools to help You
    • Reselling WordPress Hosting
    • Resell Hero
    • How To Start A Blog
  • NameHeroCloud Web Hosting

3 Ways To Open A Linux File Without Modifying It

By Bhagwad Park on December 29, 2021 0

3 Ways To Open A Linux File Without Modifying It

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:

cat test.txt

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:

less somelogfile.log

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:

Forbidding Modifications in vi
Forbidding Modifications in vi

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!

Bhagwad Park Profile Picture
Bhagwad Park

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!

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Connect With Us!

Superhero Resources

Fix Common Issues

  • How To Setup Free And Automatic SSL Certificates
  • How To Setup Cloudflare With Railgun
  • How To Fix Memory Exhausted Errors In WordPress
  • How To Edit PHP Version/Upload Limit/Add Extensions
  • How To Move/Migrate Your Business To Name Hero

Free Guides

  • How To Setup NameHero Hosting
  • How To Create A Web Hosting Business With WordPress
  • How To Start A WordPress Blog
  • How To Migrate WordPress To A VPS
  • How To Speed Test And Optimize Your WordPress Website
  • Magento 2.X Installation Guide
  • How To Clean Up A WordPress Hack

Training

Recent Posts

  • Why I Don’t Use AWS S3 For Website Backups
  • How To Add A “Vary: Accept-Encoding” Header To Apache
  • Managed vs Unmanaged VPS: The NameHero Difference
  • Why I Don’t Use Jetpack Backup
  • How To Share A Post Draft In WordPress
  • How To Use The Layout Grid Block By Automattic
Subscribe in a reader
  • Web Hosting
  • WordPress Hosting
  • Reseller Hosting
  • VPS Hosting
  • Twitter
  • Facebook
  • LinkedIn
  • YouTube
  • FTC Disclosure
  • Earnings Disclosure
  • Privacy Policy

Copyright © 2022 · Smart Passive Income Pro on Genesis Framework · WordPress · Log in