Chapter 4: Commands and Programs
4.1 Command basics
Issuing commands always takes the same form:
command
… then options
… then arguments
echo 'hello world'
echo
is actually a C program. UNIX is written in C. bin
is where most of the C programs are stored. The program echo
is stored there. When you type echo
into terminal you are just called the compiled program called "echo" which is stored in bin
.
If UNIX didn't know where the executable C program called "echo" was located you could explicitly tell it:
bin/echo 'hello world'
to find the location of a program file use which
which echo
or whereis
whereis echo
for info about a program use info
info echo
4.2 Common options for most programs
Most unix programs respond to these options:
-v
or --version
to get the version of the program
--help
to read about how to use the program
for example:
npm --version
4.3 Quitting a program
There are several options for quitting programs:
q
, or x
, or Control + q
, or Control + x
, or ESC
, or force quit
, or Control + c
,...
With some of these options, if you close the window the process may keep running so to be safe, use:
Control + c
4.4 The PATH variable
In UNIX for MacOS most programs are located at /bin/
. UNIX will look there for executable programs.
However, an executable program can be placed anywhere. But how will UNIX find an executable program if it is just placed ni any old folder?
UNIX finds program by looking through the directories assigned to the $PATH variable
echo $PATH
returns something like (depending on your setup):
/Users/yourname/.composer/vendor/bin:~/.composer/vendor/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
This is a list of colon separated paths where unix will look for executables. Its that simple.
If you want UNIX to find an executable you can add the path to that executable to the $PATH
variable.
Early entries take precedence over later entries. So if you have two executables of the same name UNIX will use the first one it finds.
In BASH to set the path just type
PATH=path:path:etc...
Setting the $PATH
variable in this way (by typing it into terminal) only lasts for the current session.
To set the $PATH
variable permanently, edit the .bash
file directly which sets the $PATH
variable each time UNIX boots up.
Note that when you use whereis
to find an executable it may return a different copy of that executable than the one that is in the path and being used by UNIX. This causes some degree of pain...
4.5 System information commands
4.5.1 date
date
Returns the date in the default format unless you have changed the format in a .bash
file.
4.5.2 uptime
uptime
Returns: 18:18 up 2:18, 3 users, load averages: 1.63 1.58 1.60
The number of users reflects the number of processes connected to UNIX. In this cae there are 3. The MacOS operating system Finder program is one, and one each for the 2 terminal windows which were open when I ran the command uptime
4.5.3 users
users
Returns username
4.5.4 who
who
Returns each instance of a user connecting to unix!
Do you multiple instances of your user name listed?
smerth console Mar 30 11:58 smerth ttys001 Mar 30 13:19
Username is using unix as shell (one for every open terminal window), and Username is also logged into unix as the MacOS finder.
4.5.5 uname
uname
Returns Darwin - the unix name
uname -mnrsvp
Returns: Darwin USER's-MacBook-Pro.local 15.5.0 Darwin Kernel Version 15.5.0: Tue Apr 19 18:36:36 PDT 2016; root:xnu-3328.51.21~8/RELEASE_X86_64 x86_64 i386
uname -ap
Returns same...
4.5.6 hostname
hostname
Returns localhost
4.5.7 domainname
domainname
Returns the domain name on a server
4.6 Disk information commands
4.6.1 df - disc freespace
To humanize the output use -h
df -h
Disc size is calculated using base2. To get disc size calculated using base10 use:
df -H
4.6.2 du - disc usage
du path
Gives the size of every file found at a path. Its important to restrict du
to a path otherwise the burden of calculating the disc size for the whole disc is onerous.
options | |
---|---|
-d | To show directories |
-a | For "all" - to show directories and files |
-d | depth |
4.6.2.1 example usage
du -ah path
When looking at directories you can specify depth...
du -hd 1
which shows the directories to a depth on one
n.b. ls
reports the actual size of files, du
reports the size of the blocks allocated in memory for the files.
4.7 Viewing processes
The kernal is UNIX. The shell is a text based UI. BASH is the flavor of shell most often used in MacOS to interact with UNIX.
The kernal sets aside memory, and starts a process, then outputs from the process if there is any output from that process, then stops the process when it is complete, then frees the memory it has set aside.
You can check the status of all the processes UNIX is running:
4.7.1 ps - process status
ps
Shows processes owned by user and in user's control (not background and not owned by others like the "root" user.
ps -a
shows all processes
people most commonly use:
ps aux
There is no dash in front of the options aux
because it is an artifact of backwards compatibility with older UNIX versions.
4.8 Monitoring processes
4.8.1 top
Get a quick overview
top
options | |
---|---|
q | to exit out (quit) |
-n | specify the number of lines to show |
-o | sort order |
-o cpu | sort by cpu usage |
-s | refresh rate in seconds |
-U | filter by user |
4.8.1.1 examples:
Show 10 lines:
top -n 10
Set refresh rate set to 3 seconds:
top -s 3
Get more help for top
top ?
4.9 Stopping processes
Ctl+C
is the best way to stop a process. But for some processes you must use kill them.
To kill a process you need the pid (process id)
kill -9 pid
-9 pid
means: "Hey, UNIX, really, I know best, kill process with id=9"
4.10 Text file helpers
4.10.1 wc - word count
wc filename
returns:
- no. of lines
- no. of words
- no. of characters
- filename
"lines" are not lines on the screen, they are defined by line-breaks
4.10.2 sort
sort filename
sort
operates on each line in a text file. It will sort the lines according to the first letter of each line and return the text to the terminal window.
Capital and lowercase letters are treated differently by sort
. To sort alphabetically, independent of case, pass in the -f option
sort -f filename
For a reverse sort pass in the -r option.
4.10.3 uniq
uniq
operates on the lines in a text file. It filters in/out repeated lines. It doesn't find duplicates in the list of lines. It finds duplicated lines - one immediately following the next.
It asks: "is this line the same as the next line?"
uniq -d
Returns the line that are repeated
uniq -u
Returns the unduplicated lines
4.11 Utility programs
4.11.1 cal / ncal - calendar
cal 3 1964
Returns the calendar for Mar 1964.
cal -y 1964
Returns a full year.
ncal
Rotates the calendar with the days running down the left side.
4.11.2 bc - calculator
Its a calculator! Just run bc
and the program executes, then on the command line enter your calculation, for eg: 88 * 10
Or,
echo "2+5" | bc
4.11.3 expr - expression evaluator
expr 5 + 3
You need to have spaces between arguments expr 1+1
won't work but expr 1 + 1
works.
And you must escape special characters!
4.11.4 units - unit converter
units
will convert from the first argument to the second.
A shortcut for entering the conversion is to put arguments into quotes on the command line:
units "190 degF" "degC"
4.12 History - working with history
The history of your interaction with bash in terminal is tracked in a file in the user driectory.
Bash writes to this history log when it quits so the current session's commands are not reflected (as you might expect...)
Bash holds the current sessions commands in memory until it quits then writes them to the file.
history
prints a list of previously executed command to the terminal - now you can use the numbers beside a command to re-issue that command instead of scrolling up with the up-arrow
!504
executes command numbered 504 in the history list
!-2
executes the command 2 back in the current history
!some alpha numeric string
executes the last command matching the string
!nano
runs the last nano you executed
!!
runs the last command
sudo !!
pulls up the last command prepended with sudo
!$
references the arguments from the last command. For example first run:
nano textfile.txt
and now:
cat !$
will open the file you nano-ed, for you to check the changes, in cat - without having the type the filename again
Delete lines from the history with -d
history -d 203
deletes line 203