LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 09-29-2011, 12:55 PM   #1
sathish3129
LQ Newbie
 
Registered: Sep 2011
Posts: 2
Blog Entries: 1

Rep: Reputation: Disabled
Cat vs Less


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.?

$ wc -l MyFile.txt
1568
$ cat MyFile.txt|less
$less MyFile.txt
 
Old 09-29-2011, 01:23 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,577
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
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.
 
1 members found this post helpful.
Old 09-30-2011, 12:03 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
Old 09-30-2011, 12:20 AM   #4
sathish3129
LQ Newbie
 
Registered: Sep 2011
Posts: 2
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Hey thanks all great replies.

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.
 
0 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cat 6 Cat 5e cable question babbab Linux - Networking 1 04-07-2010 03:33 PM
cat onelinefile.txt >> newfile.txt; cat twofile.txt >> newfile.txt keep newline? tmcguinness Programming 4 02-12-2009 07:38 AM
Using cat readline within cat readline while loop demxkn66 Programming 5 12-16-2007 06:10 PM
Cat ouncya Linux - Newbie 2 08-24-2007 06:34 PM
cat in asm/ cat --show-all option Tux Linux - Software 1 09-02-2006 10:31 PM


All times are GMT -5. The time now is 12:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration