Linux for Newbie(Part 4.1)
Basic Operations FAQ
1 Basics
1.1 Filenames
Linux is case-sensitive. For example: my_filE, my_file, and my_FILE are three different files. Your password and login name are also case-sensitive. (This follows the tradition since both UNIX and the "c" programming language are case-sensitive.) Naming conventions for files and directories are identical.Filenames under Linux can be up to 256 characters long and they normally contain letters, numbers, "." (dots), "_" (underscores) and "-" (dashes). Other characters are possible but not recommended. In particular, it is not recommended to use special metacharacters: "*" (asterisk), "?" (question mark), " " (space), "$" (dollar sign), "&" (ampersand), any brackets, etc. This is because matacharacters have special meaning to the Linux shell. It is possible to have space inside the filename, but we don’t recommend it either--we use underscore instead.
It is not possible at all to have ’/’ (slash) as a part of the filename because ’/’ is used to represent the top of the directory tree, and as a separator in the pathnames (the same as ’\’ is in DOS). To manipulate files with names that do contain matacharacters, I use a pair of ’ (apostrophes), so that the metacharacters are quoted and therefore the shell does not interpret their meaning. For example, to remove a file my file* (contains space and asterisk), I would issue:
rm ’my file*’
Please note that I use a pair of ’ (apostophes) for quoting. Quoting with a pair of " (quotation marks) is generally weaker than quoting with ’ . When you use " some metacharacters may get interpreted.
Here is the meaning of some metacharacters:
* = Matches any sequence of zero or more characters (except for "." dot at the beginning of a filename).
? = Matches any single character.
[abC1] = Matches a single character in the enumerated set. In this example the set is: ’a’, ’b’, ’C’, or ’1’.
[a-z] = Matches any lower-case letter.
[A-F] = Matches any upper-case letter from A to F in the Latin alphabet.
[0-9] = Matches any single digit.
[a-zA-Z0-9] = Matches any letter (lower or upper case) or any digit.
Examples. This command will show any filename in the current directory, with the exception of filenames starting with "." (dot):
ls *
An equivalent to this command is to type just "ls" or "dir". Files with names starting with "." are not shown
because "." as the first character of a fileneme is not matched by "*". Think of files with names starting with "." as an equivalent of DOS hidden files. Use ls -a (list with the option "all") orls .* to see these "dot" files. The dot-files are common in the user home directories and are typically used to hold user-level configurations.
This command will show any filename that contains a dot: ls *.*
This command will show any filename that contains two dots:
ls *.*.*
Please note that Linux does not have "filename extensions" and some other DOS-like file-naming features ("Micros~1.doc" comes to mind).
This command will show all filenames in the current directory that start with "a" or "b", or any capital letter:
ls [abA-Z]*
As an example of problems that you might face when using non-recommended characters in a filename, try creating a file with a name starting with a dash and then remove it--there seems to be no way to do it (a dash normally introduces command options). E.g., the command
dir > -junk
will create such a funny file (the symbol ">" redirects the output from the dir command to a file named -junk). Since the regular way of removing the file -junk does not work, I use: rm ./-junk
The dot slash at the beginning means "the current directory" and here just serves the purpose of hiding the leading dash so it is not interpreted as introducing an option to the rm command. The point here is that I rather stick to traditional naming conventions than face the occasional complications.
1.2 What are the different directories for?
This is explained very nicely in the Linux System Administrator Guide (SAG), which should be available on your system. Try:cd /usr/doc/LDP/sag
lynx sag.html
This will start a text-mode browser "lynx" to view this html book. You can also use any other browser, e.g. Netscape for Windows, to view this book. This book and other LDP books are actually quite easy to read.
Briefly, Linux contains five filesystems. These filesystems can reside on a single or different physical hard drives and/or hard drive partitions, depending on the size and need of your system. (A single filesystem can also be distributed between different physical devices, if needed.)
The root " /" filesystem--contains basic operating system and maintenance tools. The content of this filesystem should be sufficient to start up the system and perform emergency maintenance and repairs if they were necessary.
/usr filesystem--contains all commands, libraries, documentation, and other files that do not change during normal operation. This will also contain major applications, perhaps the ones that come with your distribution, for example Netscape.
/var filesystem--contains files that change: spool directories, log files, lock files, temporary files, and formatted manual pages.
/home filesystem--contains user files (users’ own settings, customization files, documents, data, mail, caches, etc).
/proc filesystem--contains entirely illusionary files. They don’t really exist on the disk and don’t take any space there (although ls -l will show their size). When viewing them, you really access information stored in memory. It is used to access information about the system. The parts of the root filesystem are:
/bin--commands needed during bootup that might be used by normal users.
/sbin--commands not intended for use by general users (users may still use them). /etc--system-wide configuration files for your operating system.
/root--the home directory of the system administrator (called super-user or root). /dev--device files. Devices appear on Linux as files so it is easy to write to them. /mnt--mount points for removable media (floppy, cdrom, zipdrive), partitions of other operating systems (like dos), network shares, and anything else that is mounted on the file system temporarily. It normally contains subdirectories for the mounting shares.
/lib--shared libraries for programs that reside on the root filesystem.
/boot--files used by LILO (a bootstrap loader, the thing that loads first when the computer is booted and perhaps gives you an option which operating system to boot, if you have more than one OS on your computer). It typically also contains the Linux kernel, but this can be stored somewhere else, if only LILO is configured to know where it is.
/opt--optional large applications, for example kde under RedHat 5.2 (under RedHat 6.0, kde is distributed as any other X-windows distribution, main executables are in the /usr/bindirectory). /tmp--temporary files. This directory may clean automatically.
/lost+found--files recovered during the filesystem repair.
The most interesting parts of the /usr filesystem are:
/usr/X11R6--X-windows system.
/usr/X11--the same as /usr/X11R6 (it is a symbolic link to /usr/X11R6).
/usr/X11R6/bin --lots of small X-windows apps, and perhaps symbolic links to the executables of some larger X-windows applications that reside in other subdirectories).
/usr/doc--Linux documentation.
/usr/bin and /usr/sbin--similar to their equivalents on the root filesystem (/bin and /sbin), but not needed for basic bootup (e.g. during emergency maintenance). /usr/local--the installed "local user" applications, for example Netscape (each application in a separate subdirectory).
/usr/local/bin--perhaps smaller "user" apps, and symbolic links to the larger executables contained in separate subdirectories under /usr/local .
It is important to understand that all directories appear in a single directory tree, even if the directories are contained on different partitions, physical drives (including floppies, etc), or even if they distributed over the network. Therefore, there are no DOS-type "drive letters" under Linux.
1.3 How do I run a program?
Typing the name of the executable on the command line doesn’t help? There are three possibilities.The first possibility: you don’t type the name of the executable correctly. Check the case--Linux is case sensitive! For example, typing "Pico" or "PICO" will not start thepico editor.
The second possibility: maybe the program is not on your PATH. Under Linux (or UNIX), an executable must be on your PATH to run it, and the current directory is NOT on your PATH. Type the full path to the executable with the executable name, or execute:
cd the_program_dir ./program_name
You must put the dot and slash in front of the program name or the program will NOT execute. (This is a security feature not to put one’s current directory on the path. It makes "trojan horses" more difficult. A "trojan horse" is a malicious program that pretends to be something different than it really is.) This dot means "the current directory", and the slash "/" is a separator between the directory name and the filename (exactly as "\" in DOS).
You may check your path using:
echo $PATHTo learn how to change your PATH, or add your current directory to it, see the next answer [p 33] .
If your executable is lost somewhere in your directory tree, you may want to find it using (for example):
find -name "netscape"
to find a file called "netscape". You may be able to achieve the same result faster using:
locate netscape
(Locate runs faster because it relies on a pre-built database of files on your system. This database if updated by a background cron process that normally runs at night, so don’t count on locateto find a file if you regularily switch off your computer for the night, or you search for a file that you just installed.)
Please note that the PATH is normally different for root than regular users (root’s PATH includes /sbin and /usr/sbin whereas users’ don’t). Therefore users cannot execute command located in the "sbin" directories unless they specify the full path to the command. Also, if you become a superuser by executing the su command, you inherit the user’s PATH, and to execute the command located in sbin, you need to specify the full path.
Conversly, if you need to learn where an executable which is on your PATH is located on your system (i.e., the executable runs by typing its name anywhere in the system, but you would like to know where it is), you may use something like this:
which netscape
which will show a full PATH to the executable program called "netscape".
The third possibility: maybe the file is not executable. If it should be, change the permissions to make it executable. E.g. (as root or the user who owns the file):
chmod a+x my_file
will make the file "my_file" executable for all users. Check if it worked using: ls -l my_file Read here [p 41] if you don’t understand the output of this command or the whole "third possiblity".
1.4 How can I change the PATH?
The PATH is the list of directories which are searched for the program the execution of which you request. You can check your PATH using this command:echo $PATH
which, on my system , shows the PATH for the user "yogin" to be:
/opt/kde/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/yogin/bin
The ":" is a separator, therefore the above PATH represents a list of directories as follows:
/opt/kde/bin
/usr/local/bin
/bin
/usr/bin
/usr/X11R6/bin
/home/yogin/bin
Here is the output from the command "echo $PATH" run on my system on the account "root":/opt/kde/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
You can change the PATH for all users on the system by editing the file /etc/profile and adjusting (as root) the line starting with "PATH=". I do it using the pico editor (as root):
pico -w /etc/profile
(The option -w turns off the wrap of long lines.)
Re-login for the change to take effect. To set up the PATH for an individual user only, edit the file /home/user_login_name/.bash_profile (please note the dot in front of the filename--files starting with a dot are normally invisible, you have to use ls -a to see them).
If you really want to have the current directory on your PATH, add "." (dot) to your PATH. When used in the place when directory name is expected, a dot means "the current directory". The specification for the path in /etc/.bash_profile may then look like this:
PATH="$PATH:$HOME/bin:"."
export PATH
This command takes the contents of the environmental variable called PATH (as set for all users in /etc/profile), and appends to it the name of your home directory as set by the variable HOME with an attached "/bin" and then a dot. Finally, the command assigns the resulting string back to the variable called PATH. It is necessary to use the command "export" after modifying PATH or any other user-enviroment variable, so that the variable is visible outside of the script that sets it.
1.5 How can I shutdown my computer?
In a text terminal, press <Ctrl><Alt><Del>, wait for the shutdown process to complete, and turn off your machine only after it starts rebooting again. If you are in X-windows, first switch to a text terminal by pressing <Ctr><Alt><F1> (three keys simultanously). Do not turn off your machine without the proper shutdown or else you may have disk error messages next time you boot. (Typically, the errors resulting from improper shutdown will be repaired automatically during the next boot, but occassionally more serious problem may result, and then you may need to repair the files manually or re-install!)If you prefer your computer to go to the halt after you press <Ctrl><Alt><Del> (instead of the default reboot), you can set this up by editing the file /etc/inittab. This file specifies something like this:
# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
Change (as root) the option "-r" to "-h" so that it reads:
# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -h now
The line starting with "#" is just a comment (it is for the humans, it does not have any effect on the computer).
Root can also use the shutdown command. This command can be used for either a local or remote shutdown of the system. The last one is very useful if a program hangs so that the keyboard is no longer functional. For example:
telnet name_of_machine_with_no_operable_keyboard
[login as a user]
su
[give password]
Now either execute ps axu |more, find the process id of the offending command in the ps output and do
kill pid_of_offending_process, or reboot your machine with:
/sbin/shutdown -rn now
This command will shutdown really fast, bypassing standard (longer) shutdown procedure--useful when the system becomes really buggy (the option -n will make "shutdown" kill all the processes before rebooting).
Please note that for security reasons, you cannot login to a remote machine as root (e.g., over the telnet). You have to login as a user and then execute su and give a password to become a super user (root).
The shutdown command may also be used to execute a shutdown later. E.g. (as root): /sbin/shutdown -r 23:59
will reboot the system 1 minute before midnight.
If the shutdown command is too long for you, you may want to try these two commands, which do exactly what their names suggest (as root):
reboot halt
A fancy way to shut down your computer is to switch your system to the runlevel 0 (for halt) or runlevel 6 (for reboot). Try it using (as root):
init 0 The meaning of the different runlevels is explained in the file /etc/inittab and here.
1.6 How do I deal with a hanged program?
Buggy programs do hang under Linux. A crash of an application should not, however, affect the operating system itself so it should not be too often that you have to reboot your computer. In our experience, a misbehaving operating system may be a sign of hardware or configuration problems: we repeatedly encountered problems with the Pentium processor overheating (the fan on the Pentium did not turn as fast as it should or it stopped altogether, the heat sink on the Pentium was plugged with dirt), bad memory chips, different timing of different memory chips (you may try re-arranging the order of the chips, it might help), wrong BIOS setup (you should probably turn off all the "advanced" options, Linux takes care of things by itself). The "signal 11" error message is typically associated with hardware problems and it most likely to manifest itself when you perform computing-intensive tasks: Linux setup, kernel compilation, etc.Not really hanged. Some programs might give the uninitiated impression of hanging, although in reality they just wait for user input. Typically, this happens if a program expects an input file name as a command line argument and no input file is given by user, so the program defaults to the standard input (which is console). For example, this command
cat
may look like it’s hanged but it waits for keyboard input. Try pressing <Ctrl>d (which means "end-of-file") to see that this will satisfy the "cat" command. Another example: I have seen many questions on the newsgroups about the "buggy" tar command that "hangs" when trying to uncompress a downloaded file, for example:
tar -zxv my_tar_file
This waits for user input too, since no option "-f filename" was specified so the second parameter "my_tar_file" was not recognized as a filename. The correct command is:
tar -zxvf my_tar_filename
Please note that the filename must follow immediately after the option "f" (which stands for "filename). This WILL NOT work (very common mistake):
tar -zxfv my_tar_file
Any program (hanged or not) can be killed. A text-mode program in the foreground can often be killed by pressing <Ctrl>C. This will not work for larger applications which block the <Ctr>C, so it is not used on them accidentally. Still you can get back in control either by sending the program to the background by pressing <Ctrl>z (no guarantee this will work) or switching to a different terminal, for example using <Ctrl><Alt><F2> and login as the same user that hanged the program (this should always work). Once you are back in control, find the program you want to terminate, for example:
ps
This command stands for "print status" and shows the list of programs that are currently being run the current user. In the ps output, I find the process id (PID) of the program that hanged, and now I can kill it. For example:
kill 123
will kill the program with the process id (PID) of "123".
As user, I can only kill the processes I own (this is, the ones which I started). The root can kill any process. To see the complete list of all processes running on the system issue:
ps axu | more
This lists all the processes currently running (option "a"), even those without the controlling terminal (option "x"), and together with the login name of the user that owns each process ("u"). Since the display is likely to be longer than one screen, I used the "more" pipe so that the display stops after each screenful.
The kill command has a shortcut killall to kill programs by name, for example:
killall netscape
will kill any program with "netscape" in its name.
X-windows-based programs have no control terminals and may be easiest to kill using this (typed in an X-terminal):
xkill
to which the cursor changes into something looking like a death sentence; you point onto the window of the program to kill and press the left mouse button; the window disappears for good, and the associated program is terminated.
If your X-windows system crashes so that it cannot recover, it may be the easiest to kill the X-server by pressing <Ctrl><Alt><BkSpace>. After that, it may be a good idea to run ps axu, find any possible X-programs that might still be running, and kill them. If you don’t do this, the misbehaving program that caused your X-windows to crash might cause trouble again.
If you have programs in the background, the operating systems will object your logging out, and issue a message like "There are stopped jobs". To override and logout anyway, just repeat the logout (or exit) command immediately --the background program(s) will be automatically terminated and you will be logged out.
Core files. When a program crashes, it often dumps a "core" into your home directory. This is accompanied by an appropriate message. A core is a memory image (plus debugging info) and is meant to be a debugging tool. If you are a user who does not intend to debug the program, you may simply delete the core:
rm core
or do nothing (the core will be overwritten if another core is ever dumped). You can also disable dumping the core using the commmand:
ulimit -c 0
Checked if it worked using:
ulimit -a
(This shows "user limits", the option "-a" stands for "all"). To make option of disabling core dumps permanent for all users, edit the file /etc/profile (as root), where ulimit is set, and adjust the setting. Re-login for the changes to /etc/profile to take effect.
If you would like to see how a core file can be used, try (in the directory where you have a core file):
gdb -c core
This launches GNU debugger (gdb) on the core file "core" and displays the name of the program that created the core, signal on which the program was terminated, etc. Type "quit" to exit the debugger. To learn the meaning of different signals, try:
cat /usr/include/bits/signum.h
No comments:
Post a Comment