Programatically determine if an script is being executed on laptop or desktop?

Looking at whether or not the system has a battery is not reliable - a UPS connected to the system (not just for power, but over USB as well for automatic shutdown and battery monitoring) may show up as a battery.

There is a nice reliable way however:

dmidecode --string chassis-type

On a laptop, this will return one of "Laptop", "Notebook" "Portable", or "Sub Notebook" (depending on what the manufacturer coded into the BIOS). There is a full list of possible values at "Identifying the Chassis Type of a Computer" in the Windows 2000 Scripting Guide - don't worry about it being a Microsoft TechNet page, this is not OS specific.

dmidecode can also get information about the hardware manufacturer, system serial number (sometimes), etc.


To avoid using sudo, you can read the contents of /sys/class/dmi/id/chassis_type. It appears conform to the following table:

  • 1 Other
  • 2 Unknown
  • 3 Desktop
  • 4 Low Profile Desktop
  • 5 Pizza Box
  • 6 Mini Tower
  • 7 Tower
  • 8 Portable
  • 9 Laptop
  • 10 Notebook
  • 11 Hand Held
  • 12 Docking Station
  • 13 All in One
  • 14 Sub Notebook
  • 15 Space-Saving
  • 16 Lunch Box
  • 17 Main System Chassis
  • 18 Expansion Chassis
  • 19 SubChassis
  • 20 Bus Expansion Chassis
  • 21 Peripheral Chassis
  • 22 Storage Chassis
  • 23 Rack Mount Chassis
  • 24 Sealed-Case PC

Debian Solution:

To find whether a machine running Debian is a laptop, try:

[ -d /sys/module/battery ] && echo "Yes it's a laptop"

This approach does not require root privileges.

On other distributions, however, this directory seems to exist, at least in skeleton form, regardless of whether or not there is a battery. From the comments (below), these distributions include CentOS, Ubuntu, and the Ubuntu-derived distribution of Linux Mint.

More General Solution

Although it does not work on my Debian systems, the solution proposed by Alex reportedly works on Ubuntu & CentOS. Thus suggests, for greater generality, a possible combined solution:

[ -f /sys/module/battery/initstate ] || [ -d /proc/acpi/battery/BAT0 ] && echo "Yes it's a laptop"

This approach does not require root privileges.

More Details

On a Debian system with an actual battery, the /sys/module/battery directory contains many files. One such file is /sys/module/battery/initstate which contains the text live. On Ubuntu, however, these files do not exist even on actual laptops. Thus, it appears that the presence of the file /sys/module/battery/initstate can be used to test for a laptop running Debian.

On Debian systems that I tested, by contrast, the /proc/acpi/battery directory did not exist.