command structure
This subchapter looks at simple UNIX/Linux commands to get you started with using the shell.
You will learn the basic format or structure of a shell command.
You can run a UNIX (or Linux or Mac OS X) command (also called a tool) by typing its name and then the ENTER or RETURN key.
All commands (and file names) are case senitive. ls is a valid command, while LS is unrecognized.
commands and utilities
A quick note on the difference between commands and utilities.
A utility is a program. who, date, and ls are examples of utility programs.
A command is all of the text typed at the command line, the utility name and all of the flags, switches, file names, and other text.
This subtle distinction between utilities and commands is generally ignored and the two terms are often used interchangeably. As I type this, I am pretty sure that I have ignored this distinction elsewhere in this book and dread double-checking every use. You can probably use the terms interchangeably except in cases like tests, papers, or books.
The kernel is loaded from hard drive (or other storage) when the computer is started and remains running until the computer is turned-off or crashes.
Utilities are also stored on hard drives, but are only loaded into main memory when used.
single command
The most simple form of a UNIX command is just a single command by itself. Many UNIX/Linux commands will do useful work with just the command by itself. The next examples show two commands (who and date) by themselves.
The output generated by a single command by itself is called the default behavior of that command.
who
The who command will tell you all of the users who are currently logged into a computer. This is not particularly informative on a personal computer where you are the only person using the computer, but it can be useful on a server or a large computing system.
Type who followed by the ENTER or RETURN key.
$ who
admin console Aug 24 18:47
admin ttys000 Aug 24 20:09
$
The format is the login name of the user, followed by the users terminal port, followed by the month, day, and time of login.
failed command
The shell will let you know when you have typed something it doesnt understand.
Type asdf and then the ENTER or RETURN key. You should see a message similar to the following:
$ asdf
-bash: asdf: command not found
$
date
The following example uses the date command or tool.
$ date
Wed Nov 10 18:08:33 PST 2010
$
The format is: day of the week, month, day of the month, 24 hour time, time zone, year.
options, switches, or flags
A command may optionally be followed by one or more options. The options are also commonly called flags or switches.
Options are usually a single character. Usually the option is preceded by a minus sign or hyphen character.
See the following example.
universal time
Adding the -u flag to the command date will cause it to report the time and date in UTC (Coordinated Universal) time (also known as Zulu Time and formerly known as Greenwich Mean Time or GMT). The seconds are slightly higher in the second example because of the passage of time.
$ date
Sat Aug 25 19:09:19 PDT 2012
$
$ date -u
Sun Aug 26 02:09:27 UTC 2012
$
option grouping
You can combine multiple options for a single command.
The following example shows the ls command by itself and with two options, first individually, and then combined.
$ ls
Desktop Downloads Movies Pictures scripts
Documents Library Music Public
$ ls -F
Desktop/ Downloads/ Movies/ Pictures/ scripts/
Documents/ Library/ Music/ Public/
$ ls -a
. .cups Library scripts
.. .vminfo Movies
.DS_Store Desktop Music
.Trash Documents Pictures
-bash_history Downloads Public
$ ls -a -F
./ .cups/ Library/ scripts
../ .vminfo Movies/
.DS_Store Desktop/ Music/
.Trash/ Documents/ Pictures/
-bash_history Downloads/ Public/
$
You can group option by listing them in any order (with a few rare exceptions) and either with their own minus sign or after a single minus sign. All of the following versions of the ls command work the same way:
$ ls -a -F
$ ls -F -a
$ ls -aF
$ ls -Fa
$
arguments
Commands may also optionally have arguments. The most common arguments are the names of files.
Technically, the options just mentioned are also arguments, but in common practice options are separated from other arguments in discussions. Also, technically, the operators mentiond below are also arguments, but again it is useful to separate operators from other arguments in discussions.
The following shows the difference between the default behavior (no arguments) of who and a version with arguments who am i.
The default behavior of who lists all users on the computer.
$ who
admin console Aug 24 18:47
admin ttys000 Aug 24 20:09
$
The use of the arguments with who am i causes the command to only lists the one user who typed the command.
The command is who and the arguments are am i.
$ who am i
admin ttys000 Aug 25 17:30
$
one argument
In an upcoming subchapter on the cat you will use the command cat with the file name file01.txt to confirm that you correctly entered your sample file.
just observe this example
do not type this into your shell
$ cat file01.txt
This is a line of text in my first file.
This is another line.
To be, or not to be: that is the question:
1234567890
ABC
XYZ
abc
xyz
$
You may download this sample file from http://www.osdata.com/programming/shell/file01.txt.
two arguments
In the upcoming subchapter on the cat you will use the command cat with two file names (file01.txt and numberfile.txt) to confirm that you correctly entered two of your sample files.
just observe this example
do not type this into your shell
$ cat file01.txt numberfile.txt
This is a line of text in my first file.
This is another line.
To be, or not to be: that is the question:
1234567890
ABC
XYZ
abc
xyz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$
You may download the second sample file from http://www.osdata.com/programming/shell/numberfile.txt.
options and arguments
You can combine options and arguments on the same command line.
The following example uses the command cat with the -b option and the file name file01.txt.
just observe this example
do not type this into your shell
$ cat -b file01.txt
1 This is a line of text in my first file.
2 This is another line.
3 To be, or not to be: that is the question:
4 1234567890
5 ABC
6 XYZ
7 abc
8 xyz
$
operators and special characters
Command lines may include optional operators or other special characters.
In an upcoming subchapter on the cat you will use the command cat with the operator > and the file name file01.txt to enter your sample file.
just observe this example
do not type this into your shell
$ cat > file01
If you accidentally typed in the above command, hold down the CONTROL key and the D key at the same time to return to your shell.
comments, suggestions, corrections, criticisms
free music player coding example
Coding example: I am making heavily documented and explained open source code for a method to play music for free almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).
View music player in action: www.musicinpublic.com/.
Create your own copy from the original source code/ (presented for learning programming).
Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.
Names and logos of various OSs are trademarks of their respective owners.