HomeLinuxView the File Contents in Linux Command Line

View the File Contents in Linux Command Line


You realized to create new recordsdata within the earlier chapter of the Terminal Fundamentals collection.

On this chapter, you will be taught to learn the recordsdata. I will be discussing the commonest Linux instructions to show the contents of a textual content file.

Earlier than you try this, let’s create our ‘playground’ with pattern recordsdata. Let’s create a listing first and change to it.

mkdir display_files && cd display_files

Copy an enormous textual content file right here.

cp /and so forth/companies .

After which, create a brand new file named columbo.txt with the next textual content (use the cat command with >> as mentioned within the earlier chapter):

Prescription: Homicide
Ransom for a Useless Man
Homicide by the Ebook
Dying Lends a Hand
Useless Weight
Appropriate for Framing
Girl in Ready
Brief Fuse
Blueprint for Homicide

You do not have to kind all of it by your self. You’ll be able to copy-paste within the terminal utilizing Ctrl+Shift+V. Most terminals help this shortcut.

With issues set, let’s examine numerous methods of viewing recordsdata within the Linux terminal.

Use cat command to show file content material

The cat command is the most well-liked methodology to view recordsdata in Linux.

It’s useless easy to make use of. Simply give it the file title and it shows the file content material on the display screen. Issues can not go easier than this.

cat filename

Are you able to strive displaying the contents of the columbo.txt file?

cat columbo.txt

That is the output it exhibits:

Using the cat command to view files in Linux
Utilizing the cat command to view recordsdata in Linux

🖥️

Non-obligatory problem: Use the cat or echo command with >> redirection so as to add a brand new line with “Etude in Black” textual content to the columbo.txt file. Confer with the earlier chapter if you happen to need assistance.

Utilizing the much less command to learn massive textual content recordsdata

The cat command is so easy. In reality, it’s too easy. And easy does not work in sophisticated situations.

Attempt utilizing the cat command to view the content material of the companies file.

cat companies

This companies is a large file with lots of of traces. Once you use cat, it floods the complete display screen with the complete textual content.

This isn’t splendid. Are you able to learn the primary line of the file? Sure, you’ll be able to however you must scroll all the best way up. If the file has 1000’s of traces, you will not even be capable to scroll again to the primary few traces.

That is the place the much less command comes into the image. It allows you to learn the contents of a file in a page-by-page method. You exit the viewing mode and your terminal display screen is clear as ever.

Use the much less command to learn the companies file:

much less companies

Now you’re in a unique viewing mode. You should utilize the arrow keys to maneuver line by line. You may as well use the Web page Up and Web page Down keys to maneuver up and down by pages.

You’ll be able to even seek for sure textual content utilizing /search_term.

If you find yourself achieved studying the file, press Q key to exit the much less view and return to the conventional terminal viewing.

less command example
Viewing an enormous textual content file with the much less command

This desk will aid you use much less:

Keys Motion
Up arrow Transfer one line up
Down arrow Transfer one line down
House or PgDn Transfer one web page down
b or PgUp Transfer one web page up
g Transfer to the start of the file
G Transfer to the tip of the file
ng Transfer to the nth line
/sample Seek for sample and use n to maneuver to subsequent match
q Exit much less

From viewing recordsdata in actual time to bookmarking textual content, much less can do much more. Learn this to be taught extra about it.

9 Sensible Instance of Much less Command in Linux

Much less is an superior Linux command utility for viewing textual content recordsdata. Listed below are some important much less command examples to make use of it successfully.

💡

You should utilize the much less command to learn PDF recordsdata within the terminal.

Head and tail to point out a part of textual content recordsdata

If you happen to solely wish to see sure elements of the textual content file in cat-styled show, use the top and tail instructions.

By default, the top command shows the primary 10 traces of a file.

head filename

However you’ll be able to modify it to point out the primary n traces as effectively.

head -n filename

The tail command shows the final 10 traces by default.

tail filename

However you’ll be able to modify it to point out n traces from the underside.

tail -n filename

Observe examples

Let’s examine some examples. Generate an easy-to-follow file utilizing this script:

#create or clear the content material of the file
echo -n > pattern

#put content material to the file
for i in {1..70}
do
  echo "That is the road $i" >> pattern
achieved

Create a brand new file named script.sh and copy-paste the above script content material into it. Now run the script like this to generate your pattern file:

bash script.sh

Now, you’ve got a file named pattern that incorporates traces like “That is the road quantity N” for each 70 traces.

🖥️

Show the primary 10 and the final 10 traces of this pattern file.

Let’s take it to the following stage. You’ll be able to mix them each to point out particular traces of a file. For instance, to point out traces from 35 to 40, use it like this:

head -n 40 filename | tail -n +35

Right here:

  • head -n 40 filename will show the primary 40 traces of the file.
  • tail -n +35 will show the traces from the thirty fifth line to the tip of the output from the head command. Yeah! Thoughts the + signal that adjustments the conventional habits of the tail command.
Show a range of lines in Linux

You may as well mix them to point out solely a specific line. For example you wish to show the fifty fifth line; mix head and tail like this.

head -n 55 filename | tail -n 1

Right here:

  • head -n 55 filename will show the primary 55 traces of the file.
  • tail -n 1 will show the final line of the output from the head command, which would be the fifty fifth line of the file.
Show only a particular line in Linux command line

Check your data

Time so that you can train your gray cells and apply what you realized on this chapter.

  • Use the identical pattern file and show traces from 63 and 68.
  • Now show the traces from 67 to 70.
  • How about displaying the primary line solely?
  • What do you see within the /and so forth/passwd file? Show its content material.

That is it for this chapter. Subsequent, you will study eradicating recordsdata and folders within the command line. Keep tuned.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments