Tuesday, February 24, 2009

Fundamental Unix Commands


So now you're logged in. Great. Now what? Unix does not present itself to you through an interface that is graphical (GUI). You know, the screen full of icons and pictorial symbols and menus for you to interact with using your mouse. While using linux on the remote server, you won't even touch your mouse. I'm not bragging, mind you, I'm not saying that's good. Just pointing it out. What the operating system does present is merely a prompt. A symbol that mutely stares at you waiting. For anything to happen, you're supposed to type in a command. In its language, not yours. So you have to know one. That requires you to learn the commands that Unix has. As opposed to a graphical interface, this style of interaction is called a command-driven or character-based interface.

By the way, note I didn't say that Unix does not have a graphical user interface (because it does). I only said it doesn't present itself to you that way. Unix has an optional GUI, called the X windowing system, that can be started by issuing a certain command while in the character-based interface. But not while using telnet or ssh. So in this course, primary focus is on Unix used through its command-driven interface.

The point is that to get beyond the prompt, you have to start learning some commands. Right now. So you won't be stuck. Using Unix effectively in command mode means mastering the use of certain commands.

There are a whole lot of commands. Every beginning Unix tutorial seems to pick its own shortlist. The ones it thinks are the top priority. A kind of "top 10." While no particular pick is sacrosanct, it's not too hard for experienced Unix users to agree on certain high-frequency commands you should know.

What does "know" mean? How deeply? Commands generally have a lot of formal "options." These appear in their documentation. As an example, the ls command is for the purpose of listing the contents of a directory. At its simplest people use it by typing "ls, " nothing more, and pressing the enter key. A list of filenames typically appears (try it). However, ls has an option called -l (the letter ell) which prints out a little more information about each file, in a different format. And another, the -t option, that sorts the file list into chronological order. And forty-eight other options that appear in the documentation for the ls command. You don't need to know them all. But among them there are just a few that make life easier if you know them (or harder if you don't). And it's the same for the other commands.

I want to call to your attention a list of arguably "most important" commands. And for each, to its "most important" options. And I want you to familiarize yourself with each command option through actual use. Here's my list:

ls - list directory contents (like MS-DOS dir)
cat - send file content to screen (like MS-DOS type)
cd - change current directory (like MS-DOS cd)
chmod - change file permissions
cp - copy files and directories (like MS-DOS copy)
echo - write characters to the screen
find - find files (slow but fresh)
locate - find files (faster but stale)
grep - print lines matching a pattern
less - file filter for viewing
man - display on-line manual pages for individual commands
mkdir - make directories (like MS-DOS md)
mv - rename/move files (like MS-DOS ren or move)
ps - give a process status report
pwd - print name of the current, working directory
rm - remove files and directories (like MS-DOS del)
rmdir - remove empty directories (like MS-DOS rd)


Here are the several variations of each command you should become familiar with:

ls
list directory contents

[david@EMACH2 fish]$ ls
salmon trout

ls -l
long listing

[david@EMACH2 fish]$ ls -l
total 8
-rw-r--r-- 1 david david 7 Jan 9 2000 salmon
-rw-r--r-- 1 david david 6 Jan 9 2000 trout

ls -F
displays a slash after each directory, asterisk after each executable file, at sign after symbolic links

ls -a
displays all files, including invisible ones (those whose filenames begin with a period)

ls -t
displays files in order by the time of last modification

ls -R
recursively lists subdirectories

[david@EMACH2 animal]$ ls -R
.:
amphibian bird fish insect mammal reptile

amphibian:
frog newt

bird:
crow robin

fish:
salmon trout

insect:
ant mosquito

mammal:
cat horse

reptile:
lizard turtle

cd
makes your home directory the current, working directory (see pwd)

[david@EMACH2 log]$ pwd
/var/log
[david@EMACH2 log]$ cd
[david@EMACH2 david]$ pwd
/home/david

cd
makes the specified directory the current, working directory

[david@EMACH2 david]$ cd /usr/src/linux
[david@EMACH2 linux]$ pwd
/usr/src/linux

cd
makes the specified directory, which must be under the initial working directory, the new working directory

[david@EMACH2 taxonomy]$ pwd
/home/david/taxonomy
david@EMACH2 taxonomy]$ ls -F
animal/ mineral/ vegetable/
[david@EMACH2 taxonomy]$ cd mineral/
[david@EMACH2 mineral]$ pwd
/home/david/taxonomy/mineral

cp
creates a copy of the source file, applying the given destination file name to it

cp
creates a copy of the source file, without changing its name, in the given destination directory

cp -r
recursive. If any of the source files is a directory, all its contents plus those of any of its subdirectories are copied into the given destination directory

cp -p
preserves each file's characteristics when copying it (owner, group, permissions, modifications times)

mv Afile B
renames a file (Afile is an existing file, B is a new name for it)

mv Xdir Y
renames a directory (Xdir is an existing directory, Y is a new name for it)

mv Afile Xdir
moves a file into a specified directory (Afile is an existing file, Xdir is an existing directory)

rm -i
removes files, interactively (asks before removing each file)

rm -f
removes files with "force" (doesn't ask)

rm -r
removes files recursively, including those found in all subdirectories of the one specified (or current working directory if none specified)

rm -rf *
removes all files in the current and subordinate directories, and also those directories. Potentially dangerous if executed from within the wrong directory.

cat Afile Bfile > Cfile
concatenate (paste together end to end) contents of two files, send result (>) to a new file (result: C = A + B)

cat /etc/lilo.conf
extract contents of the file, send result to the terminal

cat -n
number the lines as they are output

cat --show-all
show non-printing as well as printing characters (e.g., tabs, linefeeds)

man
display documentation for the named command, explaining all its options just as we're doing here

less
prints contents of a file (or other input) one screenful at a time to prevent scrolling out of sight. Press enter to advance a line, spacebar to advance a full screen, : q to exit.

grep
searches for a pattern in files. Frequently used to print all lines in file containing a certain word or phrase

| grep
searches the output produced by a command (in place of the contents of a file) for a pattern

grep -v
reverses search outcome, searching for lines the do NOT contain the target pattern

ps
process status. Displays status information about active processes that your terminal controls including PID (process id) numbers

[david@EMACH2 david]$ ps
PID TTY TIME CMD
1235 pts/0 00:00:00 bash
1820 pts/0 00:00:00 ps

ps ax
displays status information about all processes

chmod u+r
add to or remove from (+ or -) a file(s) any of 3 different kinds of permission (r or w or x) for any of 3 different user constituencies (u or g or o)
used to restrict/extend access to others, and to enable script execution

chmod 777
same, simultaneously setting multiple permissions using a numeric notation

locate
search for files on the computer (fast and stale)

find / -name -print
search for files on the computer (slow and fresh)

Note that there are a number of different kinds of "Unix." And there are some command differences among them. They have the great majority of the commands in common, but sometimes a command with which you are familiar in one version of Unix isn't included in another one. One of the kinds of Unix that is widely popular is called FreeBSD. The one used on the remote server is Linux. Are such different versions the same?? They are like different dialects of the same language (English in New Zealand, Canada, Scotland, South Africa, ...). There is both a little difference and a lot of similarity. But a lot more same than different.

Below are some links to other peoples' "top 10" command sets, and a linux command "cheatsheet" you might print out. Spend some time exploring them.

One

... and another

Linux command cheatsheet:
page1 page 2

Search

My Blog List