The purpose of this assignment is to explore Linux commands, file systems, and shell scripting.
cat > f1 this is a test ctrl-dNow type "cat f1" and press return. You should get your text back, i.e., "this is a test". The command above redirected concatenation ("cat") into a file you called f1; and ctrl-D terminated the "cat" operation and closed the file.
mkdir -p level2a/level3aDo a ls; then cd down to level3a. Do ls; you should see that there is nothing there.
cat ../../level2/level3/f1Now cat that file into a new file you will call f1a
cat ../../level2/level3/f1 > f1aNow type ls to see what is in the current directory. Type pwd to see the path to the current directory.
gpruesse@otter:~$ ls my-file ls: my-file: No such file or directory gpruesse@otter:~$ touch my-file gpruesse@otter:~$ ls -l my-file -rw-r--r-- 1 gpruesse users 0 2017-10-02 21:22 my-file gpruesse@otter:~$ touch my-file gpruesse@otter:~$ ls -l my-file -rw-r--r-- 1 gpruesse users 0 2017-10-02 21:23 my-fileHere the command touch creates an empty file if the file does not already exist and it only changes its modification date if it does. The command ls lists all the files and directories that are under the directory you are in. The option -l displays different aspects of the files being listed. (See below)
gpruesse@otter:~$ ls my-file ls: my-file: No such file or directory gpruesse@otter:~$ cat > my-fileThe > sign directs the following into the file.
This is the first line. This is the second line. ^Cntrl-dTerminate with Cntrl-d.
gpruesse@otter:~$ cat my-fileWithout the > sign, it concatenates the file to "standard-out" filestream -- i.e., it prints contents of file to the screen.
This is the first line. This is the second line. gpruesse@otter:~$ cp my-file my-file2 gpruesse@otter:~$ cat my-file my-file2 This is the first line. This is the second line. This is the first line. This is the second line. gpruesse@otter:~$ cat my-file my-file2 > my-file3The > sign redirects into a file
gpruesse@otter:~$ cat my-file3 This is the first line. This is the second line. This is the first line. This is the second line.cat is a command that assumes different functions in different situations. It can be used
gpruesse@otter:~$ emacs my-file &The & sign places a job to the background. If you don't put the ampersand at the end of the emacs command, the terminal window will run emacs in the foreground, in which case you won't be able to run other commands from it.
[1] 4771At this time, an external window should pop up.
gpruesse@otter:~$ jobs [1]+ Running xemacs my-file &(b) In the window that opens, type in the text normally as if you were typing in a Word document.
gpruesse@otter:~$ vi my-fileThere's no need to send vi to the background because unlike emacs, it opens the editor in the terminal and not in an external window.
man grepon the command line. If you want all the man pages that mention a certain word, like "print", type the command
man -k printThere's rather a lot, and it scrolls past quickly. To get it to show you a page at a time, pipe the command into "more". The pipe is the |, and it is like a pipeline, directing the output of one command (in this case, the text output from the "man -k print" command) into another command (in this case "more", which is exactly like the "less" command, as in less is more ;-). Use it and see what more does to text. Hit the spacebar repeatedly; what happens? When you've seen enough, you can use 'q' to quit "more".
gpruesse@otter:~$ diff -q einstein1.txt einstein2.txt Files einstein1.txt and einstein2.txt differNow let's see the lines where they differ. For this we'll simply invoke the diff command without any options.
gpruesse@otter:~$ diff einstein1.txt einstein2.txt 9,10c9,10 < Jack Rosenberg remembers the time Einstein’s coworker asked him to < turn the tables and help give the famous scientist a present. --- > Jack Rosenberg remembers the time Einstein’s colleagues asked him to > turn the tables and help give the famous scientist a gift...... You'll see that the differences are given line by line together with line numbers. Sometimes, it is useful to see the differences in context, meaning displaying the differences with some lines around it. This is especially valuable if your text is sparse and it's hard to understand the location of the differences without seeing what's around it. This function can be invoked using the -U option. You can decide how many lines you want to be displayed around the lines with differences.
gpruesse@otter:~$ diff -U 1 einstein1.txt einstein2.txt --- einstein1.txt 2007-09-19 16:47:39.000000000 +0300 +++ einstein2.txt 2007-09-19 16:48:34.000000000 +0300 @@ -8,4 +8,4 @@ Albert Einstein was well known in Princeton for his generosity. But - Jack Rosenberg remembers the time Einstein's colleagues asked him to - turn the tables and help give the famous scientist a gift. + Jack Rosenberg remembers the time Einstein's coworkers asked him to + turn the tables and help give the famous scientist a present. @@ -22,3 +22,3 @@ Einstein's most important findings, the theory of special relativity, - plans are now under way to remember the findings of the man who + plans are now under way to remember the discoveries of the man who revolutionized physics in 1905 by redefining scientists' perception ofIf you have time during the lab, explore awk, head, tail, and more on the pipe, in the following section.
gpruesse@otter:~$ head -3 columns2.txt (first three lines appear here)This is almost what we want except we would like only the ninth column (i.e. the numbers column) to be displayed. We do this using a command called awk. awk is really a pattern searching language in itself, which is very helpful for certain tasks. awk is ordinarily used to extract columns from files in the following way :
gpruesse@otter:~$ awk '{print $5}' columns2.txt (this is not the data in columns2) -1090.13343774 -1090.20757070 -1090.24296462 -1090.25563488 -1090.27085564 -1090.27693129 -1090.28213580 -1090.29131927In awk and in a lot of shell scripting, the direction of the quotation marks is very important. Whatever’s inside the ´{ }´ is interpreted as the instruction given to awk, which in our case is to print the fifth column of the file, designated by $5. However, this isn’t what we wanted to do. Instead of displaying the fifth column of the entire file, we are only interested in displaying the fifth column of the first three lines. A very common construct in Linux when we want to process the outcome of a command using a second command is the pipe, which is the vertical line |. Instead of having awk read from a file, we can pipe the output of head to awk directly without having to save it to a file first.
gpruesse@otter:~$ head -3 columns.txt | awk '{print $5}' (this is not the data in columns2) -1090.13343774 -1090.20757070 -1090.24296462Note that the first part of the pipe, namely the one starting with head is a full command with the file name, whereas the second part does not have the file name as the argument anymore. To add a final complication, let’s say that we are interested in displaying only the fifth column of the second line of this command. We can do this by adding another pipe with the command tail. The usage of tail is very much like that of head except that it displays the last N lines of text.
gpruesse@otter:~$ head -2 columns.txt | awk '{print $5}' | tail -1 (this is not the data in columns2) -1090.20757070The action head -2 displays the first two lines, awk then selects the fifth column of the output and finally tail -1 displays the last line of the second output. You can form a chain of pipes of arbitrary length in this way. Create your own data file of column data by cd-ing up to the top directory, and executing the command "ls -l > ~/162/labs/oslab1/column.txt" or "ls -l > ~/csci162/labs/oslab1/column.txt", depending on what you called your directory. That is, give a valid path name to your csci162 labs subdirectory for this lab.
gpruesse@otter:~$ ls -lThe -l option lists information about content, permissions, size, owner etc.
total 136 permissions links owner group size modification time name date drwxr-xr-x 2 gpruesse users 176 2007-09-18 22:03 figs -rw-r--r-- 1 gpruesse users 3048 2007-09-18 22:34 ideas.txt -rw-r--r-- 1 gpruesse users 326 2007-09-18 10:42 Makefile -rw-r--r-- 1 gpruesse users 113 2007-09-18 22:03 my-file -rw-r--r-- 1 gpruesse users 860 2007-09-18 22:31 notes.aux -rw-r--r-- 1 gpruesse users 10064 2007-09-18 22:31 notes.dvi -rw-r--r-- 1 gpruesse users 9752 2007-09-18 22:31 notes.log -rw-r--r-- 1 gpruesse users 69323 2007-09-18 22:31 notes.pdf -rw-r--r-- 1 gpruesse users 5982 2007-09-18 22:38 notes.tex -rw-r--r-- 1 gpruesse users 436 2007-09-18 22:31 notes.toc -rw-r--r-- 1 gpruesse users 563 2007-09-18 22:46 sun1.txt 7 -rw-r--r-- 1 gpruesse users 1462 2007-09-18 22:46 sun2.txt -rw-r--r-- 1 gpruesse users 1992 2007-09-18 22:46 sun3.txt -rw-r--r-- 1 gpruesse users 2640 2007-09-18 22:46 sun4.txtPerhaps you are not interested in seeing all the files in your directory but those which start with the word "sun".
gpruesse@otter:~$ ls -l sun* -rw-r--r-- 1 gpruesse users 563 2007-09-18 22:46 sun1.txt -rw-r--r-- 1 gpruesse users 1462 2007-09-18 22:46 sun2.txt -rw-r--r-- 1 gpruesse users 1992 2007-09-18 22:46 sun3.txt -rw-r--r-- 1 gpruesse users 2640 2007-09-18 22:46 sun4.txtOr equally, you want to view just those files which have a .txt extension.
gpruesse@otter:~$ ls -l *.txt -rw-r--r-- 1 gpruesse users 3048 2007-09-18 22:34 ideas.txt -rw-r--r-- 1 gpruesse users 563 2007-09-18 22:46 sun1.txt -rw-r--r-- 1 gpruesse users 1462 2007-09-18 22:46 sun2.txt -rw-r--r-- 1 gpruesse users 1992 2007-09-18 22:46 sun3.txt -rw-r--r-- 1 gpruesse users 2640 2007-09-18 22:46 sun4.txtThe asterisk(*) is called a wildcard and it is used to list files that start with, end in or contain a given pattern. Now that we are convinced that the files exist and they are not empty, let's take a look at one of them. When you try to use the command cat like we did before, you will see that the file is too long to fit into a single screen. What would be nice is to be able to control the portion of the file that is shown on screen. This can be done with the command less.
gpruesse@otter:~$ cat sun3.txt...... Runs off the screen! of the Moon is an awesome experience. For a few precious minutes it gets dark in the middle of the day. The stars come out. The animals and birds think it's time to sleep. And you can see the solar corona. It is well worth a major journey.
gpruesse@otter:~$ less sun3.txt ...... total eclipse of the Sun. Partial eclipses are visible over a wide area of the Earth but the region from which a total eclipse is visible, called the path of totality, is very narrow, just a few kilometers (though it is sun3.txt lines 1-23/32 69%Scroll using up and down arrows. Next, let’s count the number of lines, words and bytes in the file sun2.txt. This can be done using the wc command, which stands for word count.
gpruesse@otter:~$ wc sun2.txt 96 1086 5976 sun2.txtDoing research, we often find ourselves looking for a particular word or a pattern in a given file. This could be, for example, energy or result. While this search can be done by visual inspection if the file is small, for larger files, this would be impossible. Linux has a very powerful command, grep, for conducting pattern search. It takes as arguments the pattern being searched and a file name. If called without any options, it prints lines containing the pattern on the screen. Suppose we want to know the number of times the word "sun" occurs in the file sun2.txt.
gpruesse@otter:~$ grep Sun sun2.txt The surface of the Sun, called the photosphere, is at a temperature of about 5800 K. Sunspots are "cool" regions, only 3800 K (they look dark only .....Calling grep with the option -n causes the number of the line to be displayed where the given pattern occurs.
gpruesse@otter:~$ grep -n Sun sun2.txt 1:The surface of the Sun, called the photosphere, is at a temperature of 2:about 5800 K. Sunspots are "cool" regions, only 3800 K (they look dark only ..... If we are only interested in the number of occurrences of the pattern, we can use the -c option.gpruesse@otter:~$ grep -c Sun sun2.txtYou might have noticed that grep not only finds the instances of the word "sun" but also words which "sun" is a part of, such as "Sunspot" (Notice also that grep is not case-sensitive, but that can be modified through options). Suppose now that we would like to count all occurences of the word "Sun" but not Sunspot. This can again be achieved by piping the output of the grep from above to a second grep, this time using the -v option, which matches nonoccurences of the given pattern. Because we are only interested in the number, we can do a further pipe to wc, as before.gpruesse@otter:~$ grep Sun sun2.txt | grep -v spot | wc -l