Jamesfette.com

 Linux Redirection and HistorySSH into your Linux ComputerOpen PuTTY - you should be able to click the Windows icon in the bottom left of your screen and search for putty.exe.In the “Host Name (or IP address)” field, enter.username@sthenry. - (username is your first initial and last name, all lowercase)And press Enter.Put in your password - lastname2019Input/Output RedirectionIn previous exercises, we’ve used the pipe symbol “|” to redirect output from one command to another. The pipe symbol is a form of redirection.There are three types of input and output.I/O NameAbbreviationFile Descriptor NumberStandard inputstdin0Standard outputstdout1Standard errorstderr2We use the greater than sign to redirect output. A single greater than sign writes or overwrites the file if it exists. >Double greater than signs appends data to a file if it exists. >>We use the less than sign to redirect input. <You can think of these symbols like arrows for the direction you want your data to flow.$> cd books$> ls -l > library.txtNo output is expected here! The output was sent to the library.txt file.$> cat library.txtroot@ip-172-31-8-234:~/books# cat library.txttotal 5200-rw-r--r-- 1 root root 182057 Mar 4 2018 a_christmas_carol.txt-rw-r--r-- 1 root root 20631 Jan 9 16:41 a_modest_proposal.txt-rw-r--r-- 1 root root 804335 Mar 19 2018 a_tale_of_two_cities.txt-rw-r--r-- 1 root root 173701 Jan 9 18:45 alice_in_wonderland.txt-rw-r--r-- 1 root root 883160 Nov 1 2018 dracula.txt-rw-r--r-- 1 root root 450783 Jan 13 2018 frankenstein.txt-rw-r--r-- 1 root root 163464 Dec 3 18:00 jekyll_and_hyde.txt-rw-r--r-- 1 root root 0 Jan 16 03:08 library.txt-rw-r--r-- 1 root root 1053440 Nov 1 2018 little_women.txt-rw-r--r-- 1 root root 799738 Nov 12 16:00 pride_and_prejudice.txt-rw-r--r-- 1 root root 68099 Oct 14 2018 the_papers_of_thomas_jefferson.txt-rw-r--r-- 1 root root 667905 Dec 2 2018 walden.txt-rw-r--r-- 1 root root 26156 Jan 21 2019 works_of_edgar_allan_poe.txtLet’s write something new to that file and use “>” to send more information.$> hostname > library.txtNo output is expected here! $> cat library.txtroot@ip-172-31-8-234:~/class/books# cat library.txtip-172-31-8-234When we used the “>” symbol, it overwrote what was in library.txt with the output of the hostname command. If we want to append data to a file, we must use “>>”.$> lsb_release -a >> library.txtNo LSB modules are available.$> cat library.txtip-172-31-8-234Distributor ID:UbuntuDescription:Ubuntu 18.04.3 LTSRelease:18.04Codename:bionicWe have appended data, but notice the error message “No LSB modules are available.” This printed but the output of the command went to the file. If we want both the standard output and standard error to go to our file, we must run the following command.$> lsb_release -a &>> library.txtNo output is expected here!$> cat library.txtroot@ip-172-31-8-234:~/class/books# cat library.txtip-172-31-8-234Distributor ID:UbuntuDescription:Ubuntu 18.04.3 LTSRelease:18.04Codename:bionicNo LSB modules are available.Distributor ID:UbuntuDescription:Ubuntu 18.04.3 LTSRelease:18.04Codename:bionicOne more exercise for output - we can split standard out and standard error to two different files from the same command.$> lsb_release -a > lsb_out 2> lsb_errorNo output is expected here!$> cat lsb_outroot@ip-172-31-8-234:~/class/books# cat lsb_outDistributor ID:UbuntuDescription:Ubuntu 18.04.3 LTSRelease:18.04Codename:bionic$> cat lsb_errorroot@ip-172-31-8-234:~/class/books# cat lsb_errorNo LSB modules are available.Here’s an example of using input redirection. We call the sort command and redirect the file lsb_out as input to sort.$> sort < lsb_outroot@ip-172-31-8-234:~/class/books# sort < lsb_outCodename:bionicDescription:Ubuntu 18.04.3 LTSDistributor ID:UbuntuRelease:18.04This is the same as.$> cat lsb_out | sortroot@ip-172-31-8-234:~/class/books# sort < lsb_outCodename:bionicDescription:Ubuntu 18.04.3 LTSDistributor ID:UbuntuRelease:18.04Redirecting with /dev/nullThere may be times that you don’t want to see any error messages from your commands. Instead of redirecting to a file to hide the error output, we can redirect to a special device called the “null device” or /dev/null. It’s just a black hole for data, it disappears to never be seen again.$> lsb_release -a 2> /dev/nullroot@ip-172-31-8-234:~/class/books# lsb_release -a 2> /dev/nullDistributor ID:UbuntuDescription:Ubuntu 18.04.3 LTSRelease:18.04Codename:bionicIf we only want to see the error message, we can send standard out to /dev/null.$> lsb_release -a > /dev/nullroot@ip-172-31-8-234:~/class/books# lsb_release -a > /dev/nullNo LSB modules are available.Shell HistoryEvery command you’ve typed is recorded into the history of your profile. Everything is stored in the ~/.bash_history file.$> cd ~$> cat .bash_historyYour output will show LOTS here. This is everything that you’ve typed since you’ve started the class.We can use the history command for a more useful display of information.$> history | tail -n 5Your output will show the last 5 commands ran by your user.Notice that each line is preceded by a number. This line number can be used to re-run commands you’ve ran in the past.Use two exclamation points to rerun the last command.$> !!Your output should show the last 5 commands that were run.Use one exclamation point and a line number to execute the command that occurred at that line number.$> !45Use one exclamation point and a negative number to execute a command that happened X previous commands ago.$> !-5You will run the command that shows up on line 45 of your history.$> !histYou will run the most recent command that starts with “hist” which should be the last 5 commands last ran by your user.The number of items saved in your history is determined by the variable HISTSIZE. You can see the value by running this command.$> echo $HISTSIZEroot@ip-172-31-8-234:~# echo $HISTSIZE1000If you want to update this value, we want to edit ~/.bashrc and update the line that saysHISTSIZE=1000 to whatever value you want. You’ll then need to reload the file for the change to go into effect.$> nano ~/.bashrcMake your changes and then press CTRL-X, then Y, ENTER.$> source ~/.bashrcNo output will come from this command.$> echo $HISTSIZEThis should be the value you set in the file. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download