RC Linux Tutorial

RC Linux Tutorial

This tutorial has been put together by Research Computing to help familiarize new users with the Linux operating system.

About this tutorial

What is Linux?

Linux is a Unix-like Operating System originally created by Linus Torvalds while he was a student at the University of Helsinki. His main goal was to create a Minix clone and make the operating system, with the entire source code, freely available.

Here is the original quote that Linus made in comp.os.minix about his new operating system.

Do you pine for the nice days of Minix-1.1, when men were men and wrote their own device drivers? Are you without a nice project and just dying to cut your teeth on an OS you can try to modify for your needs? Are you finding it frustrating when everything works on Minix? No more all-nighters to get a nifty program working? Then this post might be just for you.

As I mentioned a month ago, I'm working on a free version of a Minix-look-alike for AT-386 computers. It has finally reached the stage where it's even usable (though may not be, depending on what you want), and I am willing to put out the sources for wider distribution. It is just version 0.02...but I've successfully run bash, gcc, gnu-make, gnu-sed, compress, etc. under it.

Soon afterwards, Linus received lots email from people that where interested in his project. From there Linux continued to become what it is today, a full-scale multitasking, multiuser operating system.

Who uses Linux?

Linux users come in a variety of different people, although most of its users are fairly knowledgeable about computers and how they work.

Linux is user-friendly. It just chooses its friends wisely.

Many people are instantly turned off by the fact that Linux is a text-based operating system. It doesn't have all the graphical interfaces that one would expect to see on a MacIntosh® or Windows® operating system. Most of the home-users turn away from Linux when they hear about this. What they don't know is that many companies have put together distributions of Linux that have many graphical equivalents of the text-based utilities, making Linux a real option for the new user.

Linux has become popular for many different uses. Among these uses include web, ftp, dns, and other Internet related servers, software development, database uses, and is also popular with the scientific research community. The reliability of Linux makes it strong in most of these areas. Very few operating systems can stay running stable for months at a time without requiring a reboot of the machine. The free compilers for just about every programming language make Linux an excellent platform for software development, instead of having to pay thousands of dollars for site licenses, Linux users get everything for free. The speed of the operating system makes Linux a good choice for database and scientific related software. For instance, a reputable source claims that Maple® runs three times faster under Linux than it does in Windows®.

Multiple Method Disclaimer

This tutorial is designed to give you a basic idea of how to use Linux so you may program on the CIRCE/SC clusters at USF. It is not solely intended to make you a Linux expert, but should make you comfortable enough to use the system. Since this tutorial will be specific to the basic setup of a user account on the cluster, the examples and programs that are taught will not cover all alternative software. Please note that because we show you one way of doing something doesn't mean that it is the only way. This tutorial will use the bash shell for its examples, there are many other shells that you may choose from, such as: tcsh, korn shell, sh, csh. For the basic commands chapter, the commands will be the same on all Linux distributions. Please note that we will be using RedHat as the distribution for the cluster, there are very little differences between RedHat and other distributions. The main difference being the package system that they use, but you won't really need to use that on the cluster. If you have ever used a distribution other than RedHat you should be able to use our distribution just as easily as the other one.

Getting Started

Since most people have never even seen Linux, it might take a while to get used to. If you have used MS-DOS you should be able to recognize some similarities. Those who have experience using other types of UNIX's should adapt right away as Linux is almost the same in every way.

For those people who have only used windows be prepared to relearn most ways of doing things. You may not be used to using a command line, which is used for most of the basic programs such as listing the files in a directory, and moving and copying files. If you don't like the text based environment you will have to get used to it, although there are many graphical utilities that can do just about anything you want.

Logging In

Linux is a multi-user operating system. This means that many people can log into the system and use it simultaneously. Each user is given certain permissions on what files and programs they can or can't run, they may also have different shells, environment variables, etc.

When you connect to, or boot-up, your system you will be prompted to enter a user name and password:

RedHat Enterprise Linux 6.8
Kernel 2.6.32-696.18.7 on i686

host login: root 

Next you will be prompted for the password for that user:

password: 

After logging in you will be given a command prompt. This is where you will be entering commands to run programs.

[root@host ~]#

Users

In the above login example we logged into the system as the user root. The root user is the super-user of the system. Root generally is used to do administrative tasks on the system, install, uninstall, and configure programs for the users. The root user can modify any of the files on the system, which is why you should know what you are doing when using the root account. If you have your own computer with Linux installed on it, you will have access to the root account. The password should have been set during the installation of your distribution. The root account should rarely be used as it can cause serious damage to the system if you are not careful.

You may be asking then, "How do I add a standard user to work with?". This can be done by using the adduser program, while you are logged into the system as root. Simply type adduser into the command-line. He is an example, we will add the user bob to the system.

[root@host /home]# adduser bob 

Depending on what distribution of Linux you are using the command passwd will be run for the user that you just added. The passwd program changes the password for a user account. If after running the adduser command, you aren't asked to change the password for the user you will want to type in the command 'passwd <user name>' to set the user's password. Otherwise the user can login to the machine with no password, and this is a BAD thing. By default, usually this command will add the user to the system's password file and the adduser command will create them a user home directory under /home/username. So if we look at /home there should now be a directory called bob. Log out as root by typing logout and then login as the user bob.

Setting up the Environment

Each user that is on the system has different settings for their account. These settings are typically what type of shell program they are given upon logging into the system to user-specific settings for globally install programs on the computer. Most of the time the settings are stored in files that begin with a '.' or they are set in the shell program as variables.

Variables

Lets take a second to look at how to use environment variables to specify paths, aliases, and other useful information that we might need.

Variables are used by the shell to temporarily hold information, this way the user can save time by not having to specify something every time a program needs to be found or what the command line should look like. To see a list of all the variables that are set type set in your command prompt, this should list all the variables and their values in the form of "variable=value". If you ever want to check the value of just one variable you can type echo $variablename, where $variablename is the name of variable with the $ in front of it. Typing echo $SHELL will output /bin/bash (or whatever your shell program is), the value for the variable SHELL.

We can create and modify variables to give us more flexibility when working with the shell. To create a usable variable we have to declare the name of the variable and then assign a value to it. I will be showing how to do this under the bash shell, other shells follow a similar way of declaring variables and usually you can find out how by typing man yourshell.

To change the value of a variable you would type the name of the variable and then assign it a value using the = sign. Here is an example where we tell the shell a path.

[bob@host ~]$ export PATH="/bin:/usr/local/bin:/opt/gnome/bin
[bob@host ~]$ echo $PATH
/bin:/usr/local/bin:/opt/gnome/bin
[bob@host ~]$ 

As you can see, we now have a variable called path that stores the directories where the shell should look to find files. This way instead of having to type /opt/gnome/bin/gcalc we can now just simply type gcalc from anywhere and the program will run, this saves us a lot of typing in the future if we are using a certain program frequently.

What if we already had a previous value for PATH and we would like to save that value and add our new directories along with it. We can just simply use the variable name with a $ in front of it to use the previous value. So if our path was originally '/usr/bin:/usr/sbin' it is now '/usr/bin:/usr/sbin:/bin:/usr/local/bin:/opt/gnome/bin'. The new line would be:

export PATH="$PATH:/bin:/usr/local/bin:/opt/gnome/bin" 

There are other things that you can do with the command prompt, you can find more information by typing 'man bash'.

Configuration Files

Lets begin with an example on configuration files and what they are. These are the files in the user's home directory that begin with a '.'. Most of the time when the user logs into their account a file is read be the shell program to set up user settings such as paths, what the command prompt looks like, and other settings that change from user to user.

For an example we will take a look at the configuration file for the bash shell. This file is called .bashrc under the users home directory. If you are using another shell other than bash the file will be different but usually will follow the same naming scheme, i.e. tcsg uses .tcshrc as a configuration file. So, once inside your home directory use the command ls -al to display all the contents of the current directory.

[bob@host ~]$ ls -al
total 8
drwxr-xr-x 2 bob other 512 Apr 22 16:03 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
-rw------- 1 bob other 397 Apr 22 16:09 .ssh
-rw-r--r-- 1 bob other 176 Apr 22 16:09 .bash_profile
-rw-r--r-- 1 bob other 124 Apr 22 16:09 .bashrc

[bob@host ~]$

The .bashrc and .bash_profile files hold settings that should be applied whenever the shell is started. Typical settings in here at set commands for specifying paths and the prompt, and other programs that should be run at login. To see what is in this file you can type cat .bashrc and cat .bash_profile. Here is an example:

[bob@host ~]$ cat .bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
      . /etc/bashrc
fi

# User specific aliases and functions

[bob@host ~]$ cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

As you can see mostly everything in the configuration file is from the previous section. With this configuration file it is possible to save those settings that we prefer and use them whenever we log into the user account. With out a .tcshrc file the shell uses the default settings. You can edit these files to your desired settings with any text editor. If you are new to Unix and have no clue about editing text try a program called pico, this is a fairly straight-forward editor; if have ever use the email program called pine you should be somewhat familiar to it. Editing text will be given in more detail in a future section.

Most configuration files will be commented, providing an explanation about how to use them.

Basic commands

In this chapter we will talk about some of the basic commands to help a new user quickly get a grasp on using a Linux system. We will discuss how to view, create and modify directories and files. You will also learn how to move around the system and change file permissions.

Basic commands for navigating the filesystem

The 'ls' command

This is perhaps one of the most basic commands that there is, you will use it all the time and it is nice to know a few different ways on how to use it. What the command does is outputs to the screen a listing of all the files in your current directory. Here is an example of the ls command in action:

[bob@host ~] ls
dir1 dir2 file1 file2 file3 file4 file5
[bob@host ~]

As you can see this gives us a listing of all the files and directories that are in the current directory. This isn't all the information that can be given by the ls command though, lets say that you wanted to know more information about a file... such as the date it was created/modified, who owns the file, what permissions are set on the file, and maybe you would like to know the size of the file. To get this information we can simply pass something called an argument to the ls command. The argument that we give the command is -l, which lists in long format. So when we piece the command together with the argument we get ls -l. Let's try it out:

[bob@host ~] ls -l
total 8
drwxr-xr-x 2 bob other 4096 Apr 25 11:05 dir1
drwxr-xr-x 2 bob other 4096 Apr 25 11:05 dir2
-rw-r--r-- 1 bob other 0 Apr 25 11:11 file1
-rw-r--r-- 1 bob other 0 Apr 25 11:05 file2
-rw-r--r-- 1 bob other 0 Apr 25 11:05 file3
-rw-r--r-- 1 bob other 0 Apr 25 11:05 file4
-rw-r--r-- 1 bob other 0 Apr 25 11:05 file5
[bob@host ~]

Let's take a minute to explain what we are seeing here. The first line as you can see, tells us the size of all the files in the directory in kilobytes (1 kilobyte = 1000 bytes). The second line that we get is a directory called dir1, we know this is a directory because if we look at the first bit of information we are given, the permissions on the file, it has a 'd' for the first letter in the set. The second piece of information we are given is the number of links to the file (explained later in this chapter). Next is the user and group that the file is owned by. Then comes the size of the file, this is given in bytes. And finally the date and file of the last modification of the file.

Another argument that is nice to know with the ls command is -a. This shows all hidden files in a directory. These hidden files are the ones that begin with a period and are usually configuration files that are used mostly by installed programs. Here is what we get when we use the command:

[bob@host ~] ls -la
total 12
drwxr-xr-x 5 bob other 512 Apr 25 16:29 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 176 Apr 24 16:12 .bash_profile
-rw-r--r-- 1 bob other 124 Apr 24 16:12 .bashrc
drwxr-xr-x 2 bob other 512 Apr 25 16:29 dir1
drwxr-xr-x 2 bob other 512 Apr 25 16:29 dir2
-rw-r--r-- 1 bob other 0   Apr 25 16:29 file1
-rw-r--r-- 1 bob other 0   Apr 25 16:29 file2
-rw-r--r-- 1 bob other 0   Apr 25 16:29 file3
-rw-r--r-- 1 bob other 0   Apr 25 16:29 file4
-rw-r--r-- 1 bob other 0   Apr 25 16:29 file5

[bob@host ~]

Now we see three more files called .ssh, .bash_profile, and .bashrc. These are used for configuration of programs. We also see . and .., these are both directories. The . stands for the current directory, and the .. stands for the previous directory.

There are other arguments that can be used with the ls command, you can type 'man ls' for more information.

The 'cd' command

This command is used to change to a different directory. From the previous example we have two directories that we can move into called dir1 and dir2. If we wanted to change into dir1 we would type:

[bob@host ~] cd dir1
[bob@host ~/dir1] 

See how we are now in the dir1 directory? Let's now use the command 'ls -la' to see what files are in this directory.

[bob@host ~/dir1] ls -la
total 6
drwxr-xr-x 2 bob other 512 Apr 25 16:52 .
drwxr-xr-x 5 bob other 512 Apr 25 16:29 ..
-rw-r--r-- 1 bob other 13 Apr 25 16:52 linux

[bob@host ~/dir1]

You may be wondering how you get back to the previous directory. This is when the .. directory comes into play.

[bob@host ~/dir1] cd ..
[bob@host ~] 

As you see, we told cd to change to .., which stands for the previous directory, and it places us back in the home directory for bob. (Note: A user's home directory is often denoted by the tilde, ~). Had we told cd to change to . we would remain in the current directory.

The 'pwd' command

Sometimes it is needed to know the full path of the directory that you are in and your command prompt isn't set up to give you that information. You can use the command pwd to help you with this problem. Here is an example:


[bob@host ~/dir1] ls
total 6
drwxr-xr-x 2 bob other 512 Apr 25 16:52 .
drwxr-xr-x 5 bob other 512 Apr 25 16:29 ..
-rw-r--r-- 1 bob other 13 Apr 25 16:52 linux

[bob@host ~/dir1] pwd
/home/bob/dir1

[bob@zedd ~/dir1]

Here the command prints to the screen for us the full path of the current working directory.

Basic commands for managing files

Linux has some basic commands that you should learn to manage your files. You can copy, delete, move/delete files and also create directories. We will show you how to use each of these and will also talk about what links are and how to use them.

The 'cp' command

This command is used to copy files. If you have ever used the copy command in MS-DOS you will find this somewhat familiar. Let's explain the basic uses for the cp command.

To copy a file to another location you would use cp in it's most basic form: cp source target. For example,


[bob@host ~] ls
total 10
drwxr-xr-x 4 bob other 512 Apr 26 14:57 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 176 Apr 22 16:09 .bash_profile
-rw-r--r-- 1 bob other 124 Apr 22 16:09 .bashrc
drwxr-xr-x 2 bob other 512 Apr 25 16:52 dir1
-rw-r--r-- 1 bob other 5 Apr 26 14:57 file1
    
[bob@host ~] cp file1 file2
[bob@host ~] ls
total 10
drwxr-xr-x 4 bob other 512 Apr 26 14:57 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 176 Apr 22 16:09 .bash_profile
-rw-r--r-- 1 bob other 124 Apr 22 16:09 .bashrc
drwxr-xr-x 2 bob other 512 Apr 25 16:52 dir1
-rw-r--r-- 1 bob other 5 Apr 26 14:57 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:57 file2
    
[bob@host ~]

Like the ls command you can have several switches that change the way the program operates. Let's take a look at a few common ones...

cp -i -i tells cp to use interactive mode. Which will prompt you if it needs to overwrite a file.
cp -r source target -r will copy a directory and all its contents.
cp -R source target -r will copy a directory and all its contents, but pipes are only replicated not read from.
cp -p -p will preserve the owner group id, permissions, etc. The user copying the files must be of equal or higher access to do this.

For more information about the cp command, run the command: man cp

The 'rm' command

This command is used to delete files and is similar to the MS-DOS del command.

The basic usage of the command is 'rm filename', but there are other arguments that can be used with the command. For instance, you must use a switch (argument) to specifically delete directories.

rm -i -i tells rm to use interactive mode. Which will prompt you to delete each file.
rm -r,-R -r and -R tells rm recursively removes a directory and all its subdirectories. This switch must be used to delete a directory whether it is empty or not.

For more information about the rm command, run the command: man rm

The 'mv' command

The mv command moves a file or directory to another location, or renames the file if in the current directory. The basic usage of the command is:

mv source target

mv -i -i tells mv to use interactive mode. Which will prompt if there is already a file of name target.

For more information about the mv command, run the command: man mv

The 'mkdir' command

This command simply creates a new directory. To use it simply type:

mkdir dirname

You can create a subdirectory tree using the -p switch,

mkdir -p dir/sub1/sub2

will create a directory named dir in the current directory and then a subdirectory called sub1 in dir and then another subdirectory will be created in sub1 called sub2.

For more information about the mkdir command, run the command: man mkdir

Basic commands for managing file permissions

This command will change the owner of a file, either by the user or the group. To use the command you must be either the current owner of the file or have higher access, such as super-user.

The 'chown' command

Changing the owning user

[bob@host ~] ls -la
total 16
drwxr-xr-x 4 bob other 512 Apr 29 16:17 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 176 Apr 22 16:09 .bash_profile
-rw-r--r-- 1 bob other 124 Apr 22 16:09 .bashrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2

[bob@host ~]

Lets change the owner of the file file1 to the user janet.

[bob@host ~] chown janet file1
[bob@host ~] ls -al
total 16
drwxr-xr-x 4 bob other 512 Apr 29 16:17 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 176 Apr 22 16:09 .bash_profile
-rw-r--r-- 1 bob other 124 Apr 22 16:09 .bashrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 janet other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2

[bob@host ~]

From this example you can see that the file is now owned by the user janet. Please take note that this will only work if the user exists, and the user must be a member of the group.

Changing the group

If you need to specify a different group we can change it by using the chown command, specifying the group. So if janet was in the group www we can set her as the owner of the file file1 by using:

[bob@host ~] chown janet:www file1

The colon ':' separates the user and the group.

For more information about the chown command, run the command: man chown

The 'chmod' command

This command is used to change the permissions that are set on each file. The specify which user(s) can have read/write/execute access for a file or directory. There are three different ways of setting read/write/execute access: user, group, and other.

   User: The settings for the specific user owning the file.
   Group: The settings for the group owning the file.
   Other: The settings for everyone else that isn't a user/group owner of a file.

You can see the individual settings for a file by using ls -l filename:

[bob@host ~] ls -l file2
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2

We are interested in the first bit of information here, -rw-r--r--. The first space tells what type of file we have here, common characters for the first space are d for directory, l for a symbolic link, or just - for a regular file -- as we have in the example. The next three spaces give us the read/write/execute access for the user owning the file. Here the user bob has read and write access to the file; r = read, w = write, x = execute, and - = empty. The next three spaces are the settings for the owning group and the very last three spaces are the settings for a user/group that does not own the file.

Lets break down the example so you may see it more clearly.

File access.jpg

This should make things a little more clear. The next thing you will need to know is how to set the r,w,x or - to change the files permissions. This is done by using numbers to represent the r,w,x or -. Here is an example of the command chmod 755.

755 ex.jpg

This should give you a pretty good idea of how we get the individual numbers for the permissions. The 7 sets the modes rwx for the user owning the file, and the two 5's set the modes r-x for the group owning the file and everyone else.


For more information about the chmod command, run the command: man chmod

Working with Input/Output

Up until now we have talked about using commands with parameters. The commands have not really needed much more than a few switches added to the end of them to have the program perform different tasks. There are however more ways of using commands together either through pipes or redirection of input and output that can help us get things done quicker and have output formatted the way that we want it to be. The options that you use in your programs will not always be the same and a lot of the time you will not know what you need to send a program as arguments to have it do what you want. In this chapter we will discuss a little about the different types of input and output, yes there are different kinds, and how to use them.

Input/Output Redirection

Let's say that you would like to have a list of all the files in a certain directory stored in a text file. How would you do that without opening a text editor and manually typing the name of each file into the editor? There is a much quicker way to accomplish this task.

Before we go on, I'd like to talk a little about input and output. All the output that you have seen so far has been the results of commands that get printed to the screen. There are other ways of using output, such as redirecting the output to a file or program. The type of input that we have used in our commands/programs has been from the keyboard, we can also get input from other places. Sure just using standard input and output is great and all, but it gets a little annoying if the size of the input is to big to type in to the keyboard all at once. This is where someone got the great idea of allowing us to redirect output and input to and from files. Using this method allows us to get information from a file and have the program use it and also allows us to save output for later use.

Linux commands use three different types of I/O streams to do everything. These three types of I/O are called stdin, stdout, and stderr.

stdin This is the basic input stream, information received by the command. Usually it is from the keyboard.
stdout This is the basic output stream, information sent by the command. stdout usually is to the monitor.
stderr This is the error stream, error messages are usually sent to the monitor.

So let's continue with our problem from above. To store the directory listing in a file all we have to do is redirect the output of the ls command to a file. The file name can be what ever we want it to be and will be created after the ls is finished and ready to output its results. See this example:

[bob@host ~] ls -al
total 14
drwxr-xr-x 4 bob other 512 May 29 11:45 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 176 Apr 22 16:09 .bash_profile
-rw-r--r-- 1 bob other 124 Apr 22 16:09 .bashrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5   Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5   Apr 26 14:59 file2

[bob@host ~] ls -al > list.txt
[bob@host ~] ls -al
total 16
drwxr-xr-x 4 bob other 512 May 29 11:45 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 176 Apr 22 16:09 .bash_profile
-rw-r--r-- 1 bob other 124 Apr 22 16:09 .bashrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5   Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5   Apr 26 14:59 file2
-rw-r--r-- 1 bob other 485 May 29 11:45 list.txt
[bob@host ~]

You won't see any output from the command, that is because we redirected the output to a file instead of the screen. The > in the command means to redirect the output. If you want to see the listing that we created you can type 'cat list.txt', this will print the contents of list.txt to the screen.

[bob@host ~] cat list.txt
total 14
drwxr-xr-x 4 bob other 512 May 29 11:45 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 176 Apr 22 16:09 .bash_profile
-rw-r--r-- 1 bob other 124 Apr 22 16:09 .bashrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5   Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5   Apr 26 14:59 file2
-rw-r--r-- 1 bob other 0   May 29 11:45 list.txt

[bob@host ~]

Let's do a little example on input redirection. We have a list of names stored in a file that we would like to have sorted in alphabetical order. We can sort the list using the sort command and input redirection. The sort command already has an option to accept a file to sort, but if the programmer had not included this we could still get the same effect using the input redirect. Let's see an example...


[bob@host ~] cat names.txt
Dustin
Brian
Carly
Shannon
Anna
Carly
Courtney
John

[bob@host ~]

Here we have the unsorted file, now lets use sort on it.

[bob@host ~] sort < names.txt
Anna
Brian
Carly
Courtney
Dustin
John
Shannon

[bob@host ~]

And there we go, it prints the list of names from the file in order. Let's try combining the input and output redirection by using the unsorted names file and output the sorted list of names to a file.

[bob@host ~] ls -al
total 16
drwxr-xr-x 4 bob other 512 May 29 13:02 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 176 Apr 22 16:09 .bash_profile
-rw-r--r-- 1 bob other 124 Apr 22 16:09 .bashrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5   Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5   Apr 26 14:59 file2
-rw-r--r-- 1 bob other 52  May 29 12:44 names.txt

[bob@host ~] sort < names.txt > sorted.txt
[bob@host ~] ls -al
total 18
drwxr-xr-x 4 bob other 512 May 20 13:02 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 176 Apr 22 16:09 .bash_profile
-rw-r--r-- 1 bob other 124 Apr 22 16:09 .bashrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5   Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5   Apr 26 14:59 file2
-rw-r--r-- 1 bob other 52  May 29 12:44 names.txt
-rw-r--r-- 1 bob other 52  May 29 13:02 sorted.txt

[bob@host ~] cat sorted.txt
Anna
Brian
Carly
Courtney
Dustin
John
Shannon

[bob@host ~]

There are more redirectors that you can use here is a list of them.

> file Redirects standard output stream to file.
2> file Redirects standard error stream to file.
>> file Redirects standard output stream to file, appending output to the file if the file already exists.
2>> file Redirects standard error stream to file, appending output to the file if the file already exists.
&> file Redirects standard output and error streams to the file.
< file Redirects standard input stream to the file.
<< text Reads standard input until a line matching text is found, at which point end of file is posted.

What are pipes?

Linux gives us the nice functionality of pipes. Pipes let us join two or more commands together by taking the output from one command and using it as the input for another one. One way of thinking about pipes is that you put something into the end of the pipe and you get something else from the other end.

For example, have you ever tried to view the files in a directory and it didn't all fit on the screen at one time? Wouldn't it be nice if you could have the listing stop so you could scroll through it? Well you can if you use pipes. Instead of just typing ls -al /etc:

   ls -al /etc | less 

What this does is the ls command is executed and then the output is sent to a program called less instead of just being sent to standard output (the screen). Each command is separated using the '|' key. The less program takes input and then outputs to the screen one page at a time. Now we can hit the spacebar to view the output page by page, we can even scroll back and forth using the arrow keys. Just enter 'Q' when you want to quit.

Lets say that we have a directory filled with various types of files and we just want to get a list of all the files that end in '.txt' and then have them sorted. We can use pipes to do this, here is the command that we would type:

   ls -la | grep '.txt' | sort 

In this example what happened is the ls command passed its output to the grep command and then grep passed its output to the sort command. grep is a program that searches through given input for a specific pattern, in this case the pattern was .txt, giving us a list of all the files that end in .txt. We then pass that list to the sort command to put everything in order, and then we get the final output to the screen.

There are many other ways of using pipes and you will probably find them as you continue to use Linux.

Editing Files

Linux usually comes with many text editors, common ones include vi and emacs. These two editors in particular have separated Linux users into two groups that fight over who has the better editor. This tutorial will focus on vi, mainly because vi is found on most Linux installations by default and vi is considering one of the fastest editors around. We will show you most of the basic commands that you need to know to get started and feel comfortable.

Using vi

vi, (pronounced vee - eye), is a line by line text editor written in 1976 by Bill Joy. There are generally three types of modes that you are in while using the editor. One of the modes is called the command mode, in this mode you will enter different commands to the editor to do things such as navigating the lines of the file, searching for patterns, deleting lines and/or individual characters, etc. We will tell you some of the more command commands later on, generally you only really need to know about 15 or so commands to edit a file and do basic functions on it. Another mode that you will spend a lot of time in is the edit mode. In edit mode you edit the file that you are working on by simply entering text. You will always have to be in this mode to actually add to the file. And the other mode that is in vi is the line (or EX) mode. In this mode you will enter commands by first typing the colon (:) and then the command. This mode is pretty much considered just the basic command mode. That's pretty much all you need to know about the different modes of vi.

So let's start working in vi! You can start the editor with an empty buffer by simply entering vi into the command-line or start it with a file by entering 'vi filename'.

Using the command mode

Upon starting the editor you will be put into command mode. Let's see how we can use this mode.

Some commands that are used in vi can be entered by first entering a ':' into the editor. This will change the line of the bottom of the editor to a ':'. Most other commands that we will be using you can just enter then in the editor while in the default command mode. You can also get to command mode by hitting the ESC key several times. Lets look at the basic commands that we can use while in this mode.

Saving and quitting the editor

:w Save the file
:w! filename Save the current buffer to filename
:q Quit the editor
:q! Quit the editor, abandoning all changes
:wq Save the current file and quit

Navigating the file

:n Move the the n-th line in the file
nw Move n words to the right
w Move to the beginning of the next word
e Move to the end of the next word
b Move to the beginning of the previous word
h Move the cursor one character to the left (May also be left arrow key)
l Move the cursor one character to the right (May also be the right arrow key)
j Move the cursor up one line (May also be the up arrow key)
k Move the cursor down one line (May also be the down arrow key)
0 Move the cursor to the beginning of the line
$ Move the cursor to the end of the line
G Move the cursor to the end of the file
% When the cursor is over a [,],(,),{, or } use this command to move the cursor to either the beginning or closing [],(), or {}.
Ctrl-f Move the cursor one page forward
Ctrl-b Move the cursor one page back
Ctrl-d Move the cursor half a page forward
Ctrl-u Move the cursor half a page back
Ctrl-m Move the cursor to the first character on the next line

Replacing/Deleting Text

dd Delete the current line
ndd Deletes n lines
dw Delete the word under the cursor
ndw Delete n words
x Delete the character under the cursor
nx Deletes n characters
cw Change a word, deletes the word under the cursor and puts you in insert mode
cc Change a line, deletes the current line and puts you in insert mode
s Change a character under the cursor
u Undo the last change
J Join the following line with the current one
U Undo all changes to current line
D Delete from the cursor to the end of the line
C Change the current line from the cursor - deletes everything after the cursor

Copying/Cutting/Pasting

yy Yank(copy) the current line into a buffer
nyy Yank n lines into a buffer
ynw Yank n words into a buffer
dd Deletes the current line, also stores it into a buffer. ndd deletes and stores n lines
"ayy Yanks the current line into a buffer called a, a could also be any other letter
n"ayy Yanks n lines and puts them into a buffer called a. Again a could be any other letter
P Pastes the buffer before the current line
p Pastes the buffer after the current line
"aP Paste the buffer 'a' before the current line
"ap Paste the buffer 'a' after the current line

Inserting Text

:r filename Add the contents of filename below the current line
i Insert text at current position of the cursor
I Insert text at the beginning of the current line
a Insert text at position next to the cursor
A Insert text at the end of the current line
o Open a new line of text below the current line and change to insert mode
O Open a new line of text above the current line and change to insert mode

Searching

/pattern Search the file downwards for pattern
?pattern Search the file upwards for pattern
n Repeat the last search
N Repeat the last search except in the reverse direction

Miscellaneous Commands

Ctrl-g Provides information on the current file such as filename, line number at current position, total lines in the file and the percentage of the current cursor position
Ctrl-[ Cancel the current command
. Repeat the last command
>> Indent the text to the left
<< Indent the text to the right
Ctrl-l Clear and redraw the screen

Other Editors

You should now have a grasp on the basic workings of vi and its commands. It will take some time to learn all the commands that are given here (which are not all the commands available to you), and feel comfortable working with the editor. But given some time you will see that the editor is very powerful and can do operations very quickly. Please understand that vi is not the only editor that is available for Linux. You may want to try nano, its very similar to the MS-DOS version of edit and can do mostly everything that vi can do, although it is not as fast.