LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-01-2014, 02:08 AM   #1
linuxUser_
LQ Newbie
 
Registered: Aug 2014
Posts: 7

Rep: Reputation: Disabled
Need one line command to count number of cores


Hi Hope everyone doing fine,

I need a one line command to count number of cores in all processers.
My lappy is 8 core mechine (4 duel core processers) and i can see that 2 cores in each and every prcessor.
There are many ways to see individual processors and cores.
But my requirement is to get total number (just a number. 8 in my case) of cores with one line command.
Is there anything as such?


Thanks & Regards,
linuxUser_
 
Old 08-01-2014, 02:17 AM   #2
yooy
Senior Member
 
Registered: Dec 2009
Posts: 1,387

Rep: Reputation: 173Reputation: 173
you should do lscpu and pipe correct line with | to grep and than pipe again with awk
example
Quote:
wmctrl -l | grep "Terminal" | awk '{ print $2 }'`
 
1 members found this post helpful.
Old 08-01-2014, 02:26 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,042

Rep: Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914
The data is also in /proc/cpuinfo - grep has an option that will do the counting for you - see the manpage.

BTW, there is never a need to use grep piping to awk like that - awk has regex which will do the selection.
 
Old 08-01-2014, 02:30 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,500

Rep: Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431
I do not really understand you. You found a thread with a lot of tips (related to your problem). Have you tried those one liners? Have you got any result?
 
Old 08-01-2014, 02:56 AM   #5
linuxUser_
LQ Newbie
 
Registered: Aug 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hi All,

Thanks for your replys.
@yooy: I am not good at shell commands. can you post complete command line to calc number of cores.
@syg00 & pan64: I need it because some of my softwares requires number of cores of customers cps. Yes pan64, I have tried all of them and gettin number of cores in individual processors. Not a total sum. As I explained in my thread in my lappy there are 4 duel core processors means 2 cores in each processors summing to 8. I am able to see 2 cores in each processors from all the commands I have found. But unable to get total number of cores = 8 in my case.


Thanks & Regards,
linuxUser_
 
Old 08-01-2014, 03:07 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,500

Rep: Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431
the solution is here: http://www.linuxquestions.org/questi...3/#post5165853
what kind of cpu do you have?
 
Old 08-01-2014, 05:12 AM   #7
linuxUser_
LQ Newbie
 
Registered: Aug 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hi pan64,

I want a generic shell script that will work in all kinds of CPU's. Because my software should work irrespective to the customar CPU, number of cores etc,
None of the postes giving me total number of cores in that link you shared. Infact I posted this thread after going to all the replys there.

Thankyou

Regards,
linuxUser_
 
Old 08-01-2014, 05:18 AM   #8
jensd
LQ Newbie
 
Registered: Jun 2014
Posts: 11

Rep: Reputation: Disabled
Try this:
cat /proc/cpuinfo|grep "cpu cores"|cut -d ":" -f 2|awk '{ OUT += $1} END { print OUT }'

or this:
cat /proc/cpuinfo|grep processor|cut -d ":" -f 2|tail -1|awk '{ OUT=$1 + 1} END { print OUT }'

A little effort from your side would be appreciated too...

Last edited by jensd; 08-01-2014 at 05:23 AM.
 
1 members found this post helpful.
Old 08-01-2014, 05:21 AM   #9
linuxUser_
LQ Newbie
 
Registered: Aug 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks a lot jensd.
I tried but could not figured it out.

Anyway thanks a lot

Regards,
linuxUser_
 
Old 08-01-2014, 05:45 AM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
Quote:
Originally Posted by linuxUser_ View Post
Hi pan64,

I want a generic shell script that will work in all kinds of CPU's. Because my software should work irrespective to the customar CPU, number of cores etc,
None of the postes giving me total number of cores in that link you shared. Infact I posted this thread after going to all the replys there.

Thankyou

Regards,
linuxUser_
So... Why need to get CPU infos, count CPU cores??
 
Old 08-01-2014, 06:15 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,500

Rep: Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431
Quote:
Originally Posted by linuxUser_ View Post
Thanks a lot jensd.
I tried but could not figured it out.

Anyway thanks a lot

Regards,
linuxUser_
first solution of jensd does not work. I have just tried on a suse linux, it simply does not return anything. The second one is probably ok, but there are much better and shorter equivalents available, especially that can be implemented with a single grep command. That was already posted and you told you tried and did not work for you.
 
Old 08-01-2014, 06:42 AM   #12
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
why not just
Code:
grep -c processor /proc/cpuinfo
?

Code:
grep --help
<snip>
      --exclude-dir=PATTERN  directories that match PATTERN will be skipped.
  -L, --files-without-match  print only names of FILEs containing no match
  -l, --files-with-matches  print only names of FILEs containing matches
  -c, --count               print only a count of matching lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name
<snip>

Last edited by Firerat; 08-01-2014 at 06:45 AM.
 
Old 08-01-2014, 08:56 AM   #13
linuxUser_
LQ Newbie
 
Registered: Aug 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hi All,

@Keefaz: my codes can be run in parallel and have to specify number of cores. So I need to know before I run it in parallel.
@pan64: Its actually good input for me. 1st line actually working for me and its not working in linux suse. I taught to reduce customer pain but finally I came to know that they have to enter number of cores. Or I have to figure it out some command that will work irrespective to the type of operating system.
@Firerat: Thanks yaar for sharing knowledge.

Regards,
linuxUser_
 
Old 08-01-2014, 09:11 AM   #14
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
You can also list the /sys/bus/cpu/devices directory as suggested in the other thread

(depending on your code language, it could be quicker to open a directory and count number of elements than open a shell, execute a command, parse the output and close the shell)

Last edited by keefaz; 08-01-2014 at 09:17 AM.
 
Old 08-01-2014, 09:14 AM   #15
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
maybe you can script something based on this info:
http://www.linuxquestions.org/questi...ml#post4782300

if you need any more help perhaps you should post your /proc/cpuinfo .
 
  


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
Number of command line arguments. nelsonkkyy Linux - General 5 03-01-2012 10:54 PM
Trying to identify file types(command line arguments) and count how many there are jdwalk Linux - Newbie 5 02-20-2010 04:51 PM
number of emails over command line kvtournh Linux - Software 1 01-11-2005 09:52 AM
Count number of line in a file. philipina Programming 7 03-18-2004 06:04 PM
Count of unread messages from command line rebuke Linux - Software 0 12-26-2003 07:41 PM


All times are GMT -5. The time now is 09:21 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