W. Welsh 2004 Aug 31
Adopted nearly verbatim from the Linux Knowledge Portal site:
Important and useful commands for newcomers by Martin Sommer (and see also Linux 4 Newcomers)

Important and useful commands for newcomers

Martin Sommer

Index

Introduction

This article describes how to create directories and the different ways to display them. It also explains how to copy and move files and directories. In this context, we will describe a command that allows you to copy files from a workstation to a server and vice versa, which is particularly useful for people operating private homepages. We will then deal with ownership and the allocation of permissions for files and directories. Finally, we will explain how crashed programs (i.e. "hanging" processes) can be terminated without having to reboot the machine.

Handling of Files and Directories

mkdir - make directory
mkdir directoryname creates a new subdirectory in the current directory.

ls - show directory content
ls without any option will show all files (except for the hidden ones, which start with "."), symbolic links and subdirectories in the current directory. [Under linux, different file types, directories, and links are represented with different colors. For example, "normal" files are black, executable ones are red, packed ones are dark red, images are pink, directories blue, and symbolic links black and bold.]
Some important options are -l, -a, and -F. Commands will therefore read "ls -l" and "ls -a", or can be combinated as in "ls -la".

The option "-l" results in the display of a long file list. The option "-a" adds all hidden files to the list, i.e. the ones starting with a dot (.). [In linux, apart from the coloration system,] the object type is indicated by a character appended to the end of the filename. (e.g. "*" for an executable file or "/" for a directory). Here are some samples:


wfw sciences> ls
ELC/                    einstein.quotes         josiah.displ
IRAF/                   far2001.tex.Z           mail/
SKYCALC/                fgs-fold.mag            nsmail/
WWW/                    fgs.mag.clip            smartclass.err.msg
dtu_crdom.readme.txt.Z  info.login              tapedrive.info.Z
wfw sciences>
wfw sciences> ls -a
./                      .netscape/              SKYCALC/
../                     .pine-debug1            WWW/
.TTauthority            .pine-debug2            dtu_crdom.readme.txt.Z
.Xauthority             .pine-debug3            einstein.quotes
.acrorc                 .pine-debug4            far2001.tex.Z
.addressbook            .pinerc                 fgs-fold.mag
.addressbook.lu         .solregis/              fgs.mag.clip
.cshrc                  .ssh/                   info.login
.dt/                    .vacation.dir           josiah.displ
.dtprofile*             .vacation.msg           mail/
.forward                .vacation.msg~          nsmail/
.hotjava/               .vacation.pag           smartclass.err.msg
.jetadmin/              ELC/                    tapedrive.info.Z
.login                  IRAF/
wfw sciences> 
wfw sciences> ls -l
total 62
drwx------   6 wfw           512 Jul  1 13:26 ELC/
drwx------   4 wfw           512 Aug 22 14:01 IRAF/
drwxr-xr-x   2 wfw           512 Nov 22  2000 SKYCALC/
drwxr-xr-x   9 wfw          1024 Jul  1 14:28 WWW/
-rw-r--r--   1 wfw          8723 Oct  6  2000 dtu_crdom.readme.txt.Z
-rw-r--r--   1 wfw           638 Feb  3  2002 einstein.quotes
-rw-------   1 wfw          7347 Oct  2  2001 far2001.tex.Z
-rw-------   1 wfw         22528 Jun 22 22:49 fgs-fold.mag
-rw-------   1 wfw         12152 Jun 30 18:02 fgs.mag.clip
-rw-------   1 wfw          1008 Feb 20  2002 info.login
-rw-------   1 wfw            43 Apr 12 11:08 josiah.displ
drwx------   2 wfw           512 Aug  5 22:12 mail/
drwx------   2 wfw           512 Aug 22 13:36 nsmail/
-rw-------   1 wfw           246 Aug 22 13:55 smartclass.err.msg
-rw-------   1 wfw           483 Dec 18  2000 tapedrive.info.Z
wfw sciences> 

In this last example, ("ls -l"), we have the following from left to right: The first column defines the file type (directory, file or link) and permissions (see below). The second column indicates the number of links to each particular object. Next you can find in a sucession: the file owner, the file group, file size, last change date, and file name.

cd - change directory
cd directory changes to the given directory. You can use both absolute and relative paths. For example, if you are currently in your home directory, the command "cd thesis" moves you to the subdirectory thesis. "cd /iraftmp/iraftmp2/" changes from whereever you are to /iraftmp/iraftmp2/. This is an absolute path. Examples of relative moves look like this: "cd .." moves up one directory level. "cd ../../myfits" moves up two hierarchy levels and then moves to subdirectory myfits. Just entering "cd" brings you back to your home directory regardless of your position in the directory tree. "cd ~" produces the same effect.

cp - copy files and directories
cp source target copies files from a source place to a target place. This can be done with a new name in the same directory or with any name in another directory. It is also possible to introduce both absolute and relative path names. The most important option is surely -R. It allows to recursively copy, i.e. with all subdirectories and their contents.

mv - move or rename files and directories
mv source target works in the same way as the copy command (cp). However, it does not create any copies, i.e. the file appears in the destination but the original file is deleted. Renaming within the same directory just "moves" a file within a directory to another file of same content but different name.

rm - delete files
rm ("remove") is used in the same way as the DOS del command ("delete"). There is a crucial difference as compared to DOS based systems: deleting under Linux cannot be undone (due to security reasons). This means that there is no undelete, unrm or similar. Therefore, you must be very careful when using this command. You can also apply the recursive function to cp by using the option -R. The option -r can be alternatively used. [It is strongly advised to alias the rm command to rm -i in your .login so that you cannot accidentally delete all your files! Likewise for "cp".]

rmdir - delete directory
rmdir directory removes the given directory.

Copy between workstations and/or servers

scp - secure copy
scp source destination

Some Unix/Linux commands have safe ways to accomplish the transfer of data between various computers. This is especially useful in the server field, particularly when the accessed servers are attached to the Internet and therefore exposed to multiple attack possibilities. You can usually retrieve data through the well-known ftp protocol; scp represents a considerably safer variation of it. You can both up and download data via scp instead of ftp. Note that you need to include the full computer address name, e.g. scp myfile_on_laptop mintaka.sdsu.edu:~/thesis/myMLOdata/.

Ownership and Permissions

chmod - change mode
The chmod ("change mode") command changes permissions for files and directories. If you have a Windows background, you should first know something about Unix permissions. As we mentioned above, all files and directories belong to an owner, and this in turn belongs to a group. Permissions must be separately defined for the owner, for the group and for all others. An object can be read, written, and executed by these three (owner, group and all others). Now it's turn to explain the somewhat "cryptic" characters from the ls -l output (see above). Just create the subdirectory images in your home directory by entering mkdir images. Then use ls -l to display its contents. The output line for this directory will be something like:

	
  drwxr-xr-x    2 bobby  user           35 Jul 16 16:00 images/

Let's first consider the initial sequence of characters. The first character implies that we are dealing with a directory (d = directory). The nine following characters are essential because they inform us about permissions. The first three characters represent the owner, the next three the group and the last three all others. r stands for readable, w for writable and x for executable. Program files and directories must be executable. The sequence rwxr-xr-x thus means that bobby, as directory's owner, can read, write and execute it. The rest (group and all others) can read and execute it. In the case of directories, execute means that you can change to them. Defining permissions can be done in two different ways: with flags for the various users and permissions, or by means of the so-called octal number.

Symbols: The symbols u, g and a stand for "user", "group" and "all others", and r, w, and x stand for read, write, and executable. Granting and deleting permissions can be done with plus (+) and minus (-). So, if the file bob.pl must be readable for all, the corresponding command reads:

  chmod a+r bob.pl

Please notice that by granting permissions for all others (a), the respective flags will be set not only for a, but also for u and g, i.e. all three positions. However, granting permissions to u or g will only set the flag for the first or second position. When granting permissions to a, the a can be omitted in the command:

  chmod +r bob.pl

Octal number: This procedure is more elegant because you can set all permission flags for the three groups at the same time. It works by adding a three-digit number to chmod, where the first digit stands for the user (u), the second for the group (g) and the third for all (a). Number 4 corresponds to the flag "read", number 2 for "write" and 1 for "execute". When setting several permission flags, the corresponding numbers will be added to each position. For example, we want to set the following permission flags for the file bob.pl: The user must be able to read, write, and execute. The group must be able to read and execute and the rest shall have "read only" permission. We will then assign 4, 2, and 1 to the user, 4 and 1 to the group (read and execute) and just 4 to all others (read only). The command will read:

  chmod 754 bob.pl

Process Termination

killall
You will surely be familiar with the following Windows problem: a program hangs and "crashes", and your only option is to reboot the computer. Crashes rarely happens in Linux, but they can occur nonetheless. Beta versions, which are not completely error-free, are particularly prone. What actually hardly ever happens in Linux is having to reboot the computer. You have the possibility to terminate (close) the hanging program. Both killall and kill commands can be used for this purpose. When working on an X-Windows interface, all programs (except for the crashed ones) can usually be started. That is to say, you open a console and can terminate the hanging program there. If e.g. Netscape hangs, you can terminate it (together with all programs whose name contains the word netscape) by entering the command:
  killall netscape 

After such a "rough" process termination, a so-called "lockfile" is often created. In the case of Netscape, it can be found in directory ~/.netscape and is plainly called lock. If a program cannot be restarted after having been terminated (this often happens with Netscape), this lockfile must be first deleted. In case killall does not seem to work, the option -9, which is actually rougher than the command itself, must be used.

kill

kill works in the same way as killall. The only differece is that not the program's name, but the program's number, the so-called PID, must be given. An individual process number is assigned to every program or command that is started either by booting or by the user. The ps command shows all running processes. In order to display all started processes ps -ax must be executed. If you now want to know e.g. Netscape's process number, you can scroll all processes or, more elegantly, pipe the output of ps -aux into the command grep to filter the Netscape process. The elegant command thus reads:

  ps -aux | grep netscape 
The output looks approximately like this:

  hansi  11779  0.0  5.1 22284 13120 ?       S    11:58   0:01 /opt/netscape/netscape
  hansi  11968  0.0  0.2  1676  708 pts/0    S    12:53   0:00 grep netscape	

In the first place you can see the user who has started the process, and after it, the process number (PID). We can see two processes above because grep has searched for the string "netscape" and the grep commmand is also a process with an assigned number. Netscape will be terminated by entering the command kill with the process PID, which in our example is:

  kill 11779

For persistent cases the option -9 can also be used:
kill -9 11779.