Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I´m trying to count how many shell or terminal windows are currently running.

I tried ps a|grep bash; ps a|grep tty;

But it can be inaccurate.

share|improve this question
What's your definition of terminal window? Do a gnome-terminal tab, virtual terminal, person logging in over ssh, screen/tmux pane, expect emulated user session, xterm not running a shell, etc count? – Stéphane Chazelas May 29 '14 at 18:40
Terminal window opened with "ctrl-alt-t". I solved it by counting how many "pts" are in >who command who|grep "pts"|wc -l – Martincho May 29 '14 at 18:57
ls /dev/pts/  | wc -l

Use the above command to count the terminal windows open.

share|improve this answer
Tried that. It shows 23. And I have only one terminal/bash window open. – Martincho May 29 '14 at 18:18
@user68957 Are you on a shared computer with multiple users by any chance? – Chirag64 May 29 '14 at 18:26
One home computer, one user at the time. The trick would be to count how many terminal windows are open, or terminal processes. If I do a >who, it list two users me :0 yyyy-mm-dd hh-mm (:0) me pts/0 yyyy-mm-dd hh-mm (:0) If I open another terminal window, with >who it shows: me :0 yyyy-mm-dd hh-mm (:0) me pts/0 yyyy-mm-dd hh-mm (:0) me pts/1 yyyy-mm-dd hh-mm (:0) Should I just grep "pts/"? – Martincho May 29 '14 at 18:44

To list them:

ps aux | awk '{print $7}' | grep -v "?"

To count them:

ps aux | awk '{print $7}' | grep -v "?" | wc -l

You will need to subtract 1 from that number as it includes the top TTY header.

This all depends if you are wanting to count how many sub shells are running or if you are wanting to count how many terminal windows are open.

To count just the terminal windows you would need to use:

ls /dev/pts/ | wc -l (stated in a previous answer)

For example:

On my system there are currently six tty's available. I also have one terminal open pts/0 that has 4 processes running in the fg or bg.

root      4565  0.0  0.0   4060   576 tty1     Ss+  May01   0:00 /sbin/mingetty /dev/tty1
root      4567  0.0  0.0   4060   572 tty2     Ss+  May01   0:00 /sbin/mingetty /dev/tty2
root      4569  0.0  0.0   4060   568 tty3     Ss+  May01   0:00 /sbin/mingetty /dev/tty3
root      4571  0.0  0.0   4060   576 tty4     Ss+  May01   0:00 /sbin/mingetty /dev/tty4
root      4573  0.0  0.0   4060   576 tty5     Ss+  May01   0:00 /sbin/mingetty /dev/tty5
root      4575  0.0  0.0   4060   572 tty6     Ss+  May01   0:00 /sbin/mingetty /dev/tty6

me  17482  0.0  0.0 110236  1136      pts/0    R+   11:36   0:00 ps aux
root     20374  0.0  0.0 108336  1816 pts/0    Ss   May23   0:00 -bash
root     20953  0.0  0.1 161436  1960 pts/0    S    May23   0:00 su - me
me  20954  0.0  0.1 108524  1984      pts/0    S    May23   0:00 -bash

If you want to take away the background child processes then just pipe to uniq:

ps aux | awk '{print $7}' | grep -v "?" | uniq | wc -l

You still have to subtract 1 for the header title of TTY, but you can further improve this by taking the tty's out altogether as it appears you don't care about those anyways.

ps aux | awk '{print $7}' | grep -v "?" | grep -vi "tty*" | uniq

That will give you an accurate count.


Thinking about it more 'ps -a' would work better and you can leave out the first grep.

ps a | awk '{print $2}' | grep -vi "tty*" | uniq | wc -l
share|improve this answer
grep and awk is redundant, use awk '/pattern/; and this doesn't work on my machine - it reports 19 when I have 1. – jasonwryan May 29 '14 at 18:21
I am a bit confused. Right now, I´m on Ubuntu 14. There is only one terminal/bash window open. With the abobe commands, I get 23 or 13, when I should get 1, or 2 in case I open another terminal with ctrl-alt-t – Martincho May 29 '14 at 18:21
That first command counts children processes running in shells in the background and not just open terminal sessions – Jeight May 29 '14 at 18:31
@jasonwryan - It works just fine on mine. Using grep with awk is person preference. If someone is more comfortable doing the whole thing in awk then that's fine. – Jeight May 29 '14 at 18:34
"It works just fine on mine" hardly substantiates your answer... Perhaps you could include some detail of what "yours" is so people are aware that it only applies to that system. – jasonwryan May 29 '14 at 20:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.