|
|
This document is available in: English Castellano Deutsch Francais Nederlands Turkce Korean |
by Manuel Muriel Cordero <murie/at/arturo.lsi.us.es> About the author: Manuel Muriel is a student of Computer Science Engineering of Universidad de Sevilla. I have comput er science as my main hobby and area of study. Linux was a big discovery to me. Besides this I have a big affection for reading, specially fictio n (Tolkien is my idol :), play basketball with my friends, and movies, bet ter if it's a SF movie (I love Blade Runner, 2001, Forbiden Planet, ... Content: |
Basic UNIX commandsAbstract:
Long time Linux users may find it a very simple and powerfull
operating system, but the truth is that for a novice user, not
very familiar with the UNIX environment, Linux appears at first
arcane and hard to learn. The author gives us a very concise
primer on some basic UNIX commands that will hopefully make the
learning curve of novice users less steep.
|
This article is specially dedicated to new users of Linux (and any UNIX as well). It can be also a useful review for more experience users.
Perhaps some of our readers basically use their systems at home as a word processor, video game and perhaps to run personal accounting software. For this home user terms like "operating system" may not have a very clear meaning because they got their systems with Windows 95 for "free" (so to say), most likely they have other applications "borrowed" from a friend or a friend of a friend. They wish the computer does it job without much trouble and become annoyed when the computer spits out system critical messages like "Memory Protection Error in 0FAG:BD07". These errors appear to be totally meaningless .
One day you hear about a free operating system that does not fail, called "Linux", and you decide to try it buying a specialized computer magazine that comes with a CD of this system.
After a complicated installation process in which the hard disk had to be partitioned, a step not like very much, you probably kept a large partition for Windows and a tinny one for Linux. Rebooting under the new operating system you get something like:
LILO:
This is time to type in the name assigned during the installation to the Linux boot partition. Then follow a number of screens filled with arcane messages and finally a login prompt:
Red Hat linux 2.0.32
darkstar login:
Now you need to enter "root" followed by the password specified during the installation.
After this you find something like:
root@darkstar:~#
Now what ?
After you have seen this screen you'll ask yourself about the benefit of so much effort. At first Linux doesn't present user with a flashy windows environment nor any GUI interface. The quickest route to the "native windows environment" for Linux is through the command: startx
I am assuming now that during installation the video card parameters were set correctly. If not, startx is likely to produce an error.
The next step is to do something productive of all this effort. Readers may try to run some of the utilities in the StarOffice suite. It is an office suite like that of Microsoft with multiple utilities. Several articles in this magazine will explain you how to install StarOffice and how it works (text processor, presentation creation) A free demo version of StarOffice is available at http://www.stardivision.com/. The set of tools included provide an answer to most of the needs of the average user.
Any decent distribution of the Linux operating system includes lots of very interesting and powerfull software, for example:
Beyond these general tools there are many other highly specialized tools for program development, CAD, mathematical analysis, scientific applications, etc..
Many of them work under the X window system stated previously, but others use the command line interpreter (shell) found when the system starts. Linux, like all UNIX, has this command line because it can efficiently perform numerous tasks whithout the need of windows overhead, moreover for some tasks the shell is a more a efficient environment.
Every computer user must always be familiar with the contents of his system hard disk and run administrative jobs over the installed packages. The system prompt is more useful and flexible than the average window environment.
From now on we will assume that the user has a minimum knowledge of MSDOS or some other operating system with equivalent shell.
The system disk of the Linux OS is organized in files. There are many type of files:
If you now enter the command pwd you'll get the following:
/root
This is the current directory, the home directory for the superuser. Let's be a little curious and go to the root directory - called like this because it's the father of all directories and files - with the command: cd /
And now let's look what we have here, using the command to list the directory contents : ls
# ls dev home lost+found root var bin dosc mnt sbin boot etc tmp lib proc usr
All these direcotories have a goal within the UNIX filesystem:
Directory | Function |
---|---|
dev | Contains all device files. |
home | This is the parent directory for all user accounts. Yes, Linux offers the capacity of many people working at the same time on the same computer without interference among them (linux is a true multiuser OS). A new user can be created with the command: adduser [username] (where username could any name bob ). I recommend you create one to do the ordinay jobs and keep the root for administration taks only, since for instance you may accidentally delete all files in hard disk being root, while as a plain user (say "bob") it would be impossible. |
lost+found | You may have used the Norton Utilities sometime to recover a file lost in some defective cluster. Don't worry anymore, if the hard disk fails the system itself will deal with file recovery, and when the recovering proccess fails to detect where the found block came from it will place it in this directory. This is just another reason why Linux is so much better than any Microsoft OS. |
root | This is the superuser working directory, like /home is to normal users. |
var | Contains numerous temporary files that store system messages, users mail, printing jobs, etc... |
bin | Here are located the most important system commands. |
dosc | It's possible this directory have other name in your computer. Here you can access the Windows (or MSDOS) partition of your system (if you have one). |
mnt | If you want to access a CD usually this directory will be used by a process called "mounting". To make the contents of the CD accesible through the /mnt directory execute mount -t iso9660 /dev/cdrom /mnt. It's possible this command may differ slightly depending on your system configuration. |
sbin | Here are the advanced system administration commands and for security reasons the should not be accesible nor run by plain users but by the administrator (root). |
boot | This is the area reserved for the system kernel. Some distributions choose to place the kernel in / instead. |
etc | This directory contain system configuration files, as well the files responsible for system booting or the window system configuration (something like the files CONFIG.SYS and AUTOEXEC.BAT). |
tmp | Programs use this directory as the prefered location for intermediate files or trash. Usually the system deletes them automaticaly during booting. |
lib | System basic libraries directory. Linux is a system which saves memory putting the code shared by many programs into only one file, called library, in a way that only one copy of it exists when it's been used |
proc | This is a mirror directory of programs in memory, it's used in order to programs like ps, top or kill allow us to see which programs are running in the system and (if there is some suspended) kill its execution. Avoiding what could happen in Windows having to restart the system and lose everything made by the other programs. |
usr | Here will be the application programs, as well your documentation and libraries. |
Usually will only be necessary manage the files in the root directory or of particular utilization of some users.
Linux is basically an operating system that favors command line communication with the user, despite the existence of a windows environment called X Window System (earlier we saw it is started with the command startx). When the user logins into the system he is immediately faced with a prompt similar to this:
root@darkstar: ~#
a typical command interpreter prompt - the structure or format of the prompt can be configure to the wishes of the user. In our particular example, the label before "@" indicates the current user (root ) and darkstar stands for the hostname of the system. Finally the ~ character stands for the users home directory, which in the case of the administrator account would be /root. Finally a "#" prompt indicates that this particular account has super-user privileges. The user is now free to enter any commands after the blinking cursor. For example:
root@darkstar:~# ls -l
In this example we can see how the user introduced a statement made of a command (ls), a command option (-l) and a parameter (bobby). When the user presses enter, Linux will list the file bobby (i.e. it will show characteristics like size, creation date, and few more things that we'll see in details later), obviously this will only happen if that file exists.
The usual sintax of a shell statement is a command name followed by options separated by blank space,
ls -l -a bobby
Options can also appear together like this:
ls -la bobby
At last, more than one filename can be specified as the target object, they should also be seperated by space characters
ls bobby joe maria
This will list the files bobby, joe and maria without details. To obtain a more detailed description of the files one can use the -l option as before.
ls -l bobby joe maria
Linux has a large amount of online information about how to use system. If you know the name of a command and want to know how use it, try:
man [command]
That will generate the manual page for the command. There are online manual pages for almost all system commands. Moreover to know about more advanced options of the man command itself, simply type man man. It is never necessary to memorize the options of a given UNIX command because the online manual pages always provide a quick and easy way to refresh our memory.
Manual page of the command man: man man |
On other hand if you only know the subject of what you want to know, or a keyword, then try:
apropos [subject]
this will show a list of all commands related with this subject.
Some commands give a short and precise description of the options available and usage when executed with the parameter --help. For instance with the command ls
ls --help
If the output of a command fills more than one screen, there is no reason to worry, just use the PageUp and PageDown keys on your keyboard to browse through the output.
Most Linux distributions include a large amount of documentation on the varous programs and utilities included. These documentation is located in the /usr/doc directory. To study the information available on tetex, for instance, just go to that directory and type more tetex. Most files in the /usr/doc directory are ASCII files.
And there is still more. There info pages include description of commands and applications that can We can also find information in the info pages which can be easily accessed through the editor emacs with the key sequence [Control+h][Control+i].
However more information could be found on web in:
http://slug.ctv.esThere are also newgroups under nntp protocol where linux subjects are vastly discussed:
news:es.comp.os.linux: Spanish groupAnd finally at many irc's we can find the #linux channel
If you have known DOS will remember the commands dir, cd, rd, md and some more. Here we'll see equivalent ones for manipulation process of created files. Always exists the possibility of get more details about what each one do with man [command].
We have already used this command previously. Let's complete a bit more its using knowledge:
If we enter the command:
$ ls -l drwxr-xr-x 3 fenix fenix 1024 Jun 15 1997 Calendar drwx------ 5 fenix fenix 1024 Nov 6 19:35 Desktop drwx------ 2 fenix fenix 1024 May 16 1997 Mail drwxrwxr-x 2 fenix fenix 1024 Mar 10 00:28 X11 lrwxrwxrwx 1 root root 11 Mar 13 20:53 ada -> private/ada drwxrwxr-x 2 fenix fenix 1024 Mar 10 00:28 bin lrwxrwxrwx 1 root root 9 Mar 13 20:53 c -> private/c -rw------- 1 fenix fenix 868352 Apr 7 21:19 core lrwxrwxrwx 1 root root 12 Mar 13 20:53 lisp -> private/lisp drwx------ 2 fenix fenix 1024 Jun 13 1997 mail -rw-rw-r-- 1 fenix fenix 302 Apr 7 20:30 mine.bmp drwxrwxr-x 2 fenix fenix 1024 Mar 10 00:28 mydocuments drwxr-xr-x 2 fenix fenix 1024 Nov 6 18:59 nhs drwx------ 2 fenix fenix 1024 Mar 12 18:05 nsmail drwxrwxr-x 2 fenix fenix 1024 Feb 17 21:27 odd drwxrwxr-x 8 fenix fenix 1024 Mar 12 23:04 private lrwxrwxrwx 1 root root 14 Mar 13 20:53 prolog -> private/prolog -rw-r--r-- 1 fenix fenix 23051 Jan 27 21:14 system.fvwm2rc drwxrwxr-x 4 fenix fenix 1024 Mar 22 15:36 tmp lrwxrwxrwx 1 root root 11 Mar 13 20:53 www -> private/www -rwxr-xr-x 1 fenix fenix 1239 Nov 6 19:46 xinitrc.bak
This is what we call a long list, which offers us a major amount of information about the directory. We can distinguish 6 fields:
The first give us information about the file permissions. It's compounded by 10 letters split in 3 groups of 3 and the first letter.
The first letter give us information about the file type:
- | ordinary file |
d | directory |
l | link |
c | special file, character device: what means you only can send or receive information byte to byte without the possibility of reading the same data. |
b | special file, block devices: this devices permit random positioning operations. |
p | pipes |
f | stacks |
The remaining letters denote who and how a file could be acccessed. They are split in 3 groups of 3 letters. The first three ones denote how the owner user can act over the file, the second triplet corresponds to the group (the user may pertain to a work group in a way that group files could be shared by them) and the third triplety is the rest of system users (or the world).
Each group of 3 letters denotes the following:
- | can not read the file, if it's a directory your content can not be listed. |
r | can be read |
- | can not write over the file (nor add information), if it's a directory files can not be created or deleted under this. |
w | can be written (create and delete files if it's a directory) |
- | can not run the file, if it's a directorys it was not accessible (what means we can not access it with command cd ) |
x | can be run. |
s | This attribute have only sense in runnable files. Whoever runs this program, it will run with permissions of the file owner in place of the permissions of the user that run it. Don't worry if you don't understand the utility of "s" attribute, just very advanced users require it. The name of this is "SetUID". |
This permissions can be changed with the command chmod that we'll see later.
The next list field is the number of links pointing to this file.
The next is the file owner user.
The fourth one denotes the group which pertains, in a way that anybody belong to this group can access to it accordly to assigned group permissions (second permissions triplet).
The fifth one give us the file size in bytes.
The sixth is the file last modification date and hour.
And at last is the file name. If it's a link the file being referred is denoted with -> [source]
There are also hidden files in Linux. This are distinguished by a dot (.) as its first character in its file name.
We can list hidden files with the option -a which shows the hidden and remaining files.
$ls -a .profile .bash_profile .bashrc .xinitrc mime.bmp misdocumentos
cp [file] [target]
If the target wasn't a existing directory it will be interpreted you want to copy the file with another name.
mv [file] [target]
If the target wasn't a existing directory it will be interpreted you want to rename the file.
cd without arguments takes you to the initial directory. If you are the superuser it'll take you to /root/ directory; if you're bob it goes to /home/bob.
How you can see, here the directories have the bar inclined to right side (and do not to left side like in MSDOS)
At last, /home may be replaced by the character ~ . Hence cd ~pepe will take you to /home/pepe
It's as simple as mkdir [directory-name] and it create a directory with this name.
If you want delete a directory free of files, this command will make your job with rmdir [file-name]. If you want delete directories having files (or others directories) see the command rm.
Delete any type of file with rm [file-name].
-i | ask for confirmation to delete the file. |
-r | delete a directory and all its contents. |
-rf | delete a diretory and its contents without any confirmation. |
Linux permits that a file have distinguish names or that we can access it from a distinguish directory. This is made with links. There are of two types:
Hard links are made with: ln
And symbolic links with: ln -s [source-files] [target-file]
This command shows the file contents into the screen.
cat [file-name]
If the file we'll see is too long (it has more lines than the screen in which we want to see it), we'll lose the first lines on top. A solution is to use the screen buffer, under which we can displace with Mays+Repag. But this buffer has limited size and many times it won't assist. The command more allow us to see files in a paged format. Its syntax is:
more [file-name]
It's a interactive program. The "keys" (interactive commands) more interesting are:
Key | Function |
---|---|
spacebar
|
advance one page |
b | take back one page |
return | advance one line |
/[string] | search the next occurrence of string into file |
?[string] | search he last occurrence of string into file |
q | quit |
There is a equivalent and more advanced program called less.
With this we can change how a file could be accessed. A regular user (root can do anything) can only change the permissions of that files which are owned by him. It are compound of 2 parameter types separated by a space.
chmod [who][+/-] [new-permissions] [file]
In who we can refer to:
Who | Description |
---|---|
u | user which owns the file |
g | group which owns the file |
o | the rest of users |
a | any user (owner, group, others) |
With + we add permisions and with - we remove them.
In new-permissions we denote what permissions will be added or removed. These are described by the letters of first field in a long list with ls (what means r:read, w:write, x:execute or visit, s:SetUID)
Let's see a example to make thinks clear:
chmod u+rw my
With this we permit the owner, actually you, to have read and write permissions over the file my.
chmod a-x directory
And with this we remove of all users (including yourself, the owner) the visit right to directory
chmod has other and more advanced options, which we won't see here, but you can use the manual page with the command:
man chmod
Linux allows us to act over a file set instead one by one. We can refer to a file set which has some common characteristic in its name. This is made using wildcards as command arguments. Some could be known of MS-DOS world:
With this we can refer to:
rm *
remove all files
rm a*
remove all files started by a
rm bet*as
remove all files startd by bet and ended by as
With this we can do:
cat c?t
show all files started by c, which have one of any letter and end by t.
In this way we can refer to:
more [tp]erez
show in a paged format the files terez and perez, if they exist.
cp c[AEIOU]endo directory
copy all files started by c followed by a capital y and ended by endo to directory.
We can use a "-" to create intervals:
mv *[a-z] trash
move all file ended by a non-capital letter to directory
trash.
We can also negate the interval:
ls [!a-zA-Z]*
list all files which the first character wasn't a capital or non-capital letter.
Linux is very powefull thanks to many reasons but one of the most versatile utilities in command line is redirecting.
Redirecting permits to redirect the output of a command or program in a way that instead show it into screen, it will be sent to a file. Also, even it will be seldom, we can send the file content as input to another command, append the output of a command to a file and at last make the output of a command be the input of another.
With this symbol we can send the output of a program to a file. For instance:
cal > february
In this way we get the output of the command cal - in this case a calendar of current month - into the file february.
With this symbol we can redirect the content of a file as input of a command.
mail mark < letter
With this we send by e-mail to user mark the file letter
These allow us append the output of a command at the end of a file, without destroy the existing content in this file. With the simple greater than redirecting, the original content of the file is replaced by the command output, thus losing its original content.
cat file1 file2 file3 >> total-file
We append (concatenate) at the end of total-file the contents of file1, file2 and file3.
This symbol permits that the output of a command be the input of another. Don't confuse this redirecting with special pipe files, they're different things in practice. Let's see a example:
who | grep "mark"
who shows users connected in the system, what terminals are using and the login time. grep will filter this output in a way that will only show the lines with mark. We know that mark is the name of a user we want to talk and in our system there is no terminal called mark, hence we can know weather mark is logged in the system.
Let's see step by step. First run in your Linux the command who and after run who, using a pipe with a grep and a logged user (root probably). For a satisfactory result log in with many different usernames through many consoles. Linux has a total of 7 consoles that could be accessed. Besides Linux permits to connect through other computers using common network hardware like token-ring, ethernet, modem, etc.. The computer consoles can be accessed with the keys Alt+F1 (for the first), Ctrl+Alt+F2 (second), Ctrl+Alt+F3, Ctrl+Alt+F4, Ctrl+Alt+F5 and Ctrl+Alt+F6. Ctrl+Alt+F7 and Ctrl+Alt+F8 are graphic terminals, activated by startx. It's possible change between graphic mode and text console with this combinations.
For last, we need text editors. They are programs that allow us introduce text, on ASCII format, in a file. With respect to use of editors there are almost authentical fanatic religions about who is more man based on the used editor :-). I myself recommend you use that seems more commodious to you. There are many editor, let's see a short summary.
$ cat > carta Dear Bob: I wish tell you that...You finish writing with the key sequence [Ctrl]+[d].
Graphic version of the popular EMACS (XEMACS). |
With this we finish the first article about basic system administration. In the next we'll see with details the GNU files utilities and following I'll talk about the most common editors in Linux ( vi and joe).
Webpages maintained by the LinuxFocus Editor team
© Manuel Muriel Cordero, FDL LinuxFocus.org |
Translation information:
|
2003-11-13, generated by lfparser version 2.38