Linux

Mary PW Chin 钱碧慧博士
PhD (Wales), MSc (Surrey)
E-mail:
setting up

To have a Linux environment to play with, we need one of the following:

list files
  1. Download beauty_beast.txt to your space.
    • If you are using PythonAnywhere, your space would be in the cloud, because that's where PythonAnywhere resides. So, you would need to first download to your computer, then upload to PythonAnywhere. The upload button can be found by clicking PythonAnywhere > Files. Look for the orange button at the bottom of the page.
    • If you are using your local computer, your space would be your local computer.
  2. In your console/terminal, make sure the present working directory/folder is the directory/folder where you placed the downloaded file. In case it isn't, do either one of the following:
    • change your present working directory to the directory where you placed the downloaded file: the command is 'cd'.
    • move the downloaded file to your present working directory: the command is 'mv'.
    If you are not sure how to use 'cd', type 'man cd'. If you are not sure how to use 'mv', type 'man mv'. 'man' stands for manual. It works for all Linux commands.
  3. Type 'ls'. Then type 'ls -l'. Note the difference between the two outputs. Find out more by typing 'man ls' and you will find that '-l' is one of the many options.
  4. Are you sure 'ls -l' is showing you all the files in your directory? Try 'ls -la'. Note the difference between the two outputs. Find out more by typing 'man ls' again; this time look for the '-a' option. 'ls -la' is equivalent to 'ls -al', 'ls -a -l' and 'ls -l -a'. We can combine and swap the order of multiple options as we like.
  5. Note the way I named the file. No space. Filenames should not contain any space or comma. 'beautybeast.txt' would have been less readable. To make it readable we either use underscore (beauty_beast.txt) or camel-case (beautyBeast.txt). It can even be 'beauty_and_the_beast.txt' or 'beautyAndTheBeast.txt'. No space, no comma, no dash. Underscore (_) is good, dash (-) isn't.
shortcuts, auto-completion & command history

Actually you should never need to type out filenames in full. For 'beauty_beast.txt', try typing 'b' then press . If in the present directory that's the only file beginning with 'b', you should see the full filename typed out automatically for you. In case you have more files beginning with 'b', type the second letter, 'e', and then press again. This is how we tab our way through to get the full filename without typing it all out. We type just enough by providing the clue so that Linux can uniquely identify the filename.

You should never need to type out lines you had previously entered either. Just use the up-arrow and down-arrow keys to recall lines you previously entered. You may edit the lines using left-arrow and right-arrow keys, edit to taste, then only press .

look inside text files
  1. Try
    • cat beauty_beast.txt
    • cat beauty_beast.txt beauty_beast.txt
    • head beauty_beast.txt
    • tail beauty_beast.txt
  2. Try
    • head -n 3 beauty_beast.txt
    • tail -n 3 beauty_beast.txt
    Find out more with 'man cat', 'man head' and 'man tail'. What does option '-n 3' do?
find within text files
  1. Try
    • grep sun beauty_beast.txt
    • grep Sun beauty_beast.txt
    • grep -i Sun beauty_beast.txt
    and note the difference between the three outputs. Find out more with 'man grep': what is the '-i' option for?
  2. Try
    • grep ange beauty_beast.txt
    • grep ange beauty_beast.txt | grep sweet
    • grep ange beauty_beast.txt | grep ing
    and note the difference between the three outputs. Here we have used a pipe ('|') so that we can grep twice. The first grep had beauty_beast.txt as input. The second grep had as input the output from the first grep.
  3. Try
    • grep -i song beauty_beast.txt
    • grep -i song$ beauty_beast.txt
    • grep -i ^song beauty_beast.txt
    and note the difference between the three outputs. The '$' means 'line ending with'. '^' means 'line beginning with'. '$' and '^' are regular expressions, customarily shortenned and affectionally referred to as regex. You may find out more here.
create more files
  1. Try
    • cat beauty_beast.txt beauty_beast.txt beauty_beast.txt > triple.txt
    • cat triple.txt
    • less triple.txt
    '>' redirects the output to the file, 'triple.txt'. The output no longer appear on the screen. We also see that for longer files, 'cat' output runs off the screen. 'less' lets us scroll up and down with the arrow keys. Press 'q' to exit. Look for more navigation shortcuts from 'man less'.
  2. Try
    • wc beauty_beast.txt
    • wc triple.txt
    Find out more with 'man wc'.
  3. Try
    • sed s/old/young/ beauty.txt
    • sed s/old/young/ beauty.txt > younger.txt
    • diff beauty.txt younger.txt
    Find out more with 'man sed' and 'man diff'.
  4. Try
    • sed s/as/AS/ beauty.txt
    • sed s/as/AS/g beauty.txt
    What is the difference with and without 'g'?
  5. Try
    • sed s/as/AS/ beauty.txt > without_g.txt
    • sed s/as/AS/g beauty.txt > with_g.txt
    • diff without_g.txt with_g.txt
    With the help of 'man diff', make out what's what in diff's output.

☞ more Linux!