Lab 1 : Using the Terminal / Command Line

 Introduction 

Knowing how to use the command line efficiently will really help you get the most out of your computer. There are many ways to save time and effort when typing commands, you just need to know what they are. There are many different Linux environments available. This chapter focuses on the popular Bash shell.


1.Command retrieval and line editing

You don't need to hit the Input key to insert text because a regular Bash terminal is already in insert mode. To recall a previous command, press the up and down arrow keys. Then, press other cursor keys to make any necessary changes to that line


Getting ready

All you need for this example is a terminal running the Bash shell. Other terminals may not have these capabilities.

How to do it

We will run a few commands as follows:

The line is stored in a buffer with full editing capabilities. This buffer is sent to the OS when the Enter key is pressed.

The summary of the keys used for retrieval and editing is as follows:

1.   Type in the command route and press the Enter key.

2.   Do the same for uptime, ls, date, and sync, pressing Enter after each command.

3.   Now press the up-arrow key one time. You should see the following command: sync.

4.   Now press the up arrow two more times. You should see date and ls.

5.   Press Enter. The ls command will run again. Pressing Enter will always run the command shown.

 


How it works

The line is stored in a buffer with full editing capabilities. This buffer is sent to the OS when the Enter key is pressed.

The summary of the keys used for retrieval and editing is as follows:

·       Up arrow: It is used to scroll up the history buffer

·       Down arrow: It is used to scroll down the history buffer

·       Home: It is used to bring the cursor to the beginning of the line

·       End: It is used to bring the cursor to the end of the line

·       Delete: It is used to delete the character to the right of the cursor

·       Backspace: It is used to delete the character to the left of the cursor and shift the line

·       Left and right arrow: These are the cursor movement keys


2.Using history

The standard Bash shell includes a history function. It records each command in a database that can be shown by running the history command. In this section we have shown how this is done.

Getting ready

All you need is a Bash terminal to follow the given steps.

See the following screenshot:







How to do it

Run a few commands such as route, uptime, date, and sync.

Run the history command.

Look for a command you would like to run again, but instead of typing the command, type an exclamation point (!) and then the number next to the command as shown  in the history listing, and press Enter.

That command will run again.

How it works

Think of the command line history as a linear database. You can scroll up and down until you see the command you want. This is also helpful to recall something you did a while back. The HISTSIZE environment variable controls how many commands will be saved in the buffer.


3.Filename auto-completion

When running a command, you do not have to type the entire filename. This saves a lot  of time and effort, and also helps prevent typos.

The Tab key is used to invoke filename auto-completion. See the following screenshot:







You only need to type enough characters to make the filename you want unique, and then press Tab. If you didn't type enough characters, you will hear the console beep (in most shells). If you now press Tab again, all of the possibilities will be displayed.

Getting ready

All you need for this example is a terminal running the Bash shell.



How to do it... 

1. Change to your home directory.

2. Create a directory using the following command: mkdir Linuxbook 

3. Change to it Linuxbook using the following command: cd Linuxbook ls > file2.txt ls > file3.txt ls > file4.txt ls > unique1.txt

4. Now let's create some dummy files; run using the following command: ls > file1.txt 

5. Now type ls -la u and then press Tab. The rest of the filename "unique1.txt" will appear. Press Enter. 

6. Now type ls -al file and press Tab. Nothing will happen, and your console may beep. Press Tab again. Now all 4 filenames will appear. 

7. Add a 4, press Tab again, and then Enter. The command ls -la file4 will run.

 This may look complicated or even clumsy but if you give it a chance you will become an expert in no time at all.







 







4.The shell prompt

 

Standard terminal which only has cryptic command line prompt can be changed by modifying PS1 environment variable.

 

PS1 is the main prompt that appears before every command, it is the one that most people customize.

 


Referring to the line export PS1=”\u \h \w \$ ”

\u command shows the current user of the shell,

\h shows the hostname of the machine used,

\w was used to show the full path of current directory, it is highly recommended due to the user won’t have to type pwd(Print Working Directory) every time to know what directory is being used.

Lastly, \$ was used to display a $ or # that depends on effective UID


5.Other environment variables

The PS1 variable is only one of literally hundreds of environment variables. Don't worry,  you don't have to know them all! The following are a few very useful ones:

f  PS1: It shows and sets the command line prompt f USER: It shows the current user f HOSTNAME: It shows the current hostname for this machine f HOME: It shows the home directory of the current user

f  SHELL: It shows the current shell this terminal is running in f TERM: It shows which terminal type is being used f PATH: It shows and sets the directories where programs are searched for f PWD: It shows the current working directory

f  EDITOR: It can be set to the full path to your desired text editor for use with certain commands such as crontab -e

f  TZ: It shows and sets the time zone variable f HISTSIZE: It shows and sets the size of the history buffer

Most of these are self-explanatory; however, a few need more discussion. The PATH environment variable is where commands are searched for in the filesystem.

 The echo command is used to display the contents of a variable:



1.      Prepending a dot to the PATH means the program will be looked for in the current directory first, before searching the rest of the path. This is very useful during the code development for example. Do this by running: export PATH=".:$PATH"

2.      The EDITOR variable can be set to your favorite text editor. Most people use vi (or vim); however, you can point it to the one you want. If you change this, be sure to use the full path. To How to do it... The following are the steps to create an alias: 1. Type tput clear and press Enter. Your screen should have cleared. 2. Now enter alias cls="tput clear". Now when you run cls it will do the same thing. 3. Let's create some more. To show a long directory listing enter alias la="ls -la". Enter 'la' to run the alias. 4. To show a long listing with the most current files last enter 'alias lt="ls -latr"'. If you create an alias and then decide you no longer want it you can remove it by using the unalias command, for example, unalias cls. You can also use aliases to move around the filesystem efficiently. This is very handy and will save you an incredible amount of typing. Here are some examples: 1. mkdir /home/jklewis/linuxbook 2. alias lbook="cd /home/jklewis/linuxbook" 3. lbook change the EDITOR variable do this: export EDITOR=/lewis/bin64/kw

3.     An export can be removed by setting it to nothing:

export EDITOR=

4.     By convention, environment variables are usually written in uppercase. View the man pages and/or search Google for more information on these variables.

How it works

Think of these environment variables just as you would if you were using a programming language. In this case, the type of the variable is determined by the OS. For example,  you could type A=1 or A="This is a string".

The OS knows the difference. Also, there is variable scope. Notice I did not use export above. That means this A is local to this shell. Only exporting a variable will make it available to other shells (after sourcing the file).

6.Using aliases

Wouldn't it be nice if you could easily create a simple command without having to make a script out of it? Well, there is a way. This is done using aliases




How to do it... 

The following are the steps to create an alias: 

1. Type tput clear and press Enter. Your screen should have cleared.

 2. Now enter alias cls="tput clear". Now when you run cls it will do the same thing. 

3. Let's create some more. To show a long directory listing enter alias la="ls -la". Enter 'la' to run the alias. 

4. To show a long listing with the most current files last enter 'alias lt="ls -latr"'. 

If you create an alias and then decide you no longer want it you can remove it by using the unalias command, for example, unalias cls. You can also use aliases to move around the filesystem efficiently. This is very handy and will save you an incredible amount of typing. Here are some examples: 

1. mkdir /home/jklewis/linuxbook 

2. alias lbook="cd /home/jklewis/linuxbook" 

3. lbook 

You will now be taken to that directory. Here is something I make frequent use of on my systems: 

1. export LBOOK="/home/jklewis/linuxbook"

 2. alias lbook="cd $LBOOK"

 3. lbook 

As you can see, running lbook will take you to the directory as shown above. However, you can also use the LBOOK variable to copy files to that directory: 

1. cd /tmp

 2. touch f1.txt 

3. cp f1.txt $LBOOK 

The file f1.txt will now exist in the /home/jklewis/linuxbook directory. This becomes even more handy when extremely long filenames are used


You can list your aliases by just running alias without any parameters. Any time you find yourself constantly typing the same commands or filenames consider creating an alias for i





Powered by Blogger.