User Commands
It would take ridiculously long to try and explain to a new user
all the wonderful things one can do on such a system.
Since 1970, unix systems have evolved their set of commands, some
taken from other systems of the time, and some new. In spite
of moderate variations in syntax there are many commands that
can be expected to exist and to function in much the same way
on any modern unix system (freebsd, openbsd, netbsd, linux,
solaris, hp-ux, aix etc.).
Indeed, the IEEE "POSIX" (portable
operating system) standard defines quite a few, clearly having
tried to standardise unix practice.
In a linux based system, and in many others, the
GNU utilities
are used for the common commands.
{ GNU (stands for "GNU is not unix" !) has since the early 1980s
developed "Free Software" (freedom of use, not necessarily
of cost) aiming to provide all the programs needed for computer
work.
}
The things that can be automated when you have a good knowledge
of these commands are amazing. The shell (commandline interpreter)
itself does very clever things.
Try searching on google or similar, with such terms as "unix command"
"linux command" if you want a "cheat-sheet" or tutorial.
Please bear in mind how most so called "linux" commands are in fact
GNU programs that just happen to be very often used with linux
(linux is the kernel rather than the things you directly interact
with). So you are likely to have the same programs available on many
other unixish systems, or else very similar (usually somewhat inferior)
commands.
To get more information about a command, there are various things
you can do:
- type the command, followed by -h or --help
-- many commands then display a list of options
- use the "man" or "info" commands to view the manual or info page
e.g. "man ls" or "info ls" will give details for ls (list)
The "man" (manual) system is the traditional unix way. Most commands
and many configuration files and C functions have their own pages.
Use the space-bar to move down, or Ctrl-D and
Ctrl-B for down and up half pages --- page up and page down keys also
work on most systems. Just press q to exit the page.
The "info" system is the GNU one. It uses links (denoted by stars)
to split the information by menus. If you type "info" by itself,
the top level willbe displayed, allowing you to see all the GNU
programs and what they do. Note that there exist many other non-GNU
progams that won't show up here.
The virtue of info is most seen
when dealing with complex programs, for example the shell "bash".
This comes out as >110 pages when printed on A4 in 10pt text, so
having it all as one long manual page is not very helpful for searching!
As a basic introduction to options and manuals, note that options
to commands are normally prefixed with "-", and that manual
descriptions of command usage traditionally uses [] around
optional fields, <> around obligatory fields, and | to denote
alternatives.
For example, command [-a|-b] <arg1> <arg2> [arg3] ...
suggests that either (or presumably both) of the -a and -b
options can be given, then two required arguments, then any
(integer, non-negative) number of additional arguments.
Some basic commands -- see the man or info for more details of them:
- cp copy a file
- cp -R copy recursively (sub-files too)
- cp -a copy for archive -- preserve original dates etc.
- mv move a file or directory
- rm remove a file
- rm -r recursively remove directory and contents
- rm -rf as above, but without asking for confirmation...
- mkdir make a new directory (name must be given)
- rmdir remove a (empty) directory
- find find files of certain name, age, owner, etc.
- ps list running processes on the current terminal
- ps auxf list all processes, with user name and tree-structure
- kill kill process by number (found from ps)
- killall kill all processes with a certain name, e
e.g. killall -9 MATLAB
-
- env see the environment variables of your shell
- tar (short for "tape archive") -- make an archive
- gzip compress a file
- gunzip decompress a file
- vi edit a text file (Esc then :q to exit,
Esc then :wq to save and exit)
- nano edit a text file (the traditional version is "pico")
-
- passwd change your password (it's fussy about what you choose)
-
- df fullness of disk partitions
- du space taken on disk by files
- free details of memory usage
Lots of useful compound commands can be made.
For example, with the help of the
imagemagick "convert" command (NOT a standard unix command, but
installed here) and the bash shell's syntax and the basename command,
the following would copy all files called *.bmp in the current
directory (we assume they are bitmap images) to jpeg images of
the same base name but .jpg extension, and resized by 50% of the
pixel-dimensions:
for f in *.bmp ; do convert -resize 50% $f `basename $f .bmp`.jpg ; done
(If the names contain silly characters such as space, ' and so on, some more
characters may needed in the above line, to stop these characters being
misinterpreted.)
Commands such as grep, sed, awk, perl (sed, awk, and especially perl are
really programming languages in themselves!) can do clever things with
text.
Use of input and output redirection (<, >) and of "pipes" (|) in the shell
allows data to be streamed from and to files or the keyboard/screen, and
through various programs in turn. For example, the following line
takes the contents of the passwd file (list of users), extracts only
the lines containing "10100" (on this system, this suggests the user is
in the "magnet" group), then cuts out just the first of the colon-separated
fields (which is the username), and splits (tees) the result so that it
is written to the screen as well as to the file "users" in the current
directory:
cat /etc/passwd | grep 10100 | cut -d \: -f 1 | tee users
to write to the file without going to the screen, and to give full names
instead of login names, this would be:
cat /etc/passwd | grep 10100 | cut -d \: -f 5 > users
And so on ...
To add your own programs so they can be accessed directly by name,
put them in your home directory in a subdirectory called "bin".
On this system, ~/bin is at the beginning of the PATH variable (the list of
directories searched for a program name).