Linux and CS Systems - Getting Started

From Computer Science
Revision as of 12:41, 13 August 2019 by Jkinne (talk | contribs) (System Setup)
Jump to: navigation, search

This page is meant to help a new student in a CS course at ISU to get familiar with using Linux (in particular using the text-based terminal to interact with the system).

Reading

To begin with, read through - Linux Tutorial

System Setup

See Linux - System Setup

Try it Out

Once you have your Linux system to try out, open up the terminal (also called the shell, or command prompt), and you are ready to try out some commands. Note that all commands in Linux are case-sensitive. Also, _ is different than -, and ' is different than ".

System Information, Part 1

The following are some commands to print information about the system.

  • uptime - how long since last system reboot
  • df - information about disk free space
  • whomi - which user is currently logged in on the terminal
  • hostname - what computer are you currently running commands on
  • pwd - which directory are you inside of
  • clear - clear the terminal screen

Here is an example session from running on the CS server using all of these commands. Note that the part "cs299@cs:~>" is a prompt that is printed by the terminal, and the part after this is what was typed by the user. So for the first command, the user typed the command uptime and then pressed enter. The system then printed some information on the next line, and then printed the prompt "cs299@cs:~>" again to indicate it is ready for the next command.

You should login to one of the CS systems, open up the terminal, and try out these commands as well. If you get an error, check that you are typing the commands exactly as they are given here.

cs299@cs:~> uptime
 09:00:45 up 56 days, 19:35,  3 users,  load average: 0.32, 0.31, 0.32
cs299@cs:~> df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root      1016G  229G  778G  23% /
devtmpfs        498G     0  498G   0% /dev
tmpfs            32M  2.3M   30M   8% /run
tmpfs           498G  8.0K  498G   1% /dev/shm
cgroup_root     8.0M     0  8.0M   0% /sys/fs/cgroup
/dev/sda2       127G   22G  105G  17% /usr
/dev/sda3       127G  8.3G  118G   7% /var
/dev/sda4        64G   13G   51G  20% /tmp
/dev/sda5        10T  2.8T  7.2T  28% /u1
/dev/sda6       4.0T  685G  3.3T  18% /net
/dev/sda7        18T  6.9T   11T  40% /store
cs299@cs:~> whoami
cs299
cs299@cs:~> hostname
cs.indstate.edu
cs299@cs:~> pwd
/u1/class/cs299

Text Editors, Part 1

One thing programmers need to do is edit text files. The text files could be your programs, or they could be data files. There are many different text editors that are normally installed on Linux systems. The most popular are - vim, emacs, nano.

nano is among the easiest text editors to use, so let's get you started using nano. You first need to be logged into Linux and open a terminal. Type the following

nano hello.txt

You will now be running the nano editor and editing a file named hello.txt. Note that the interface is completely text-based. There is nothing for you to click on. Instead of clicking on menus, you have control codes that you can type to save, close, etc. The bottom of the screen lists the control codes that you can use. Each control code is typed by holding down the control key on your keyboard and then pressing a letter.

For example, hold down control and press x to Exit nano. You can type nano hello.txt again to open the nano editor again.

Now type some text, for example "Hello World". You can save your file by holding down control and pressing o. When you do this, nano prompts you to ask what the name of the file should be; you can leave it alone as hello.txt and press enter. Your file is now saved! You can type more into your hello.txt file if you wish. Use the arrow keys on your keyboard to move around inside of your text file. When you are done editing, use control-o to write out (save) your file, and then use control-x to exit nano.

Files and Directories, Part 1

With the terminal you can create, copy, move, and delete files and directories. The following are the most important commands to do this.

  • cd - change directory
  • mkdir - make a new directory
  • ls - list directory contents
  • cp - copy files
  • rm - remove files (be careful, there is no recycle bin or trash - once you delete, it's gone)
  • rmdir - remove directory
  • nano - simple text editor

And here is a transcript of the use of these commands on the CS server.

cs299@cs:~> pwd
/u1/class/cs299
cs299@cs:~> mkdir new-dir1
cs299@cs:~> mkdir new-dir2
cs299@cs:~> nano hello.txt
cs299@cs:~> ls
bin/  hello.txt  new-dir1/  new-dir2/  proto/
cs299@cs:~> cp hello.txt new-dir1
cs299@cs:~> cp hello.txt new-dir2/hello2.txt
cs299@cs:~> ls new-dir1
hello.txt
cs299@cs:~> ls new-dir2
hello2.txt
cs299@cs:~> mv new-dir2 new-dir3
cs299@cs:~> ls
bin/  hello.txt  new-dir1/  new-dir3/  proto/
cs299@cs:~> rm hello.txt 
cs299@cs:~> ls
bin/  new-dir1/  new-dir3/  proto/
cs299@cs:~> rmdir new-dir3
rmdir: failed to remove 'new-dir3': Directory not empty
cs299@cs:~> rm new-dir3/hello2.txt 
cs299@cs:~> rmdir new-dir3
cs299@cs:~> ls
bin/  new-dir1/  proto/

Shakespeare Text File

In this section we walk you through looking at a text file that contains the complete works of Shakespeare (courtesy of Project Gutenberg).

First, login to the system and open up the terminal. Next, make sure you are in your home directory and create a directory to use for this example.

cs299@cs:~> cd ~
cs299@cs:~> mkdir shakespeare
cs299@cs:~> cd shakespeare

Copy the text file from where it is stored on the CS server and use the wc command to see how many lines, words, and characters (bytes) are in the file.

cs299@cs:~/shakespeare> cp /u1/junk/shakespeare.txt .
cs299@cs:~/shakespeare> ls
shakespeare.txt
cs299@cs:~/shakespeare> wc shakespeare.txt 
 124787  904061 5589890 shakespeare.txt

You can also use the nano text editor (or whatever text editor you like) to look through the text file.

nano shakespeare.txt

More Reading

Read through another tutorial about Linux. Some options include the follow.

Commands to Know

Up to this point, the following are commands that have been demonstrated: uptime, df, whomi, hostname, pwd, clear, nano, cd, mkdir, cp, ls, rm, rmdir, wc.

Other commands to be aware of include the following.

  • head, tail - for printing out just the first few lines or last few lines from a text file.
  • grep - search a file for some particular text (grep "something" file.txt)
  • man - show manual information about a command.
  • chmod - change file/directory permissions.
  • more - list file contents, use q to quit
  • less - list file contents, use q to quit
  • du - display disk usage information for a directory (du -h -d 1)
  • free - display memory usage information (free -h)
  • finger - see who is logged on to the system
  • passwd - change your password
  • date - see the current date and time
  • cal - calendar
  • bc - binary calculator
  • locate - find files that have been used in the system. Sometimes doesn’t find a file.
  • find - search directories for files. Does actually walk the directories, so finds files locate misses. But is slower.
  • stat - information about files/directories - permissions, access dates, etc.
  • cat - concatenate, but can use to print a file, like cat hello2.txt
  • sort - sort text files
  • More commands - are in /bin, /usr/bin, /usr/local/bin, /usr/games

For all of these you can find help either online (search google for something like linux passwd) or in the terminal using the man command. For example, you can get help about passwd by typing the following.

man passwd

And then you exit the man program by typing q, and can use the arrows to scroll up or down.

Bootcamp Questions/Tasks

Here we give small tasks for you to complete. These are numbered so that we can refer to them.

Linux.0 Log in to the CS server, and run the chfn command to put your name into the system. You can leave all the other fields blank (just type enter). Also, use the chsh command to change your default shell from /bin/tcsh (or whatever it is) to /bin/bash

Linux.1 Log in to the CS server, create a directory named aboutMe in your home directory, and create a text file name.txt. In the name.txt file, give your full name.

Linux.2 In your aboutMe directory, create a file courses.txt and list which CS courses you are currently taking or plan to take in the upcoming term.

Linux.3 Log in to the CS server, create a directory named notes in your home directory, and create a text file named linux.txt and put in some links to linux tutorials or information that you have found helpful.

Linux.4 Log in to the CS server, create a directory named linux-bootcamp in your home directory.

Linux.5 In your linux-bootcamp directory, create a text file shake.txt. In shake.txt, put how many lines are in the shakespeare.txt file mentioned in the information above.

Linux.6 In your shake.txt file, add information about how many times "the" appears in shakespeare.txt. Use the command grep, add to what you had how many lines "the" appears in shakespeare.txt. Hint - use the -c option for grep, also use the -i option so your count will also include "The", "THE", etc. In your shake.txt, you should have the number of lines that contain "the" (including "The", etc.), and also the percentage of the lines that contain the.

Linux.7 In your linux-bootcamp directory, create a text file weather.txt. Copy the Indianapolis weather csv file from /u1/junk/weather/ into your linux-bootcamp directory. In your weather.txt file, put how many rows of data are in the csv file (use wc, head, and tail). Assuming 365 days per year, put in your weather.txt file how many years of data are in the csv file.

Linux.8 In your weather.txt file, put the maximum PRCP, SNOW, SNWD, TMAX, TMIN, MEAN from the csv file. Hint - use the sort command, and use the options -t, -k, -n, -o.