Showing posts with label Command Line. Show all posts
Showing posts with label Command Line. Show all posts

Saturday, April 9, 2016

Command Line_Lesson_Enviroment

1.Environment
Each time we launch the terminal application, it creates a new session. The session immediately loads settings and preferences that make up the command line environment.
We can configure the environment to support the commands and programs we create. This enables us to customize greetings and command aliases, and create variables to share across commands and programs.
We'll begin by learning to use a simple, command line text editor called nano.
In the terminal, type

nano hello.txt
This will open the nano text editor.
In nano, at the top of the window, type

"Hello, I am nano."
Using the menu at the bottom of the terminal for reference, type Ctrl + O (the letter, not the number) to save the file. This is the letter "O", not the number zero.
Press Enter, when prompted about the filename to write.
Then type Ctrl + X to exit nano.
Finally, type clear to clear the terminal window. The command prompt should now be at the top of the window.
2.nano
nano is a command line text editor. It works just like a desktop text editor like TextEdit or Notepad, except that it is accessible from the command line and only accepts keyboard input.
  1. The command nano hello.txt opens a new text file named hello.txt in the nano text editor.
  2. "Hello, I am nano" is a text string entered in nano through the cursor.
  3. The menu of keyboard commands at the bottom of the window allow us to save changes to hello.txt and exit nano. The ^ stands for the Ctrl key.
  4. Ctrl + O saves a file. 'O' stands for output.
  5. Ctrl + X exits the nano program. 'X' stands for exit.
  6. Ctrl + G opens a help menu.
  7. clear clears the terminal window, moving the command prompt to the top of the screen.
learn more about nano here.
2.1.Create a file to store environment settings.
In the terminal, type
nano ~/.bash_profile
This opens up a new file in nano.
2.2.In ~/.bash_profile, at the top of the file, type

echo "Welcome, Jane Doe"
You can use your name in place of "Jane Doe".
Type Ctrl + O to save the file.
Press Enter to write the filename.
Type Ctrl + X to exit.
Finally, type clear to clear the terminal window.
2.3.see the greeting you entered.
$ source ~/.bash_profile                                                                         
Welcome, Becky 3.Bash Profile
~/.bash_profile is the name of file used to store environment settings. It is commonly called the "bash profile". When a session starts, it will load the contents of the bash profile before executing commands.
  • The ~ represents the user's home directory.
  • The . indicates a hidden file.
  • The name ~/.bash_profile is important, since this is how the command line recognizes the bash profile.
  • The command nano ~/.bash_profile opens up ~/.bash_profile in nano.
  • The text echo "Welcome, Jane Doe" creates a greeting in the bash profile, which is saved. It tells the command line to echo the string "Welcome, Jane Doe" when a terminal session begins.
  • The command source ~/.bash_profile activates the changes in ~/.bash_profile for the current session. Instead of closing the terminal and needing to start a new session, source makes the changes available right away in the session we are in.
1.Now that we know what bash profile is, let's continue configuring the environment by adding command aliases. Open ~/.bash_profile in nano.$ nano ~/.bash_profile

2.In ~/.bash_profile, beneath the greeting you created, type

alias pd="pwd"
Save the file.
CTRL+O
Press Enter to write the filename
Exit nano.
CTRL+X
Clear the terminal window.
clear

3.In the command line, use the source command to activate the changes in the current session.

source ~/.bash_profile

4.Let's try out the alias. Type

pd
You should see the same output as you would by typing the pwd command.
$ pd                                                                                        
/home/ccuser/workspace/music4.Aliases I
The alias command allows you to create keyboard shortcuts, or aliases, for commonly used commands.
  1. Here alias pd="pwd" creates the alias pd for the pwd command, which is then saved in the bash profile. Each time you enter pd, the output will be the same as the pwd command.
  2. The command source ~/.bash_profile makes the alias pd available in the current session.
Each time we open up the terminal, we can use the pd alias.
4.1.Open ~/.bash_profile in nano.

4.2.In the bash profile, beneath the previous alias, add

alias hy="history"
Save the file.
Press Enter to write the filename.

4.3.Add another alias

alias ll="ls -la"
Save the file.
Press Enter to write the filename.
Exit nano
Clear the terminal window.

4.4.In the command line, use source to activate the changes to the bash profile for the current session.
$ source ~/.bash_profile  

4.5.Let's try out the aliases. In the command line, type

hy
1  nano ~/.bash_profile                                                                      

    2  clear                                                                                     
    3  nano ~/.bash_profile                                                                      
    4  clear                                                                                     
    5  source ~/.bash_profile                                                                    
    6  hy  


4.6.Now type

ll
1  nano ~/.bash_profile                                                                      

    2  clear                                                                                     
    3  nano ~/.bash_profile                                                                      
    4  clear                                                                                     
    5  source ~/.bash_profile                                                                    
    6  hy  
5.Aliases II
hy is set as alias for the history command in the bash profile. The alias is then made available in the current session through source. By typing hy, the command line outputs a history of commands that were entered in the current session.
ll is set as an alias for ls -la and made available in the current session through source. By typing ll, the command line now outputs all contents and directories in long format, including all hidden files.
5.1.Setting environment variables.
Open ~/.bash_profile in nano.

5.2.In the bash profile, beneath the aliases, on a new line, type

export USER="Jane Doe"
Feel free to use your own name.
Save the file.
Press Enter to write the filename.
Exit nano.
Finally, clear the terminal.

5.3.In the command line, use source to activate the changes in the bash profile for the current session.
$ source ~/.bash_profile     
5.4.Type

echo $USER
Becky
This should return the value of the variable that you set. 6.Environment Variables
environment variables are variables that can be used across commands and programs and hold information about the environment.
  1. The line USER="Jane Doe" sets the environment variable USER to a name "Jane Doe". Usually the USER variable is set to the name of the computer's owner.
  2. The line export makes the variable to be available to all child sessions initiated from the session you are in. This is a way to make the variable persist across programs.
  3. At the command line, the command echo $USER returns the value of the variable. Note that $ is always used when returning a variable's value. Here, the command echo $USER returns the name set for the variable.
1.Let's learn a few more environment variables, starting with the variable for the command prompt.
Open ~/.bash_profile in nano.
$ nano ~/.bahs_profile

2.On a new line, beneath the last entry, type

export PS1=">> "
Save the file.
Press Enter to write the filename.
Exit nano
Finally, clear the terminal window.

3.In the command line, use source to activate the changes in the bash profile for the current shell session.
$ source ~/.bash_profile                                                                         
>> 

4.Let's try out the new command prompt. In the terminal type

>>echo "hello"
>>hello

5.Now type

>>ls -alt

total 16                                                                                         
-rw-r--r-- 1 ccuser ccuser  362 Apr  9 22:28 .test.bats                                          
drwxr-xr-x 3 ccuser ccuser 4096 Apr  9 22:14 .                                                   
drwxr-xr-x 3 ccuser ccuser 4096 Apr  9 22:13 ..                                                  
drwxr-xr-x 2 ccuser ccuser 4096 Aug  1  2015 artists
the prompt has changed from $ to >>.
7.PS1
PS1 is a variable that defines the makeup and style of the command prompt.
  1. export PS1=">> " sets the command prompt variable and exports the variable. Here we change the default command prompt from $ to >>.
  2. After using the source command, the command line displays the new command prompt.

In the command line, type
echo $HOME
/home/ccuser
This returns the value of the HOME variable.

8.HOME
The HOME variable is an environment variable that displays the path of the home directory. Here by typing echo $HOME, the terminal displays the path /home/ccuser as output.
You can customize the HOME variable if needed, but in most cases this is not necessary.


8.1.In the command line, type

$ echo $PATH                                                                                     
/home/ccuser/.gem/ruby/2.0.0/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin 
8.2.Type

$ /bin/pwd                                                                                       
/home/ccuser/workspace/music 

8.3.Type

$ /bin/ls                                                                                        
artists  hello.txt
9.PATH

PATH is an environment variable that stores a list of directories separated by a colon. Looking carefully, echo $PATH lists the following directories:
  1. /home/ccuser/.gem/ruby/2.0.0/bin
  2. /usr/local/sbin
  3. /usr/local/bin
  4. /usr/bin
  5. /usr/sbin
  6. /sbin
  7. /bin
Each directory contains scripts for the command line to execute. The PATH variable simply lists which directories contain scripts.
For example, many commands we've learned are scripts stored in the /bin directory.
/bin/pwd
This is the script that is executed when you type the pwd command.
/bin/ls
This is the script that is executed when you type the ls command.
In advanced cases, you can customize the PATH variable when adding scripts of your own.
9.1.In the command line type

$ env                                                                                            
GEM_HOME=/home/ccuser/.gem/ruby/2.0.0                                                            
TERM=linux                                                                                       
LC_ALL=C.UTF-8                                                                                   
PATH=/home/ccuser/.gem/ruby/2.0.0/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:
/usr/sbin:/sbin:/bin                                                                                                
PWD=/home/ccuser/workspace/music                                                                 
LANG=C.UTF-8                                                                                     
PS1=$                                                                                            
HOME=/home/ccuser                                                                                
SHLVL=1                                                                                          
UPSTART_INSTANCE=                                                                                
UPSTART_JOB=godex                                                                                
_=/usr/bin/env         
9.2.Then type
$ env | grep PATH                                                                                
PATH=/home/ccuser/.gem/ruby/2.0.0/bin:/usr/local/sbin:/usr/local/bin:
/usr/bin:/usr/sbin:/sbin:/bin
10.env
The env command stands for "environment", and returns a list of the environment variables for the current user. Here, the env command returns a number of variables, including PATH, PWD, PS1, and HOME.
env | grep PATH is a command that displays the value of a single environment variable. Here the standard output of env is "piped" to the grep command. grep searches for the value of the variable PATH and outputs it to the terminal. 
11.Generalizations
  • The environment refers to the preferences and settings of the current user.
  • The nano editor is a command line text editor used to configure the environment.
  • ~/.bash_profile is where environment settings are stored. You can edit this file with nano.
  • environment variables are variables that can be used across commands and programs and hold information about the environment.
    • export VARIABLE="Value" sets and exports an environment variable.
    • USER is the name of the current user.
    • PS1 is the command prompt.
    • HOME is the home directory. It is usually not customized.
    • PATH returns a colon separated list of file paths. It is customized in advanced cases.
    • env returns a list of environment variables.
Project:
1.Type the less command, with the filename javanese.txt. After you do this, type q to quit.
The less command is similar to cat, but is better adapted to handling larger files since files to the terminal, one page at a time.
less javanese.txt 
2.The less command is a special command that we can configure in the bash profile through the LESS environment variable.
Open the bash profile, and create and export a new environment variable called LESS, setting it equal to the option "-N". The -N option adds line numbers to the file.
Save the bash profile, exit nano, and clear the terminal window.
export LESS="-N"
3.Type the less command again now, with the filename javanese.txt. The file should appear, along with line numbers. Press q to quit.
4.return the value for the environment variable for LESS.
>> env | grep LESS                                                                               
LESS=-N 




Command Line_Lesson_Redirection

1.Redirection
Through redirection you can direct the input and output of a command to and from other files and programs, and chain commands together in a pipeline.
Let's begin by taking a closer look at input and output.
In the terminal, after the shell prompt, type
$ echo "Hello"                                                                                   
Hello 
The echo command accepts the string "Hello" as standard input, and echoes the string "Hello" back to the terminal as standard output.
2.stdin, stdout and stderr
  1. standard input, abbreviated as stdin, is information inputted into the terminal through the keyboard or input device.
  2. standard output, abbreviated as stdout, is the information outputted after a process is run.
  3. standard error, abbreviated as stderr, is an error message outputted by a failed process.
Redirection reroutes standard input, standard output, and standard error to or from a different location.
3.Your first redirect
$ echo "Hello" > hello.txt     
The > command redirects the standard output to a file. Here, "Hello" is entered as the standard input. The standard output "Hello" is redirected by > to the file hello.txt.                                                         $ cat hello.txt                                                                                  
Hello 
The cat command outputs the contents of a file to the terminal. When you type cat hello.txt, the contents of hello.txt are displayed.
4.>
$ cat oceans.txt > continents.txt                                                                
$ cat continents.txt                                                                             
Arctic Ocean                                                                                     
Atlantic Ocean                                                                                   
Indian Ocean                                                                                     
Pacific Ocean                                                                                    
Southern Ocean 
> takes the standard output of the command on the left, and redirects it to the file on the right. Here the standard output of cat oceans.txt is redirected to continents.txt.
Note that > overwrites all original content in continents.txt. When you view the output data by typing cat on continents.txt, you will see only the contents of oceans.txt.
5.>>
$ cat glaciers.txt >> rivers.txt
>> takes the standard output of the command on the left and appends (adds) it to the file on the right. You can view the output data of the file with cat and the filename.
Here, the the output data of rivers.txt will contain the original contents of rivers.txt with the content of glaciers.txt appended to it.
6.<
$ cat < lakes.txt
< takes the standard input from the file on the right and inputs it into the program on the left. Here, lakes.txt is the standard input for the cat command. The standard output appears in the terminal.
7.|
$ cat volcanoes.txt | wc                                                                         
     17      26     204                   
| is a "pipe". The | takes the standard output of the command on the left, and pipes it as standard input to the command on the right. You can think of this as "command to command" redirection.
Here the output of cat volcanoes.txt is the standard input of wc. in turn, the wc command outputs the number of lines, words, and characters in volcanoes.txt, respectively.
$ cat volcanoes.txt | wc | cat > islands.txt                                                     
$ cat islands.txt                                                                                
     17      26     204  
Multiple |s can be chained together. Here the standard output of cat volcanoes.txt is "piped" to the wc command. The standard output of wc is then "piped" to cat. Finally, the standard output of cat is redirected to islands.txt.
You can view the output data of this chain by typing cat islands.txt.
8.sort
$ sort lakes.txt                                                                                                                                                                                
Caspian Sea                                                                                      
Great Bear Lake                                                                                  
Lake Baikal                                                                                      
Lake Malawi                                                                                      
Lake Michigan                                                                                    
Lake Nicaragua                                                                                   
Lake Superior                                                                                    
Lake Taganyika                                                                                   
Lake Titicaca                                                                                    
Lake Victoria   sort takes the standard input and orders it alphabetically for the standard output. Here, the lakes in sort lakes.txt are listed in alphabetical order.                                                                       
$ sort lakes.txt | sort > sorted-lakes.txt                                                       
$ cat sorted-lakes.txt                                                                           
Caspian Sea                                                                                      
Great Bear Lake                                                                                  
Lake Baikal                                                                                      
Lake Malawi                                                                                      
Lake Michigan                                                                                    
Lake Nicaragua                                                                                   
Lake Superior                                                                                    
Lake Taganyika                                                                                   
Lake Titicaca                                                                                    
Lake Victoria    Here, the command takes the standard output from cat lakes.txt and "pipes" it to sort. The standard output of sort is redirected to sorted-lakes.txt.
You can view the output data by typing cat on the file sorted-lakes.txt.
9.uniq
$ uniq deserts.txt
uniq stands for "unique" and filters out adjacent, duplicate lines in a file. Here uniq deserts.txt filters out duplicates of "Sahara Desert", because the duplicate of 'Sahara Desert' directly follows the previous instance. The "Kalahari Desert" duplicates are not adjacent, and thus remain.
$ sort deserts.txt | uniq
A more effective way to call uniq is to call sort to alphabetize a file, and "pipe" the standard output to uniq. Here by piping sort deserts.txt to uniq, all duplicate lines are alphabetized (and thereby made adjacent) and filtered out.
sort deserts.txt | uniq > uniq-deserts.txt
Here we simply send filtered contents to uniq-deserts.txt, which you can view with the cat command.
10.grep I
$ grep Mount mountains.txt
grep stands for "global regular expression print". It searches files for lines that match a pattern and returns the results. It is also case sensitive. Here, grep searches for "Mount" in mountains.txt.
$ grep -i Mount mountains.txt
grep -i enables the command to be case insensitive. Here, grep searches for capital or lowercase strings that match Mount in mountains.txt.
The above commands are a great way to get started with grep. If you are familiar with regular expressions, you can use regular expressions to search for patterns in files.
11.grep II
$ grep -R Arctic /home/ccuser/workspace/geography
/home/ccuser/workspace/geography/deserts.txt:Arctic Desert                                       
/home/ccuser/workspace/geography/oceans.txt:Arctic Ocean                                         
/home/ccuser/workspace/geography/uniq-deserts.txt:Arctic Desert                                  
/home/ccuser/workspace/geography/continents.txt:Arctic Ocean   
grep -R searches all files in a directory and outputs filenames and lines containing matched results. -R stands for "recursive". Here grep -R searches the /home/ccuser/workspace/geography directory for the string "Arctic" and outputs filenames and lines with matched results.
$ grep -Rl Arctic /home/ccuser/workspace/geography
/home/ccuser/workspace/geography/deserts.txt                                                     
/home/ccuser/workspace/geography/oceans.txt                                                      
/home/ccuser/workspace/geography/uniq-deserts.txt                                                
/home/ccuser/workspace/geography/continents.txt  
grep -Rl searches all files in a directory and outputs only filenames with matched results. -R stands for "recursive" and l stands for "files with matches". Here grep -Rl searches the /home/ccuser/workspace/geography directory for the string "Arctic" and outputs filenames with matched results.
12.sed
$ sed 's/snow/rain/' forests.txt
sed stands for "stream editor". It accepts standard input and modifies it based on an expression, before displaying it as output data. It is similar to "find and replace".
Let's look at the expression 's/snow/rain/':
  • s: stands for "substitution". it is always used when using sed for substitution.
  • snow: the search string, the text to find.
  • rain: the replacement string, the text to add in place.
In this case, sed searches forests.txt for the word "snow" and replaces it with "rain." Importantly, the above command will only replace the first instance of "snow" on a line.
$ sed 's/snow/rain/g' forests.txt
The above command uses the g expression, meaning "global". Here sed searches forests.txt for the word "snow" and replaces it with "rain", globally. All instances of "snow" on a line will be turned to "rain".
13.Generalizations
  • Redirection reroutes standard input, standard output, and standard error.
  • The common redirection commands are:
    • > redirects standard output of a command to a file, overwriting previous content.
    • >> redirects standard output of a command to a file, appending new content to old content.
    • < redirects standard input to a command.
    • | redirects standard output of a command to another command.
  • A number of other commands are powerful when combined with redirection commands:
    • sort: sorts lines of text alphabetically.
    • uniq: filters duplicate, adjacent lines of text.
    • grep: searches for a text pattern and outputs it.
    • sed : searches for a text pattern, modifies it, and outputs it.