Viewing and Editing Files

Linux

📂 File and Directory Navigation

Now that we've learned the basics of file and directory navigation, let's learn how we can display and edit files, search for text within files and more.

🔍 Viewing File Contents

In the Windows GUI, if we want to open a file and view its contents, we can just double-click on the file. Depending on the file type, it will open in a default application. In Windows, text files default to open in an application called Notepad. But we can change this if we want to.

⚙️ Changing Default Application

To change the default application that opens files, just right-click and click "Properties". Under Open With, we can change the application to another text editor like WordPad. Most of the files that we'll be dealing with throughout this course will be texts and configuration files. Let's just focus on those files instead of images, music files, etc.

💻 Viewing Files in the Terminal

Viewing the contents of a file in PowerShell is simple using the cat command, which stands for concatenate. Let's give it a try. This will dump the contents of the file into our shell. This isn't the best solution for a file, since it just keeps writing the content until the whole file is displayed.

📖 Paging Through Files

If we want to view the contents of the file one page at a time, we can use the more command like this. The more command will get the contents of the file, but will pause once it fills the terminal window. Now we can advance the texts at our own pace. When we run the more command, we're launched into a separate program from the shell. This means that we interact with the more program with different keys.

🔑 more Command Keys:

  • Enter: Advances the file by one line

  • Space: Advances the file by one page

  • Q: Allows you to quit out of more and go back to your shell

🔝 Viewing the Head and Tail of Files

If we just wanted to quickly see what the first few lines of a text file are, we can use the head command with the cat command. This will show us the first 10 lines of a file. To view the last few lines or the tail of the file, we can use the tail command.

📜 Viewing Files in Bash

🐚 Using cat Command

To read a simple file in bash we can also use the cat command to view a document. So let's look at important document. The cat command is similar to the Windows cat command, but it doesn't do a great job at viewing large files. Instead, we use another command, less.

📖 Using less Command

Less does a similar thing that more does for Windows, but it has more functionality. Fun fact, there's a bash command called More but it's been slowly dying out in favor of Less. It's literally a case of "less is more". Similar to more, when we use less we're launched into an interactive shell.

🔑 Common less Commands:

  • 🔼🔽: Navigate up and down

  • 🔼🔽: Page up and page down

  • g: Move to the beginning of the file

  • G: Move to the end of the file

  • /: Search for a word or phrase

  • Q: Quit less and go back to the shell

Less offers functionality like searching within a file, making it a great tool to use to view files of any size. You'll no doubt end up using this command often as an IT support specialist.

🔝 Viewing the Head and Tail of Files

Similar to the Windows cat and head parameter, we can do the same thing in Linux using a command called head. This will show you by default the first ten lines of the file. Now, what if you wanted to view the last few lines of the file? You can use a command called tail. This will show you by default the last ten lines of the file.

Last updated