Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

10 Examples of more and less command in Linux

Hello guys, if you have been working on Linux then you must have come across less and more commands. They are the most popular command to view content of a file, particular log file in Linux.  In fact, less is my favorite command to see the logs in Linux because you can open both normal log file as well as a gzip or compressed log file using less command for viewing. It's also very well designed and fast command and you easily open even large log files using less which you would otherwise struggle to open using Windows tool like Notepad or Notepad++.  In the past, I have shared examples of lsofcurl, netstat, and ssh command and in this article, I am going to share common examples of both less and more command.

In today's article, You shall be learning about the more and the less command. This is an interesting command in Linux. The objectives of this article are: To explain the more command, and less command, and to provide examples of both commands and the difference between them.

Now let's move on to the examples or usage of the more command in Linux


10 Example of using more and less command in Linux

Here are my favorite and common example of how to use less and more command in Linux. The best thing about more command is that now they are also supported on Windows command line as shown below:



1. To use more to display a file

$ "more [options][filename]"

This is the basic and simple thing you could do with more command. But it could also be that you don't have any option. let's say I don't want to use any option, I would just do the following

$ more sample.txt

If you do this, you will see the content of a text file that you specified.


2. To display specific numbers of a file

"more -10 sample.txt"

The first command we had here will bring all contents in the file ,while this particular one bring the first 10 lines

3. To squeeze blank lines in a file

"more -s sample.txt"

You may want to do away with the blank lines in the file, the option -s enables you to do that, which means you are squeezing the white spaces or blank lines out.

4. To make more prompt

"more -d sample.txt"

There could be times you may want to prompt more useful information. e.g press space to continue, press q to quit. and lots more. The option -d enables us do that. then followed by the file name, the sample.txt file is just a demo file.

Now let's move to the less Command. That is quite the opposite of more. think of it as a reduction let's see the following command examples on less.


10 Examples of more and less command in Linux


5. To display reduced content a file text

"less sample.txt"

For instance, if sample.txt is a large file , Of course it would be loaded into the memory with it's full capacity if no exception occurs. So, using the less commands reduces it. It does not load the entire file, it only loads it in parts and this makes it faster.

6. To omit the blank lines

Yes! you may be wondering squeezing blank lines with the less command too. As you could squeeze blank lines with the more command, same with the less command. let us see the command

"less -s sample.txt"

Remember because we are using less command. It will not load the entire file, It will load in part for effectiveness sake. So what happens with this command is that after the part of the file is been loaded for display, all blank lines are squeezed.

7. To open multiple files

"less sample1.txt  sample2.txt"

Given that, you already have the files available, in our case, we have the first file and the second file. sample1 and sample2 respectively. separated by space.

8. To edit files

"v"

Normally you wouldn't have been able to edit files because with the less command you can only view files. however, there is a shortcut that can handle that which is the option v. while in viewing mode just press v, and that takes you to the system's default text editor. and that same file is reopened there and you can edit it.

9. Use the less command with pipes

You use less commands with other commands with pipes. Most especially when you know the output you are combining with the less command is going to give you a very huge result. For instance we are going to be using dmesg command which usually returns thousands lines of messages or even more. And you don't want to bombad your screen with lot of messages so there could be effective analysis. So in this case you just do the following:

dmesg | less

10. To search for a word in less command

After using the less command on a file, you could search for the word you are looking for by doing the following:

/word

You only need to provide a forward slash and type the exact word you are searching for. This brings all the occurrences of the word.


That's all about how to use more and less command in Linux. In this article, you learned how to use the "less" and "more" commands. In a simple term the more command will completely load the entire file and there are numerous options you could use with it. Also, the less command on the other hand will reduce the content of the file by not loading the entire file at once, It loads in part.

Other Linux Articles and Resources you may like


  • 10 examples of Networking commands in Unix (nslookup)
  • 5 Example of kill commands in Unix and Linux (example)
  • How to set up cron jobs in Linux (Crontab example)
  • 10 Examples of chmod command in Linux (chmod)
  • Top 5 Courses to learn Vim Editor (vim courses)
  • VI Editor examples and tips for beginners (vi examples)
  • How does the nslookup command work in UNIX? (answer)
  • 10 Linux command line courses for Beginners (courses)
  • Linux find + du + grep example (example)
  • 7 Best Linux Courses for DevOps Engineers (Linux courses)
  • 10 examples of lsof command in Linux? (examples)
  • How to use the netstat command to find which process is listening on a port? (example)
  • 10 Examples of curl command in Linux (cURL)


Thanks for reading this article so far. If you like these Linux tutorial about using lsof command and my examples  then please share it with your friends and colleagues.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.