The once upon a time Apple System Profiler, now just System Profiler is an amazing CLI (command line interface) tool. If you need some quick information on your computer put into a shell script of some sort, parsing through System Profiler is a very easy way to do it. For your initial reference, you can check the man pages on System Profiler by typing:
man system_profiler
This will give you all the details you’d want to know about passing certain flags. What I want to do is give you a quick and dirty overview with some examples to get you started.
First off, the best way to figure out if the information you are looking for can be obtained through system_profiler is by checking out the GUI System Profiler. You can find this by clicking Apple -> About this Mac -> More Info…
Did you find the information you were looking for? If you did, we can now move to the terminal. You can go ahead and run
system_profiler
on its own. You will get a lot of data… everything that you saw in the GUI System Profiler. However now we can parse.
Let’s say we want to find out the serial number of our mac. Where did we see that on the GUI System Profiler, that’s right, under the Hardware information. A quick and easy way to narrow down quickly to a specific category with CLI System Profiler is to append a data type to the call. To get a list of all supported data types, run the command:
system_profiler -listDataTypes
From here we will see that we want the SPHardwareDataType. To only list the hardware data types, we can run the command:
system_profiler SPHardwareDataType
Note that only a fraction of the information is listed, and it is all related to (or is) the hardware. You should also see that there is a listing of the serial number.
From here for further data manipulation and parsing, there are a variety of tools available which include things such as sed or awk. I, personally, am a fan of awk, so that is what I will demonstrate with. I have found awk to be a very simple and straight-forward way to grab portions of a string. First we will want to only view the line that contains the serial number. This is easily completed with grep.
system_profiler SPHardwareDataType | grep -i 'serial number'
_should return something like: Serial Number: W1234512345_
Now we can throw in some awk commands to only have the actual serial number.
system_profiler SPHardwareDataType | grep -i 'serial number' | awk -F ": " '{printf $NF}'
_should return something like:W1234512345_
You will notice that the result is printed on the same line as your next command line input. This is because we used the awk printf instead of print. It is best to use printf when grabbing data because no end line/carriage returns/”\n” will be returned.
The thing to remember with gathering specific data from CLI System Profiler is to narrow down as far as possible before using grep and awk/sed. This will help save the time during execution.
A few more fun notes about using system_profiler
-
You can save your system_profiler as an XML
system_profiler -xml >MySystem.spx
-
You can change the detail level so no identifying information will be listed
system_profiler -detailLevel mini
-
Like most applications, you can view a list of flags
system_profiler --help
All-in-all, CLI System Profiler can be very useful. Apple developers have already queried the system and hardware for this information, so go ahead and take advantage.