Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
There are more ways to do read a file. If we have larger file more than 1000 lines, most of them use LESS rather than CAT. i have listed two ways of reading such big file. I just want to know which is efficient way, which consume less memory of my CPU.?
In general if a command will do the open/read for you by itself it is less efficient to cat the file and pipe it to the command. The command is processing the data anyway.
You can see the difference yourself. Try doing:
time cat MyFile.txt
time (cat MyFile.txt|cat)
The latter should show more time used.
Also more memory would be used by doing the pipeline and cpu has to spawn another program. For tiny things this isn't an issue but if everything in your system operated that way it could cause you problems.
cat is a concatenator. It's designed for taking multiple separate inputs and printing them all to stdout as a single unbroken stream. It's basically just a pipe from input to output. While it can be used to display a single file for reading, that's just a convenient side-effect. The system requirements are very low, and it exits immediately.
less is a pager, a text display application. It's designed to take a single input and break it up into pages that will fit the size of your display. You can then skip back and forth among them. This requires the program to stay active, holding the input text in memory, until you manually exit it.
And in general, two processes will consume more system resources than one.
The only real reason you'd want to use both cat and less together is if you wanted to combine several files into one and then read them as a single unit.
It seems like, less keeps entire file in memory and buffer(display) the part of file(few lines). while cat wont store the file, its just through what it have.
when cat followed by pipe with less seems to be efficient rather than using only less a file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.