Lab 2 - 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.

We'll go through some of the most common Linux commands that are used on a daily basis by Linux system administrators. 


  Part 1 - Command retrieval and line editing  

We will run a few commands as follows: 

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

    

  • Route command is used to show/manipulate the IP routing table. It is primarily used to setup static routes to specific host or networks via an interface.


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


  • uptime: It is used to find out how long the system is active (running).
  • ls: This command lists file and directories within the file system, and shows detailed information about them.
  • date: This command is pretty self-explanatory, it is used to display the system date and time. 
  • sync: It used to synchronize cached writes to persistent storage. 


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

  • Some handy time-saving shortcuts tips in linux is to use the arrow keys. Imagine typing in a long linux command and making a typo. This is one of the frustrating things about a command line interface. Wouldn't it be nice to have a way to recall a previous command, make a few changes and enter the new command? This is what the up arrow is for. 

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.



  Part 2 - Using history  

The history command is used to view the previously executed command. In Bash shell history command shows the whole list of the command. 

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



    2. You can use the history command by just typing its name: history

  • Here, the number(termed as event number) preceded before each command depends on the system. You may get different numbers while executing on your own system.


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


  • In the picture above, i choose the number 263 as an example. Type !263 and then press enter. 

    4. That command will run again.



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

1. First change to your home directory, in my case it's: cd /home/arnond.

  • cd: This command is one of the most important and most widely used command for newbies as well as system administrators.cd is the only way to navigate ti a directory to check log, execute a program/application/script and for every other task. 
  • Picture above shows that I'm changing the current directory.

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


  • mdkir command in Linux allows the user to create directories. This command can create multiple directories at once as well as set the permissions for the directories. 

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




  Part 4- The shell prompt  

A standard terminal usually has a rather cyptic command line prompt. This should be changed by modifying the PS1 environment variable.

1. The \u command means to show the current user of this shell. 

2. The \h command shows the hostname of this machine. 

3. The \w command means to show the full path of the current directory. This change is highly recommended, as the user 

    doesn't have to type pwd (Print Working Directory) all the time to know what directory is being used. 

4. The \$ means to display a $ or # depending on the effective UID.


 export PS1=”\u \h ” 


export PS1=”\u “


  • As anything else in the Linux system also bash prompt can be customized. We can accomplish the task by changing the values of bash PS1PS2PS3PS4 variables.


  Part 5 - Other environment variables  

The PS1 variable is only one of literally hundres of environment variables. Don't worry, you don't have to know them all! 

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



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

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/arnond/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/arnond/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/arnond/linux