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 need to be able to record the mouse movements every so often (every .2 of a second for example) and have them in a coordinate representation instead of a diff.

I found the following script:

while :
cat /dev/input/mice | read -n 1
sleep 1

But it doesn't seem to print anything to the terminal (or perhaps it's all gibberish). Other discussions suggest that /dev/input/mice is deprecated. On top of that, /dev/input/mice wouldn't actually have the data in a friendly format.

Am I going to have to do the conversion manually (from the format in the /dev/input files), or is there an API for this?

share|improve this question
up vote 18 down vote accepted

Try the following command :

xdotool getmouselocation 2>&1 |
    sed -rn '${s/x:([0-9]+) y:([0-9]+) .*/\1 \2/p}'

See http://www.semicomplete.com/projects/xdotool/

share|improve this answer
Edited to display only x & y values without noise. – Gilles Quenot Sep 30 '12 at 21:07
You are a hero. – anonymous Sep 30 '12 at 21:08
=) thanks, you're welcome. – Gilles Quenot Sep 30 '12 at 21:11
Removed not needed g modifier in sed command. – Gilles Quenot Oct 1 '12 at 13:06

One more option is xinput. For instance, xinput test 8 would write

motion a[0]=496 a[1]=830 
motion a[0]=496 a[1]=829 
motion a[0]=496 a[1]=832 
motion a[0]=496 a[1]=834 

upon mouse movement, where "8" is my mouse device number. Use xinput --list to find out the number of your mouse among devices.

share|improve this answer

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.