Part 2. Console crash course
A quick guide to the Linux console
Any administrative task can be done from the console in Linux. In many cases, using the console is faster than using a graphical program and may provide additional functionality. Furthermore, any console task can be placed into a script, and thus automated. To really take control of your Linux environment, you will want to learn how to do things from the console. The information here is a guide to get you started at the Linux console if you have a DOS/Windows background.
Accessing a console
If your system boots into text mode (a common configuration for servers to conserve overhead for services), then you are already at a console when you execute a text login. On a typical Linux system, you can get to additional consoles by pressing Ctrl + Alt + (F1 - F6). Each console is a completely different session on the system and can be accessed as different users at the same time.
This multi-console behavior is different from the multiple-desktop in Windows. In Linux, each console can be controlled by a completely different user. For example, you can be logged as root on console 1, and logged in as joeuser on console 2. Both consoles run different programs in their own user space. In the same vein, different users can be logged into a Windows system remotely. In this instance, Linux provides capabilities more like a mainframe than a simple server or workstation.
If you are in a graphical mode, then you can open a terminal to get access to a console screen. The terminal will usually have a button on your desktop's task bar, or you can find it under System Tools in the Program menu. You can also open a terminal from the context menu (right click on the desktop).
There are many potential commands available from the console. Some of these tools are only truly useful when writing scripts. Here are some of the first ones that you'll probably need. Remember that all commands and options are case sensitive. -R is different from -r, and will probably do different things. Console commands are almost always lowercase.
Moving around in directories uses the familiar
cd command. The main trick is to remember that in Linux the
forward-slash (/) is used where you are accustomed to using the back-slash
(\). The back-slash is still used, but it specifies that a command should
be continued on the next line. This is sometimes done for readability when
typing in a particularly long command.
Listing files in a directory can be done with the
ls command. There are several switches you can use to alter
the look of the listing:
|Shows a long listing, including files size, date and time, and attributes|
|Sorts files by time|
|Sorts files by size|
|Combined with one of the sorting
switches, reverses the order. |
|Human readable. Uses friendly k, M, and G indicators to show file size rather than listing them in bytes.|
|Shows all the files in a directory, even the hidden ones|
Copy files with the
cp command. The
command works essentially the same as the DOS
|Copies files recursively; required if you are copying an entire directory|
|Forces the copy and overwrites existing files without asking|
|Links files instead of copying; see below|
Move files and rename files with the
mv command. It works essentially the same as the DOS
move command, except that it will move entire directory
structures as well as files.
View files with the
This is the equivalent of the DOS
type command. It will dump
the contents of a file to another file, to the screen, or to another
cat is short for concatenate, and can be used to
sequence several files together into a larger file.
View information one page at a time with the
more command. It works essentially the same as the DOS
less to view a text file with
the ability to scroll up and down through the document and search for text
Some might say that
vi stands for
"virtually impossible." It is a text editor that has a long tradition in
the Unix world.
vi is not really intuitive, but it is
available in almost any Unix-like environment. There is a built-in
tutorial for the version installed in Linux, and once you get used to it,
you can do some truly incredible things in a few keystrokes. Truly, no
editor has managed to replace
vi for editing password and
View documentation for a command with the
man command. Man is short for manual. Documentation
tends to be thorough. To learn more about
info is like
it provides hyperlinked text to make browsing documentation easier.
One critical difference between DOS/Windows and Linux is that the command shell is a layer separated from the operating system. The shell environment affects the features you have, such as editable command lines and scrolling histories. The shell also determines the syntax required to do functions in scripts. In DOS/Windows, there was only one option for scripting, the lowly .BAT file. It did a lot, but required a good deal of creativity on the part of the script writer to do more than basic tasks. In Linux, scripts can contain loops and do more than basic conditional statements, including many things that you expect from a programming language. If you were good at writing .BAT files, shell scripts are going to let you shine.
The default shell is a parameter in each user account. The typical default
shell in Linux is /bin/bash, though others are available. The
man documentation for each shell is actually very good and
goes into detail about shells and how they work. Rather than try to
paraphrase that information here, select a shell from the list below and
look at its man page.
The bash shell is a free version of the Bourne shell, the first Unix shell, and includes many additional features. Bash has editable command lines, a scrollable command history, and tab completion to help avoid typing long file names.
The C shell uses a "C-like" syntax and has borrowed many features from the Bourne shell, but uses a different set of internal shell commands.
The Korn shell uses the same syntax as the Bourne shell and has included the user-friendly features of the C shell.
ksh is used in many installation scripts and should probably
be installed on the system even if it's not your primary shell.
The TC shell is an enhanced version of the C shell and is 100% compatible with it.
The Z shell is an enhanced version of the Korn shell with many features found in the bash shell.
Link me up, Scotty!
One compelling feature in the Linux file system is the file link. A link is a reference to a file, so that you can let files be seen in multiple locations of the file system. However, in Linux, a link can be treated as the original file. A link can be executed, edited, and accessed without having to do anything unusual. As far as other applications on the system are concerned, a link is the original file. When you make edits to a file through the link, you are editing the original. A link is not a copy. There are two kinds of links: a hard link and a symbolic link.
A hard link can only reference files in the same file system. It provides a reference to the file's physical index (also called an inode) in the file's system. Hard links do not break when you move the original file around because they all point to the file's physical data rather than its location in the file structure. A hard-linked file does not require the user to have access rights to the original file and does not show the location of the original, so it has some security advantages. If you delete a file that has been hard linked, the file remains until all references have been deleted as well.
A symbolic link is a pointer to a file's location in the file system. Symbolic links can span file systems and can even point to files in a remote file system. A symbolic link shows the location of the original file and requires a user to have access rights to the original file's location in order to use the link. If the original file is deleted, all of the symbolic links become broken. They will point to a non-existent location in the file system.
Both types of links can be made with the command
<source> <target>. By default
will make a hard link. The
-s switch will make a symbolic
# Create a hard link from MyFile in the current
# directory to /YourDir/MyFile
ln MyFile /YourDir
# Create a symbolic (soft) link from MyFile in
# the current directory to /YourDir/YourFile
ln -s MyFile /YourDir/Yourfile
In the above examples, MyFile, /YourDir/MyFile, and /YourDir/Yourfile are all treated as the same file.
Coming out of your shell
Learning to work from the console is a necessary skill for Linux
administration. There are tools to avoid the console, but you will always
be more limited by what you can do through a tool. Accessing a console is
easy, and accessing command documentation is easy too with the
- Check out the other parts in the Windows-to-Linux roadmap series (developerWorks, November 2003).
- Get started with the vi editor by following the tutorial "vi intro -- the cheat sheet method" (developerWorks, ).
- You can browse many man pages online at the GNU Manuals Online page.
- The From DOS/Windows to Linux HOWTO outlines some good quick-start information for people with a DOS or Windows background.
- AllCommands.com is an unusual site which helps to reference and cross-reference commands from various operating systems.
- The tutorial "LPI certification 101 exam prep, Part 1: Linux fundamentals" covers bash, standard Linux commands, and more.
- Learn more about shell scripting in the article series "Bash by example" (developerWorks, ).