Windows-to-Linux roadmap: Part 2. Console crash course

A quick guide to the Linux console

IBM e-business architect Chris Walden is your guide through a nine-part developerWorks series on moving your operational skills from a Windows to a Linux environment. He covers everything from logging to networking, and from the command-line to help systems -- even compiling packages from available source code. In this part, we cover the different shells, as well as some of the most essential Linux commands.

Share:

Chris Walden, e-business Architect, IBM

Chris Walden is an e-business Architect for IBM Developer Relations Technical Consulting in Austin, Texas, providing education, enablement, and consulting to IBM Business Partners. He is the official Linux fanatic on his hallway and does his best to spread the good news to all who will hear it. In addition to his architect duties, he manages the area's all-Linux infrastructure servers, which include file, print, and other application services in a mixed-platform user environment. Chris has ten years of experience in the computer industry ranging from field support to Web application development and consulting.



11 November 2003

Also available in Russian Japanese

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).


Commands

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.

cd
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.

ls
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:

Listing files
CommandFunction
ls -lShows a long listing, including files size, date and time, and attributes
ls -tSorts files by time
ls -SSorts files by size
ls -rCombined with one of the sorting switches, reverses the order. ls -lt shows the files with the newest one at the top of the list. ls -lrt shows the files with the newest ones at the bottom.
ls -hHuman readable. Uses friendly k, M, and G indicators to show file size rather than listing them in bytes.
ls -aShows all the files in a directory, even the hidden ones

cp
Copy files with the cp command. The command works essentially the same as the DOS copy command. Essential switches:

Copying files
CommandFunction
cp -RCopies files recursively; required if you are copying an entire directory
cp -fForces the copy and overwrites existing files without asking
cp -lLinks files instead of copying; see below

Creating links with the copy command

The cp command can be used to create a quick set of hard links to a file, or to an entire file structure. Use the -l switch to indicate link copying. All directories will be created as directories, but all files will be set up as hard links.

cp -lR /data/accounting/payroll /data/management/hr

The above command will copy the entire directory structure from /data/accounting/payroll and below to /data/management/hr/payroll. All files in the directory structure will be set up as links. This can be used to provide different views of the same files within a file system. This is also a helpful security technique, allowing access to files from a different directory with different access controls.

mv
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.

cat
View files with the cat command. 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 command. cat is short for concatenate, and can be used to sequence several files together into a larger file.

more
View information one page at a time with the more command. It works essentially the same as the DOS more command.

less
Use less to view a text file with the ability to scroll up and down through the document and search for text patterns.

vi
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 configuration files.

man
View documentation for a command with the man command. Man is short for manual. Documentation tends to be thorough. To learn more about man, type:

man man

info
info is like man except it provides hyperlinked text to make browsing documentation easier.


Which shell?

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.

bash
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.

csh
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.

ksh
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.

tcsh
The TC shell is an enhanced version of the C shell and is 100% compatible with it.

zsh
The Z shell is an enhanced version of the Korn shell with many features found in the bash shell.

Shell game

You can change your shell at any time by simply executing it from the console. A script can specify the shell it wants to run by putting a shebang (#!) at the top of the file pointing to the desired shell. When the script is executed, it will run in the correct shell, but leave the user's shell environment alone. Here's an example of a line to get a script to run in C shell:

#!/bin/csh


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 ln <source> <target>. By default ln will make a hard link. The -s switch will make a symbolic link.

# 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 man and info commands.

Resources

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Linux on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Linux
ArticleID=11350
ArticleTitle=Windows-to-Linux roadmap: Part 2. Console crash course
publish-date=11112003