function f-tutorial.4 () { clear bold cyan echo "In Which we Search for Magritte's Pipe" echo white cat << EOI So far we have made a few files, ordered a cat around, clobbered some information, and discovered one of many editors. Now, any GNU/Linux system has a mind-boggling number of files, containing an extraordinary amount of information. This is both good and bad. Finding what you are looking for can be a challenge. Several tools have been devised to make this easier. Some search directly in real time, and others use a database, updated at regular intervals. We'll start with the most sensibly named command, which uses the database approach. The command is EOI yellow echo "locate" echo white cat << EOL Now "locate" is less immediately useful on a live CD, because it does nothing without having a database to work with. On an installed system, updatedb is usually run automatically at some early morning hour, daily. Thus the database is always slightly "out of date". If you try, for example, "locate .ogg" for the first time on this CD, locate will complain. You will see: locate: /var/cache/locate/locatedb: No such file or directory which is locate's way of telling you that it has absolutely no idea how to find anything... EOL echo "If you haven't already run \"sudo updatedb\" try typing:" yellow echo echo "locate .ogg (then exit or CTRL+D, of course)" white echo unbold /bin/bash bold echo echo "We can fix that by running:" yellow echo echo "sudo updatedb" echo cyan echo "As usual in these tutorials, hit to continue, m for the menu, x for a prompt..." f-tutescape echo cyan echo "Give me back my command prompt!" echo white cat << EOB This brings up at least three interesting points worth knowing about. First: * combing the whole filesystem from "/" down to build a database can take quite a while. If we issue the command "sudo updatedb", we will be watching a blinking prompt until it finishes. Second: * updatedb needs to be run with "administrative" privileges, because it needs to access all files, and your user does not have permission to do this. That is why we put "sudo" in front of the command. Third: * having a "long" job running in the "foreground" like this is usually impractical, or just plain annoying. It takes over our current terminal - until it finishes, and gives us back our command prompt. Rather than pursue the use of "locate" now, let's see how the Unix gurus got around this issue. EOB f-tutescape echo cyan echo "Get out of my way!" white echo cat << EOJ They came up with a clever dodge called "job control". By using a few tricks, we can shove our long job out of the way, while we continue to issue other commands at the prompt. We won't go into a lot of detail about this - just give a few useful methods. The first and most common form of job control is to start a "takes a while" command with "&" appended, to "background" it immediately. So you would think, in the case of "updatedb" that we would type: EOJ yellow echo "sudo updatedb &" white echo cat << EOX Actually, because "sudo" is a special command that gives us administrative powers, this is not the right way for this case. It will work on this live CD, because sudo does not prompt for a password. On a more usual system, the problem with this command is that it immediately goes to the background, without being able to give us a password prompt. This confuses matters. To avoid this we instead type: EOX yellow echo "sudo -b updatedb" white echo echo "So as a practical exercise, type that last command in the shell below, then exit to return" echo "to the tutorial. If you do it right, you will get your prompt back, and can let updatedb" echo "do its job while continuing with this tutorial." echo echo "Throughout this tutorial, remember to \"exit\" or \"CTRL+D\" after each exercise, as usual." unbold echo /bin/bash bold echo cat << EOC This is rather a special case, of course. More often, we find ourselves in the situation where we have started a command that is taking longer than expected. The cursor is blinking at us malevolently, as if to say "You can't have your command prompt back!". We can fix that... EOC f-tutescape echo cyan echo "The trick here is to type:" echo yellow echo "CTRL+Z" echo white echo "... which stops the process in its tracks, without killing it." echo " That is not much use until we follow with:" echo yellow echo "bg" white echo echo "... which puts the process in the "background" to continue, and gives us our prompt back." echo echo "We can bring the job back to the foreground by typing:" echo yellow echo "fg" white echo echo "While the job is running in the foreground, we can either kill it with:" yellow echo echo "CTRL+C" white echo printf "Or repeat the sequence by putting it back in the \"background\" with" yellow printf " CTRL+Z" ; white ; printf ", followed by" ; yellow ; printf " bg" echo white echo "We'll try this with the ubuntu_sax.ogg file that happens to be in /usr/share/example-content." echo echo "You can access this directly, or via a link in your $HOME directory." echo echo "The one in $HOME is linked to /usr/share/example-content -" echo "so using "example-content/ubuntu_sax.ogg" from $HOME is the same as using the path above." echo echo "Cheat sheet on the next page..." f-tutescape echo cyan bold echo "Cheat sheet for job control experiments" white echo echo "At the subshell prompt below, type these commands in order, with a few seconds in between:" echo "Remember tab completion!" yellow echo echo "ogg123 -q /usr/share/example-content/ubuntu_sax.ogg" echo echo "CTRL+Z" echo echo "bg" echo echo "fg" echo echo "CTRL+C" echo cyan echo "Don't leave out the -q (for \"quiet\")," echo "or ogg123 will continue to print information to your screen while in the background :)" echo white echo "That gets confusing..." echo unbold /bin/bash bold echo echo "OK - you will have seen some output like [1]... and the command shown, and probably also a" echo "message from ALSA - the Advanced Linux Sound Architecture. These messages are harmless :)" echo echo "I suggest trying the exercise again, this time alternating CTRL+Z with the" yellow ; printf "bg" ; white ; printf " and" ; yellow ; printf " fg" ; white ; printf " commands." echo echo "Don't forget that you can stop the playing with CTRL+C while it is in the foreground." cyan echo echo "Here's another prompt:" echo white unbold /bin/bash bold echo echo "You might be wondering at this point where \"locate\" went, and why we are digressing..." f-tutescape echo cyan echo "What can locate do now?" echo white echo "If you ran updatedb as suggested earlier, it should have overcome its amnesia by now ;-)" echo "If it still can't find its database, give it a while longer to finish updating, and try again." echo echo "Let's see what " echo yellow echo "locate .ogg" white echo echo "gives us this time..." echo unbold /bin/bash bold echo echo "That should be a bit more useful than before..." echo echo "On most systems, \"locate .ogg\" would return a lot of output - much" echo "more than one screen." echo echo "So how do we handle such output?" echo echo "You will remember that we used the \"less\" pager to read long files..." echo echo "This is a different case though - we are dealing with actual output from a command." echo echo "Well, that can be fixed too..." f-tutescape echo cyan echo "Connecting Programs." printf "\n\n" white echo "You recall that the Unix philosophy is to use programs that do one thing well, and" echo "connect them together to make more complex tasks possible. One of the methods is" echo "symbolised by:" yellow echo echo " |" echo white echo "This is known as a pipe. We can feed output from one program through this \"pipe\"" echo "to another program that handles the output for us." echo echo "Try typing:" echo yellow echo "locate apt | less" echo white echo "at the prompt below. Remember that the \"less\" pager can be navigated with the space bar" echo "or arrow keys, and that you can use \"/searchterm\" to look for things in it. q for quit..." echo echo "Reminder: When you exit \"less\" you are at a prompt. Type exit or CTRL+D to return to the" echo "tutorial." echo unbold /bin/bash bold echo echo "As you see, the output of the locate command is fed to \"less\", which makes it readable." echo "Another tool makes an appearance on our next page ( well, our next screen I suppose...)" f-tutescape echo cyan echo "Grab Really Exceptional Patterns" echo white echo "The above line will make geeks' blood boil... The acronym we are introducing here is" echo yellow echo "grep" white cat << EOG No-one seems to be quite sure what it actually stands for, but you will see "Global Regular Expression Parser" , "Global Regular Expression Print" , and of course the most likely one, "GREen Parakeet". grep is really useful, whatever you decide it stands for - and you really don't need to know what a "Regular Expression" is to use it. In the simplest case, we can use grep to find words in a file. The output will show the line in which each instance of the word appears. In the shell below, check that you are in $HOME then type: EOG yellow echo "grep INX about-inx" echo white unbold /bin/bash bold echo cat << EOE Grep returns each line on which the "pattern" INX occurs in the file "about-inx" Big deal, you say... Well, let's do some more Unixish "grepping"... Suppose we want to find out information about the "apt" software installation and removal tool. We can combine the locate command with grep and less like this, for example: EOE echo yellow echo "locate apt | grep -v aptitude | less" echo white echo "What does the -v do? It is one of the useful options for grep, and means" echo "\"Don't include this pattern in the search\" - so we are eliminating \"aptitude\"" echo "in this case." echo echo "Try it below:" echo unbold /bin/bash bold echo echo "Of course, as you see, we still get some irrelevant results, but they are easy to spot." echo echo "Another useful option is -i , which makes our search \"case insensitive\". " echo echo "You are probably aware by now that GNU/Linux is picky about case :) A file named INX" echo "is not the same as a file named inx, or inX, and so on." echo echo "grep can also rather cleverly search through a directory \"recursively\" that is, " echo "including its sub-directories. For example:" echo yellow echo "grep -ir INX /usr/local/* | less" white echo echo "Try that to see - notice that grep tells you which files contain the strings it finds." echo "The \"-r\" in grep -ir of course means \"recursive\" . As it happens, all the INX and inx" echo "references happen to be in /usr/local/bin :)" echo unbold /bin/bash bold echo white echo "You might have noticed that last command introduced an extra character at the end of" echo "the file path." echo echo "This is known as a \"wild card\" or \"metacharacter\"." echo echo "If you are curious about this, try a google search on the terms" echo echo "linux bash globbing" echo echo "To keep things slightly less mind-boggling, we will just note two useful wildcards" echo "here:" echo echo " * means \"match any number of characters\"" echo " ? means \"match just one character\"" echo echo "For example, suppose you are looking for file names in the current directory ending with" echo "the string \"dm\" and only one leading character before the \"dm\"" echo echo "You can type" echo yellow echo "ls ?dm " white echo echo "More generally useful is the * wildcard - for instance you can match all .ogg files in" echo "a directory with:" echo yellow echo "ls *.ogg" white echo echo "So, as an example, to play all the Ogg Vorbis tracks in a directory you could type:" echo yellow echo "ogg123 -q *.ogg" white echo echo "... then use CTRL+Z, bg ,fg and CTRL+C to put them in the background, start and stop them," echo "and so on. A quick and dirty music list player. There are better ways in this instance, of course." f-tutescape echo cyan echo "Special Characters." white cat << EOS0 Bash has certain characters that can cause issues, because they have a special meaning. This is both useful and occasionally frustrating. The most obvious case is the "space" character. As far as bash is concerned, a space means either that the next string is a command, ( that could include seemingly unlikely things like "[" ), or an option, if preceded by " - ", or another file name, and so on. This actually makes sense, but drives former users of other operating systems nuts, because they are usually in the habit of using multi-word file names with spaces. Tip: in Unix and GNU/Linux, make a habit of naming files that have multiple words as this-file , or this_next_file or this.other.file Tip: you can tab-complete file names with spaces. So, for example, to play your innocent new video named "Blood Violence and Gratuitous Sex.avi", with mplayer, try EOS0 yellow echo "mplayer Bloo " white echo echo "You will notice that what bash shows is Blood\\ Violence\\ and\\ Gratuitous\\ Sex.avi" echo echo "Bash has \"escaped\" the spaces with backslashes for you." echo "You can do this laboriously yourself, of course, but it would be a bit pointless" echo "if tab completion does it for you." echo echo "Another way to make spaced file names understood in bash is to quote them." f-tutescape echo echo echo "Other characters that are \"special\" to Bash include:" yellow cat << EOSP ! # ; . \\ EOSP white cat << EOSP1 Because their meaning is dependent on context, there is too much here to cover in this tutorial. A google search on linux bash special characters will turn up some further explanation - for example http://tldp.org/LDP/abs/html/special-chars.html EOSP1 f-tutescape echo cat << EOCH Some are very useful on the command line. For example we can use the bash "history" with "!" . ! is one of those context-dependent special characters. It can also mean "not" and is often so used in bash script constructs. Using it with the Bash history is more useful from the command line... EOCH yellow cat << EOOGG !ogg EOOGG white echo "will execute the last command we used starting \"ogg\" ( ogg123 for instance)." yellow cat << EODOLL !$ EODOLL white cat <