Shell Basics

 < Day Day Up > 



The first thing you must do is get comfortable with a shell. You may have used DOS commands in a Windows command prompt, and this is not going to be much different. If you never used any DOS commands, don’t worry, this section will walk you though the basics. First you should note the default command prompt shown in Figure 3.3. The default command prompt tells you who is logged in currently. The user’s name and the domain he is logged in to are shown in the command prompt. In our example, it is root@localhost root. This means that the root user is logged on to this local machine. Don’t forget that the root user is the same as a Windows administrator and has full privileges to change anything in the system.

As was previously mentioned, bash is the default Linux shell. Fortunately, all shells share a lot of common commands. Now would be a good time to walk you through a few of these and compare them to DOS commands you might use in Windows. Keep in mind that these commands are case sensitive. That means that if you capitalize something you shouldn’t, it will not be recognized. For example, the command to show who is currently connected to your Linux machine is done by entering who. If you enter Who, WHO, or wHo, it will not work. When in doubt, go with all lowercase.

One of the simplest commands is the command to list all the contents of your current directory: the ls command. This is very much like the dir command in Windows. Both commands list the contents of the current directory. You simply type ls, in all lowercase letters, and press the Enter key. You will see the contents of that directory, as shown in Figure 3.4.

click to expand
Figure 3.4: The ls command.

It was mentioned previously that the Linux shell is more versatile and robust than the Windows command prompt. You can see this in the ls command. When you use the equivalent DOS command, dir, you see a list of the directory content with no clue as to what types of files or subdirectories it contains. When you enter ls you might notice that some items are in different colors. All files are in black, all directories in blue, and all executable files or scripts are in green. This color-coding of items makes it easy to identify and use certain items. This is just one example, a simple one that illustrates some of the added features in Linux.

Now that you have seen how to list the contents of a directory, let’s look at some other shell commands. This should help you get started with the basics of shell operations. This brings us to creating directories, changing directories, and moving files. Directories are designed to give you an organized way to store your files and programs. From time to time, you may need to create new directories, navigate through directories, or move files around. This is not much different than it was in the Windows command prompt, where you create a file by typing md directoryname. For example, if you wanted to create a directory named stuff, you would enter md stuff. In Linux you do this with the mkdir command (short for Make Directory) like this: mkdir stuff. Simply type mkdir stuff in the shell, and then run the ls command to see the content of the directory you are in. You should see a new subdirectory named stuff. You can see this demonstrated in Figure 3.5.

click to expand
Figure 3.5: Using the mkdir command.

Changing directories is not much tougher than that. Before we change to our new directory, perhaps we should put something in it. You may have noticed that when we ran the ls command, there was a file named install.log. This is a log of what happened during the installation of Linux on this machine. We are going to copy that file to our stuff directory. This is a simple process using the cp command. Simply type cp filename target directory (the name of the file you want to copy and the directory you want to copy it to). Enter cp install.log stuff, as you see in Figure 3.6.

click to expand
Figure 3.6: Copying files.

This command is very similar to copying files in a Windows command prompt, where you enter the word copy followed by the file and target directory. If we were performing this copy in Windows, we could enter copy install.log stuff.

If you want to see if the file was actually copied to the directory in question, you will need to change to that directory and run the ls command. You can change directories with the cd command by simply typing cd followed by the name of the directory. In our case that would be cd stuff. Then enter ls and you will see that the file has been copied to that directory. This is shown in Figure 3.7.

click to expand
Figure 3.7: Changing directories.

The change directory command is the same as it is in Windows command prompts. You type cd followed by the directory name.

Note 

You will note that in several instances we examine commands that are very similar or identical to Windows command prompt commands. It is important to remember that Unix existed decades before DOS or Windows, and it was Windows that copied the commands from Unix, not vice versa. This is important because Linux beginners often wonder why Linux did not simply use the same commands Windows uses.

You may have noticed that the previous commands are not displayed in the figures in this chapter, just the command that is being discussed. When you type this into your shell, you probably see everything you have entered so far. This may lead you to ask how to get rid of the old stuff you no longer want to view. Just type the word clear, and the shell screen will clear. The same thing can be done in the Windows command prompt by typing in cls (short for clear screen).

What happens when you want to get rid of a file or a directory? You must delete all files in a directory before you can get rid of that directory, so let’s discuss removing files first. In Windows you probably used the del command, simply typing in del filename. The process is very similar in Linux, only the command is rm (for remove). You enter rm filename. In our case, we want to enter rm install.log. You will then need to be out of the directory you are in, back in the root directory, to delete the directory stuff. In a Windows command prompt you enter cd\; in Linux, simply typing in cd will take you to the root directory. So now you type in cd, and you are back at the root and ready to remove that directory. In the Windows command prompt, you can use the rd (remove directory) command. In Linux you use the rmdir command (remove directory). The format is rmdir directoryname. In our case, that would be rmdir stuff. The entire process, from removing the file to removing the directory, is shown in Figure 3.8.

click to expand
Figure 3.8: Removing files and directories.

Table 3.2 lists some basic Linux shell commands with an example of using each command, an explanation of the command, and the equivalent Windows command prompt command.

Table 3.2: Linux shell command and Windows command prompt commands

Linux

Explanation and Example

Windows Command Command Prompt Equivalent

ls

List the content of the current directory. Example: ls

dir Example: dir

cp

Copy a file to another directory. Example: cp filename.txt directoryname

copy Example: copy filename.txt directoryname

mkdir

Create a new directory. Example: mkdir directoryname

md Example:md directoryname

cd

Change directories. Example: cd directoryname

Identical.

rm

Delete or remove a file. Example: rm filename

del Example: del filename

rmdir

Remove or delete a directory. Example: rmdir directoryname

rd Example:rd directoryname

clear

Clear the screen. Example: clear

cls Example: cls

This list is by no means comprehensive, but it does contain the basic shell commands you must be familiar with. It is recommended that you take some time to experiment with these commands and get comfortable with them. Section V, “Advanced Linux,” deals extensively with using the shell. Chapter 18, “Linux Shell Commands,” discusses a host of shell commands at some length. Chapter 19, “Linux Administration from the Shell,” shows you how to perform administrative tasks using the shell. Chapter 20, “Basic Shell Scripting” teaches you the basics of writing your own shell scripts. For now all you need is a basic understanding of what a shell is, and how to accomplish some basic tasks in the shell.

There is one last simple but very useful feature of the shell that needs to be covered before we continue. The shell has access to Linux manuals built into it. These are frequently called man pages (short for manual). Man pages enable you to look up the specifics for any Linux command. For example, if you forget how the mkdir command works, you can simply enter man mkdir at the shell, and you will be shown an entire section of the Linux manual on this command. Man pages are very useful, and no Linux aficionado would dream of not using them.



 < Day Day Up > 



Moving From Windows to Linux
Moving From Windows To Linux (Charles River Media Networking/Security)
ISBN: 1584502800
EAN: 2147483647
Year: 2004
Pages: 247
Authors: Chuck Easttom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net