Here are some possible answers, all using the 'open' command-line utility.
The -a option means "open the file argument with the named application":
open -a TextEdit file.txt
The -e option means "open the file argument with the TextEdit application":
open -e file.txt
The -t option means "open the file with the default application for editing text files, as determined via LaunchServices". By default, this will be /Applications/TextEdit.app; however, it's possible for this setting to get overridden:
open -t file.txt
Finally, any file that's of the "text" type will get opened by the application bound to the text type if you just say open file.txt
. You can use the "file" command to reveal what the operating system thinks the file type is: file file.txt
. So, for example, if you renamed "file.txt" to just "textfile" then open textfile
would still open it in the default text-file editing application, as long as file textfile
still thought that "textfile" was actually a text file.
A short 'help' file on open
can be found by running
open --help
Or you can read the whole manual with
man open
The system_profiler command provides a direct answer that’s easily human readable (assuming you are on 10.3 or newer), but you can also use ioreg for the task as it generally completes faster.
system_profiler SPHardwareDataType
is the data type that contains the core hardware information, and you can use grep or awk to pare things down further as needed:
system_profiler SPHardwareDataType | awk '/Serial/ {print $4}'
or
ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'
Both of those commands take between 0.5 and 0.2 seconds to run on modern SSD Macs, so if you want to optimize the command and remove the " you can have your answer in 0.005s or so:
ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}'
Best Answer
The
ioreg
command can be used for this task.Similarly, you can get the same information in a Property List (plist/xml) format by including the
-a
option which is useful for implementations that work with XML better. One command line example would be to usexmllint --xpath
I often see the
system_profiler
command used for this task, however, I have found theioreg
method to be slightly faster if performance is a concern.