Linux Lab¶

While you will be able to do subsequent labs on your Virtual Machine, you must consummate this lab in the Computer Science Instructional Lab (CSIL).

Objectives¶

  1. Become familiar with the Linux environment
  2. Learn basic terminal commands and how to piece of work with a text editor
  3. Larn to run a Python program from the command-line
  4. Acquire the basics of Git and chisubmit
  5. Learn about file permissions
  6. Learn nearly redirection and pipes
  7. Learn nigh remote access tools

If you lot can't work through the entire lab, we recommend you work through the last iii sections (file permissions, redirection and pipes, and remote access tools) at a later time. We accept also marked a few sections as optional, which y'all are welcome to skip if you lot are pressed for fourth dimension. That said, you should ultimately work your way through the unabridged lab, equally information technology volition help you become more than comfortable with the Linux environs that you will be using throughout the quarter.

Linux¶

Linux is an operating arrangement much like Os 10 or Windows. Information technology has windows, programs, web browsers, and and so on. Files are stored in directories (folders) that, in turn, are stored in other directories. Although you can access Linux's features using your mouse, as you perform more than and more complex tasks, you volition detect that using the mouse is ineffective. Linux allows us to collaborate with the reckoner entirely through text using a program chosen the final. (Macs provide a similar terminal application, and at that place are ways to utilize text-based commands on Windows besides. But, Linux provides the lowest barrier to entry.) In this lab yous will learn how to use the terminal to perform some basic operations in Linux. You volition need these skills for the residue of the course.

We testify many examples of sample output below. The output you see when yous run the commands may vary a fleck. For example, most of you are non named "Gustav Martin Larsson".

Concluding/Crush¶

On your personal estimator, you probably navigate your hard drive by double clicking on icons. While convenient for simple tasks, this approach is express. For example, imagine that yous desire to delete all of the music files over five MB that you oasis't listened to in over a year. This task is very difficult to practise with the standard double-click interface simply is relatively simple using the terminal.

On the CSIL auto, click the Application button (at the top left) and type "concluding" in the input box. Click the "terminal" icon to open the last window. Alternatively, y'all can utilize the keyboard shortcut Ctrl-Alt-T to open a terminal window.

A terminal window volition open and you lot will see text of the form:

where username has been replaced past your CNetID and computer is the name of the machine you lot happen to be using. This string is called the prompt. When you outset typing, the characters you blazon will appear to the correct of the $ .

The programme that runs within a terminal window and processes the commands the you blazon is called a shell. Nosotros use bash , which is the default shell on most Linux distributions, simply there are other popular shells, such as ksh , tcsh , etc.

The process for completing this lab is every bit follows. For each department, read through the explanatory text and the examples. Then, effort these ideas by doing the exercises listed at the bottom of the section.

Show Files¶

The last will start in your home directory, /home/username/ , which is a special directory assigned to your user account. Any figurer that you use in CSIL will automatically connect to your home directory and all files that you created or inverse in previous sessions in CSIL will be available to y'all.

2 very useful commands are pwd and ls :

pwd Prints your current working directory - tells y'all where you are in your directory tree.
ls Lists all of the files in the current directory.

The following is an example using these two commands in a terminal window:

                username@estimator:~$ pwd /home/username/ username@reckoner:~$ ls Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos username@computer:~$              

Endeavor these commands yourself to verify that everything looks like.

Notice that the directory path and list of files that you see if you open your home folder graphically are identical to those provided by pwd and ls , respectively. The merely divergence is how y'all get the information and how the data is displayed.

Modify Directory¶

cd <path-name> change to the directory path-name
cd .. move up/back ane directory
cd move to your habitation directory

How can nosotros move around in the file system? If we were using a graphical system, we would double click on folders and occasionally click the "back" arrow. In club to change directories in the last, we use cd (change directory) followed by the proper name of the destination directory. (A note about notation: we will use text inside angle brackets, such as <path-name> every bit a place holder. The text informally describes the type of value that should be supplied. In the example of <path-proper name> , the desired value is the path-proper noun for a file. More well-nigh path-names later.) For example if we want to change to the Desktop directory, we type the post-obit in the terminal:

Here is an example of changing to the desktop directory in the concluding. Nosotros utilise pwd and ls to verify where we are and where we tin go:

                username@figurer:~$ pwd /domicile/username/ username@reckoner:~$ ls Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos username@computer:~$ cd Desktop username@computer:~/Desktop$ pwd /habitation/username/Desktop/ username@computer:~/Desktop$ ls  username@computer:~/Desktop$              

Find that subsequently we cd into the Desktop the command pwd now prints out:

rather than:

In the kickoff, there are no files in the Desktop directory, which is why the output of ls in this directory is empty.

We tin move up 1 step in the directory tree (e.g., from /home/username/Desktop to /home/username or from /dwelling house/username to /dwelling ) by typing cd .. Here "up" is represented by " .. " In this context, this command will move usa upwardly one level dorsum to our home directory:

                username@computer:~/Desktop$ pwd /home/username/Desktop/ username@computer:~/Desktop$ cd .. username@computer:~$ pwd /home/username/              

Notice that the current working directory is likewise shown in the prompt string.

~ shortcut for your home directory
. shortcut for the current working directory
.. shortcut for one level up from your current working directory

The tilde (~) directory is the same every bit your home directory: that is, ~ is autograph for /home/username . Here's another useful shorthand: a unmarried dot ( . ) refers to the electric current directory.

Commonly when you lot utilize cd , y'all volition specify what is called a relative path, that is, you are telling the calculator to take you to a directory where the location of the directory is described relative to the current directory. The merely reason that the calculator knows that we can cd to Desktop is because Desktop is a folder within the /home/username directory. Just, if we employ a / at the beginning of our path, we are specifying the path relative to the the "root" or top of the file arrangement. For example:

                username@figurer:~$ pwd /dwelling house/username/ username@computer:~$ cd /home/username/Desktop username@figurer:~/Desktop$ pwd /home/username/Desktop username@figurer:~/Desktop$ cd /home/username username@computer:~$ pwd /abode/username              

These commands achieve the aforementioned thing as the ones higher up: nosotros cd into Desktop , a folder within our home directory, and and then back to our dwelling directory. Paths that commencement with a / are known as absolute paths considering they e'er lead to the same identify, regardless of your electric current working directory.

Running cd without an statement volition take you back to your home directory without regard to your current location in the file system. For example:

                username@computer:~/Desktop$ cd username@computer:~$ pwd /home/username              

To meliorate the readability of our examples, we will use $ equally the prompt rather than the full text username@computer:~$ in the rest of this lab and, more than mostly, in the course going forrard. Keep in mind, though, that the prompt shows your current working directory.

Setting Up Your CMSC 12100 Directory¶

Nosotros demand a set of files to practice our commands on. Unfortunately, your home directories are generally empty. In this section, we're going to download a fix of files for you to piece of work with. We will do this using Git, a version control system and code-sharing tool. Git will be described in more depth below. For at present, please execute the following:

  • Make sure that your departmental Git business relationship is correctly set. Using a browser, become to https://mit.cs.uchicago.edu/ and try to log in with your CNetID and password. When you blazon in your username, use only your CNetID username (without "@uchicago.edu"). If you are unable to log in, delight tell a TA.

  • Dorsum in a last window, brand certain that you are in your home directory /home/username using the pwd control. If you are not in that directory and so use cd to navigate to it.

  • Run the following control in the concluding if you lot are on a CSIL figurer:

                                            cs                    -                    setup                    -                    script                    cmsc12100                    -                    aut                    -                    nineteen                  

Notation

Copy-Paste: In Windows (Mac) you usually copy-paste with Ctrl-C ( Control-C ) and Ctrl-V ( Command-Five ). These curt-cuts are available in graphical programs in Linux but not in the Concluding. Instead y'all tin can copy text just past selecting it with your mouse. Select the line that starts with cs-setup-script... above to copy it. You lot can paste by clicking the middle mouse button where you desire the copied text to go. Eye click in the last. Y'all should too be able to utilise Ctrl-Shift-C and Ctrl-Shift-5 , simply yous may discover that this method does not reliably work.

  • The setup script will ask you first to enter your CNetID:

                                            Enter                    your                    CNetID                    [                    username                    ]:                  

    Where your CNetID will appear in place of username . You can either type in your CNetID or hit enter to accept the username in brackets. Next, you will be asked for your CNetID countersign:

                                            Enter                    your                    CNetID                    password                    :                  

    The script needs your countersign to access your information on the CS section's Git server and will handle it securely.

Note

You may be accepted to seeing an asterisk grapheme appear for each password character that you blazon on a web browser. This will not happen when you type passwords into the final. The password is not "echoed" back in whatever mode (not fifty-fifty with asterisks), so don't be alarmed if it looks as if you lot're password isn't being typed in.

  • Next, the script will print this:

                                            You lot                    are                    a                    member                    of                    the                    following                    repositories                    .                    Please                    select                    the                    i                    you                    desire                    to                    use                    :                    [                    1                    ]                    username                    [                    X                    ]                    Exit                    Choose                    one                    :                  

    Choose ane . Later in the quarter, yous will become a member of other repositories as you work in groups with other students.

  • If successful, the script will print out the following:

                                            Setting                    up                    your                    Git                    repository                    ...                    Your                    git                    repository                    has                    been                    created                    in                    /                    abode                    /                    username                    /                    cmsc12100                    -                    aut                    -                    19                    -                    username                    Setting                    upwardly                    chisubmit                    ...                    chisubmit                    has                    been                    set                    upwards                    .                    You                    tin can                    utilise                    chisubmit                    commands                    inside                    /                    dwelling house                    /                    username                    /                    cmsc12100                    -                    aut                    -                    19                    -                    username                  

    Where, once again, your CNetID will appear in place of username .

    This output indicates that your Git repository has been correctly prepare. The script besides configured your cmsc12100 directory for chisubmit , a tool y'all will utilise to submit your programming assignments. We'll discuss chisubmit subsequently in this lab.

  • Afterward running the setup script, list the files in your home directory. You lot should see a new directory cmsc12100-aut-19-username . This directory will contain all of your work for this class. It contains a subdirectory, lab1 , that has some files for us to play with. You volition learn how to manipulate these files in the next section.

    Note that y'all will too see subdirectories named pa0 and pa1 . You can ignore these for now. Employ pwd , ls , and cd to navigate to the lab1 subdirectory.

Using an Editor¶

List the files in the lab1 directory. You should see the following:

                                hello_world                .                py                my_echo                .                py                my                -                input                .                txt                exam                .                txt              

How do we view and edit the contents of these files? There are many high-quality text editors for Linux. We will use Sublime Text, which is good for writing code.

You can open up a specific file, say test.txt , using the subl control from the Linux command-line by typing:

When yous run this control, you will get a new window that displays the following text:

                                Lab                i                Test                file                ===============                Writer                :                Firstname                Lastname              

If the file is blank, quit subl and ensure that the file test.txt exists in your local directory (utilise ls to list the files in your local directory). If it does not, utilize cd to navigate to the lab1 subdirectory inside the cmsc12100-aut-19-username directory.

For now, we will use sublime ( subl ) in a very basic fashion. You can navigate to a particular place in a file using the arrow keys and then type typical characters and delete them as you would in a regular text editor. You tin save your changes using the save selection in the file card or use the keyboard shortcut Crtl-southward . To quit, yous tin can employ the file menu quit option or the keyboard shortcut Ctrl-q .

As an aside, y'all can also launch Sublime Text from the application launcher: simply click the Awarding push button (at the elevation left of your screen), type "sublime" in the input box, and a "Sublime Text" icon should appear. You tin can and so use the File carte to navigate the correct file.

Later on yous may detect it useful to have admission to a graphical tree view in your editor. You tin can access this by launching folder view of sublime rather than just a single file via subl .

Exercises¶

Make sure that y'all are comfortable with this level of usage:

  1. Add your proper noun afterwards Author: in this file
  2. Save the file
  3. Shut and reopen the file in sublime and ensuring that your proper noun is withal at that place
  4. Finally, close sublime

Re-create ( cp ), Move ( mv ), Remove ( rm ), and Brand Directory ( mkdir

cp <source> <destination> copy the source file to the new destination
mv <source> <destination> move the source file to the new destination
rm <file> remove or delete a file
mkdir <directoryname> brand a new empty directory

Sometimes it is useful to copy a file. To copy a file, use the command:

                                cp                <                source                >                <                destination                >              

where <source> is replaced past the name of the file you desire to re-create and <destination> is replaced past the desired proper noun for the copy. An example of copying the file test.txt to copy.txt is beneath:

<destination> can also be replaced with a path to a directory. In this case, the re-create volition exist stored in the specified directory and will have the same name every bit the source.

Movement ( mv ) has exactly the same syntax, but does not proceed the original file. Remove ( rm ) will delete the file from your directory.

If you want to copy or remove an unabridged directory along with its the files, the normal cp and rm commands will not work. Use cp -r instead of cp or rm -r instead of rm to copy or remove directories (the r stands for "recursive"):

Make sure you desire to remove everything in the named directory, including subdirectories, earlier y'all use rm -r .

You lot can make a new directory with mkdir directoryname , where directoryname is the desired name for the new directory.

Exercises¶

Try the post-obit tasks to practice and bank check your understanding of these terminal commands.

  1. Execute the above re-create command and use ls to ensure that both files exist.
  2. Move the file copy.txt to the proper noun copy2.txt . Use ls to verify that this command worked.
  3. Make a new directory named backups using the mkdir command.
  4. Copy the file copy2.txt to the backups directory.
  5. Verify that step (four) was successful by listing the files in the backups directory.
  6. At present that nosotros have a copy of test.txt in the backups directory we no longer need copy2.txt . Remove the file copy2.txt in this directory.

It tin be tedious (and, when yous are tired, challenging) to spell directory or file names exactly, so the terminal provides an autocomplete mechanism to guide yous through your folder explorations. To access this functionality but start typing whatever name you lot are interested in the context of a command and and then hitting tab. If at that place is merely one way to finish that term striking tab volition fill in the rest of the term, for instance, if we typed ls b then hit tab it would automatically finish the word ls backups and and then await our hitting enter. If at that place is MORE than one way to terminate a term, like if we had another folder called backups-old , then hitting tab twice with cause the last to display all of the options available.

Training yourself to utilise motorcar-completion (aka, tab completion) will save y'all time and reduce the inevitable frustration that arises from mistyping filenames when you lot are tired or distracted.

Run a Python Program¶

python3 file.py runs the python program file.py

In this course, y'all will learn Python. To run a Python plan, utilise the command python3 and the name of the file that contains your program.

Employ ls to verify that there there is a file named hello_world.py in your lab1 directory. Now, run the program in hello_world.py by typing (don't forget almost autocomplete!):

This program is a very simple. It just prints "Hello, World!" to the screen.

Notation

There are several variants of Python, including Python two.7 and Python 3. We will be using Python iii and the respective python3 interpreter. The CSIL machines have Python 2.vii installed as the default Python. As a result, the command python runs a version of Python two.7. There are some differences between the two languages and Python 3 programs may non run properly using a Python ii.vii interpreter.

Edit and Run a Python Program¶

In this department you lot volition modify and rerun the program in hello_world.py . This change is very simple but goes through all the mechanical steps needed to program.

Open the file hello_world.py with the command:

The file contains a single line of code:

Change this line so that it instead says "Hello " and and then your name. For example if your name were Gustav Larsson, the line would read:

Do the post-obit steps:

  1. Salvage the file hello_world.py in sublime (forgetting to save is a surprisingly mutual error)
  2. Rerun the program using python3

Let's reinforce the steps to programming in Python with the concluding:

  1. Alter your .py file with an editor
  2. Save the file
  3. Run the file with python3

Forgetting to salve the file (step 2) is a very common mistake!

Git¶

Git is a organisation used for developing software in a group. This system maintains files and all changes that are applied to them. You will each have a personal Git repository that is hosted on a central server. The server stores the project files and stores all changes to those files that take been uploaded to the repository.

We have created accounts and repositories for each of you on a CS department Git server. Also, nosotros will be able to meet whatsoever changes you upload to your repository, which allows us to provide help remotely, form your programming assignments, and provide feedback.

Git tracks every version of a file or directory using commits. When yous have made changes to ane or more files, you can logically group those changes into a "commit" that gets added to your repository. You can think of commits as "checkpoints" in your work, representing the work you've washed since the previous checkpoint. This mechanism makes information technology possible to await at and even revert to older versions of a file by going back to your code as information technology was when yous "checkpointed" information technology with a commit.

When using Git, your bones working cycle will be:

  • Log into a CS machine (or your VM)
  • Change to your cmsc12100-aut-19-username directory
  • Download updates from the Git server (we will add files to your repository throughout the quarter). In Git, this operation is called pulling from the server.
  • Work on your files
  • Create a commit with any changes you accept made
  • Upload the commit to the Git server. In Git, this operation is called pushing to the server.

The course staff does non accept access to whatsoever files stored in your dwelling directory or files that you have not pushed to the Git server. All we can access are files that have been pushed to the Git server, so remember to e'er push your latest commits when you're done or when you ask a question on Piazza that will require us to look at your code.

Delight navigate to your cmsc12100-aut-xix-username/lab1 directory using cd . username should always be substituted by your CNetID.

Creating a commit¶

Creating a commit is a 2-step process. First, you have to indicate what files you desire to include in your commit. Allow's say we want to create a commit that merely includes the hello_world.py file that y'all modified earlier. Nosotros can specify this functioning explicitly using the git add command from the Linux command-line:

There are various shortcuts that will let you to add all of the files in a directory, such as git add . or git add --all . Using these commands is poor practice, because you can easily cease up calculation files that you did not intend. Instead, it is meliorate to add files explicitly when you create them and so use the control from the Linux command-line:

when you want to add any file that has inverse since your final commit.

To create the commit, use the git commit control. This command volition take all the files yous added with git add and will bundle them into a commit:

                                git                commit                -                m                "Made some changes to hello_world.py"              

The text afterwards the -g is a curt message that describes the changes you accept made since your final commit. Mutual examples of commit letters might be "Finished part 1 of the homework" or "Finished lab one".

Note

If you forget the -thousand"Annotate" at the terminate and so Git will think that you forgot to specify a commit message. It will graciously open up a default editor so that yous can enter such a message. On the CS machines this editor is vim . To escape the vim view, attempt typing exactly the post-obit:

And then pressing Enter.

Please note that, if yous see this at the lesser of the screen:

You volition need to press the ESC key before you type :q! .

At present try git commit again and don't forget the -m"Comment" .

In one case you lot run the in a higher place command, you lot will see something like the post-obit output:

                                [                master                99232                df                ]                Fabricated                some                changes                to                hello_world                .                py                1                file                changed                ,                1                insertion                (                +                ),                1                deletion                (                -                )              

You've created a commit, but yous're not washed yet: y'all haven't uploaded it to the server still. Forgetting this stride is actually a very common pitfall, so don't forget to upload your changes. Y'all must utilize the git button command for your changes to actually exist uploaded to the Git server. If you don't, the graders volition non be able to see your code. Simply run the following command from the Linux command-line:

Yous should see something like this output:

                                Counting                objects                :                7                ,                washed                .                Delta                compression                using                upward                to                16                threads                .                Compressing                objects                :                100                %                (                4                /                iv                ),                washed                .                Writing                objects                :                100                %                (                4                /                4                ),                452                bytes                ,                done                .                Total                4                (                delta                1                ),                reused                0                (                delta                0                )                To                git                @git                -                dev                .                cs                .                uchicago                .                edu                :                cmsc12100                -                aut                -                19                /                username                .                git                c8432e4                ..                99232                df                principal                ->                master              

You tin ignore most of those letters. The important thing is to not run into any warnings or mistake messages.

Y'all tin can verify that our Git server correctly received your commit by visiting the following page:

                                https                :                //                mit                .                cs                .                uchicago                .                edu                /                cmsc12100                -                aut                -                19                /                username              

Where username should be substituted by your CNetID.

This URL takes you to the web frontend of our Git server (please notation that yous will have to log in using your CNetID and countersign). More specifically, the above URL volition prove you the contents of your repository, exactly as it appears on the Git server. You tin can click on "Files" to come across your repository's files, and on "Commits" to come across the latest commits uploaded to the server. If you see a commit titled "Made some changes to How-do-you-do World", then your commit was successfully uploaded.

In general, if you're concerned about whether the graders are seeing the right version of your code, you lot can just go to the above URL. Whatever is shown on that page is what the graders will meet. If you wrote some code, and it doesn't show up in the in a higher place URL, make certain you didn't forget to add together your files, create a commit, and push button the virtually contempo commit to the server.

Pulling changes from "upstream"¶

When nosotros distribute new homework assignments or lab materials, we will do and then through Git. These files are located in a separate repository on our Git server, which we phone call the "upstream" repository. The setup script you ran earlier already configured your Git repository and then you lot can easily download whatsoever new files we upload to the upstream repository). To download these changes, run this command from inside the cmsc12100-aut-19-username directory:

If you run it now, goose egg will actually happen, since nosotros haven't changed anything in "upstream" since the start of this lab. You should meet something like this output:

                                From                git                -                dev                .                cs                .                uchicago                .                edu                :                cmsc12100                -                aut                -                xix                /                cmsc12100                -                aut                -                nineteen                *                branch                master                ->                FETCH_HEAD                Already                up                -                to                -                date                .              

When you pull from "upstream", Git automatically downloads whatever new files or changes that have been committed to "upstream" and updates the files in your repository. If you have fabricated local changes to files that take changed upstream, Git will endeavor to merge these changes.

After you've pulled from upstream, whatsoever new files or changes will only be downloaded to your local copy of cmsc12100-aut-19-username . Equally with any other changes to your lawmaking, you need to run git push to upload them to the Git server (you don't need to do a git commit to prepare a commit, though; git pull already takes care of this task).

  • Every time y'all piece of work on your lawmaking, you should run git pull upstream primary in your cmsc12100-aut-19-username directory before you lot do anything else. Sometimes, the instructors detect typos or errors in the lawmaking provided for a programming assignment, and they'll commit fixes to upstream. By running git pull upstream master , you tin make sure that those fixes propagate to your code as well.

Pulling your changes from the server¶

If you take done work and committed it to the server from a lab estimator and now wish to work on your VM (or vice versa), you volition need to pull these changes from the server to your VM. To download these changes, run this control from within the cmsc12100-aut-19-username directory:

It is important that you commit your changes subsequently every session and that you pull from both upstream and cmsc12100-aut-19-username before y'all start to do any work.

Note

Your output may vary from our sample output slightly. Do non worry nearly the divergence unless yous see an fault message or a alarm message.

git add revisited and git status

So far, nosotros've created a single commit with a single file that we had already supplied in the lab1 directory. If you create new files, Git will not consider them a part of the repository. You need to add them to your repository explicitly. For instance, permit'south create a re-create of hello_world.py :

                                cp                hello_world                .                py                hello_universe                .                py              

Is hello_universe.py office of your repository? You lot can use the following command to ask Git for a summary of the files it is tracking:

This command should output something like this:

                                # On co-operative main                # Changes not staged for commit:                #   (use "git add <file>..." to update what will be committed)                #   (use "git checkout -- <file>..." to discard changes in working directory)                #                #   modified:   test.txt                #                # Untracked files:                #   (employ "git add <file>..." to include in what will be committed)                #                #   hello_universe.py                no                changes                added                to                commit                (                utilise                "git add together"                and                /                or                "git commit -a"                )              

The exact output may vary depending on how far along you got in previous sections of the lab. Even so, the important thing is that there are two types of files listed here:

  • Changes non staged for commit : This is a listing of files that Git knows virtually and accept been modified since your concluding commit, but which have not been added (with git add ).

  • Untracked files : This is a list of files that Git has found in the same directory equally your repository, merely which Git isn't keeping track of.

    Yous may come across some automatically generated files in your Untracked files section. Files that start with a pound sign (#) or cease with a tilde should not be added to your repository. Files that cease with a tilde are backup files created past some editors that are intended to help y'all restore your files if your computer crashes. In full general, files that are automatically generated should non be committed to your repository. Other people should be able to generate their own versions, if necessary.

To add together a previously untracked file to your repository, yous tin can just use git add together (dissimilar the previous commands, don't actually run this just yet; you lot volition be doing a similar exercise afterwards):

                                git                add                hello_universe                .                py              

If y'all re-ran git status you would come across something similar this:

                                # On branch master                # Changes to be committed:                #   (use "git reset HEAD <file>..." to unstage)                #                #   new file:   hello_universe.py                #                # Changes non staged for commit:                #   (utilise "git add <file>..." to update what will exist committed)                #   (use "git checkout -- <file>..." to discard changes in working directory)                #                #   modified:   test.txt              

Discover how at that place is now a new category of files: Changes to exist committed . Adding hello_universe.py not only added the file to your repository, it likewise staged it into the next commit (which, call up, won't happen until you really run git commit ).

The git status command reports the status on the local copy of the full repository. If y'all wish to look at the status of a smaller office of the repository (the directory you are working in for example), yous can add a path name to the status control. For example:

reports the status of the current directory (a single dot is the path used to refer to the electric current directory).

Unstaging, discarding changes, and removing files (optional, skip if curt on time)¶

Take a closer look at the git status output in a higher place. Git is providing yous hints in case you want to undo some of your piece of work.

For example, you lot tin can use git reset hello_universe.py to unstage the file. Doing so reverses git add together hello_universe.py and so you tin can create a commit only of changes to other files. This is skilful practice if you think the changes you made to hello_universe.py don't logically go in the commit you are going to make.

Another useful git control is git checkout . This command will undo modifications to files. If you again wait at the above git status output, you lot will see in the terminal line that examination.txt was modified. To undo any changes to the file, type git checkout test.txt . This control will revert the file content to lucifer the last commit yous made in your repository's history

Finally, if y'all would similar to remove a file from your directory, using git rm test.txt combines the outcome of doing rm exam.txt and git add test.txt .

Looking at the commits log (optional, skip if short on time)¶

Once you accept made multiple commits, you can see these commits, their dates, commit letters, writer, and an SHA-1 hash past typing git log . This command will open a scrollable interface (using the upward/downwardly pointer keys) that you tin get out of past hitting q .

Exercises¶

  1. You have already inverse the examination.txt file in your directory. Verify this by using the command git condition . You should see it under Changes not staged for commit .
  2. Use git add together and git commit to create a commit that includes merely the test.txt file. A good commit bulletin would exist "Added my name as Author in exam.txt".
  3. Upload your work to the server using git push .
  4. Verify that this file was sent by again using the command git condition . You should see that the file exam.txt is no longer listed.
  5. If yous accept not already washed so, apply cp to make a copy of hello_world.py named hello_universe.py .
  6. If you lot run git status , hello_universe.py should prove up under Untracked files . Add it to the repository using git add .
  7. Run git status again. Is hello_universe.py in a different category of files now?
  8. Although nosotros have added this file, nosotros have not yet created a commit. Create a commit and push it to the server.
  9. Run git status a final fourth dimension to verify that hello_universe.py was committed (if and then, you lot should not see it in whatsoever category of files)
  10. Run git push to upload your changes to the server.

We strongly recommend you to cheque in and push inverse files as often as possible, especially if you finished some work and are most to log off a computer. This way the latest files are attainable from whatever other computer where your repository is set up.

chisubmit¶

You will be using a locally-adult system named chisubmit to submit your programming assignments. The set-upwardly script that y'all ran earlier gear up you upwards to apply chisubmit in addition to initializing your Git repository.

All chisubmit commands should be run from within your cmsc12100-aut-19-username directory.

chisubmit has commands for managing assignments. Here are descriptions and sample runs of some of the more useful commands. You tin can run these commands every bit you read through this section.

chisubmit student consignment list : lists upcoming programming assignments and their deadlines.

                username@cs-vm:~/cmsc12100-aut-19-larsson$ chisubmit student assignment listing  pa0 2019-x-07 23:59:59-05:00       Programming Assignment #0              

chisubmit student assignment evidence-borderline <consignment name> : lists deadline information for the specified programming consignment.

                username@cs-vm:~/cmsc12100-aut-nineteen-larsson$ chisubmit student assignment evidence-deadline pa0 Programming Consignment #0        Now: 2019-09-25 12:08:52-05:00  Deadline: 2019-10-07 23:59:59-05:00  The deadline has not yet passed You lot accept 12 days, eleven hours, 51 minutes, 7 seconds left              

chisubmit student assignment register <assignment proper name> : registers a educatee for a specific consignment. You lot will practise this pace once per assignment.

                username@cs-vm:~/cmsc12100-aut-19-larsson$ chisubmit educatee assignment register pa0 Your registration for pa0 (Programming Assignment 0) is complete.              

chisubmit student assignment submit pa0 : submits your electric current commit

              username@cs-vm:~/cmsc12100-aut-xix-larsson$ chisubmit pupil consignment submit pa0  SUBMISSION FOR ASSIGNMENT pa0 (Programming Consignment 0) --------------------------------------------------------  This is an Individual submission for Gustav Martin Larsson  The latest commit in your repository is the following:       Commit: eeed8efa66a13c0b04c587acdda43fbe75c9b99b        Date: 2019-09-28 13:47:16-05:00     Bulletin: Added log for testing purposes      Author: Gustav Martin Larsson <larsson@cs.uchicago.edu>  PLEASE VERIFY THIS IS THE EXACT COMMIT Yous Want TO SUBMIT  You currently have 2 extensions  You are going to use 0 extensions on this submission.  You volition have 2 extensions left afterwards this submission.  Are you sure y'all want to keep? (y/n):  y  Your submission has been completed.            

chisubmit has many other commands, including control for canceling registrations, canceling submissions etc. You lot can find detailed instructions on these and other commands hither.

More than Git (optional, skip if short on time)¶

Pulling on some other car¶

At present permit's return to Git. This role requires that you accept a laptop with you and that you take gone through the procedure of setting-up your laptop with the CS121 VM and with your CS121 repository. If you lot do non have a laptop or do not take one with you, y'all tin can only skip this this department.

If you expect at your files in lab1 on your VM, you'll notice that they are out of engagement. The changes you made to test.txt are not visible and hello_universe.py , the new file you added earlier, is missing. Why? Considering your local copy on your VM is out of sync with the authoritative version of repository on the server.

Run git pull from the Linux command-line to option up your changes. One time you run this command the file examination.txt should take changed and there should exist a file named hello_universe.py .

As long equally you committed and pushed all your changes on the CSIL machines to the server, the copies of your repository on the CSIL/CS machines, your VM, and the server will exist the aforementioned.

Merge conflicts¶

The beauty of Git specifically, and version command in general, is that you can share repositories with other people and yous can piece of work on the code separately. Merge conflicts arise when different copies of the repository get changed in incompatible means. Unfortunately, this complication can ascend even if yous are the merely one working on your repository! You lot just need to work on your lawmaking using multiple machines.

Let'southward piece of work through an example. Permit's say I take the following steps using a CSIL machine:

  1. Change the line in hello_world.py from:

to:

  1. Add and commit these changes.
  2. Forget to practise a push.

And then switch to my VM and do the following:

  1. Run git pull inside my cs121 directory to pick upwardly the most contempo copy from the server.
  2. Change the line in hello_world.py from:

to:

                                print                (                "Hi, New York!"                )              

(Discover that the change from "World" to "Chicago" did not go far to my VM because I forgot to push!)

  1. Add, commit, and push these changes to the server.

And then, finally, switch back to my CSIL machine and run git pull to pick up the virtually contempo version from the server. This command will neglect with an error like the following:

                username@cs-vm:~/cmsc12100-aut-19-larsson/lab1$ git pull remote: Counting objects: 4, done. remote: Compressing objects: 100% (3/iii), washed. remote: Total 4 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (4/iv), done. From mit.cs.uchicago.edu:cmsc12100-aut-nineteen/larsson    62c72de..b70ae2a  master     -> origin/principal Auto-merging lab1/hello_world.py Disharmonize (content): Merge conflict in lab1/hello_world.py Automated merge failed; ready conflicts and then commit the outcome.              

Git was not able to reconcile my local changes with the version on the server automatically and and then, information technology updates hello_world.py to reverberate the conflicts. The file hello_world.py will look like this:

                                <<<<<<<                HEAD                print                (                "How-do-you-do, Chicago!"                )                =======                print                (                "Hullo, New York!"                )                >>>>>>>                b70ae2a739c7775189b284be04ae568568ac3c62              

The lines between <<<<<<< Caput and ======= contain the code as it exists on the CSIL machine, where as the lines between ======= through >>>>>>> b70ae2a739c7775189b284be04ae568568ac3c62 contain the code from the server. In general, a failed merge tin yield a mix of merged and unmerged blocks. I will need to resolve these conflicts by choosing among the offending lines and removing the disharmonize markers ( <<<<<<< Caput , ======= , and >>>>>>> b70ae2a739c7775189b284be04ae568568ac3c62 ). And and then I'll need to add together, commit, and push the updated files, earlier I can pull or push button my lawmaking successfully.

Note

The sections beneath expand on useful Linux concepts and skills, but you can piece of work through them at a afterwards time if you lot want (including on your VM, instead of on a CSIL motorcar).

File Permissions¶

Sometimes we want to restrict who can admission certain resources on the file system.

Most file systems assign 'File Permissions' (or simply permissions) to specific users and groups of users. Unix is no unlike. File permissions dictate who can read (view), write (create/edit), and execute (run) files on a file system.

All directories and files are owned past a user. Each user can be a member of one or more groups. To see your groups, enter the command groups into the command line.

File permissions in Unix systems are managed in three distinct scopes. Each telescopic has a distinct set of permissions.

User - The owner of a file or directory makes up the user scope.

Group - Each file and directory has a grouping assigned to information technology. The members of this group make upwards the grouping scope.

Others - Every user who does not fall into the previous two scopes make up the others telescopic.

If a user falls into more than one of these scopes, their effective permissions are determined based on the first scope the user falls within in the gild of user, grouping, and others.

Users that fall into each scope can have 3 specific permissions.

read - The read permission allows a user to view a file's contents. When ready for a directory, this permission allows a user to view the names of files in the directory, merely no further information well-nigh the files in the directory. r is shorthand for read permissions.

write - The write permission allows a user to modify the contents of a file. When prepare for a directory, this permission allows a user to create, delete, or rename files. w is shorthand for write permissions.

execute - The execute permission allows a user to execute a file (or program) using the operating system. When set for a directory, this permission allows a user to access file contents and other information nearly files within the directory (given that the user has the proper permissions to access the file).). The execute permission does not allow the user to list the files inside the directory unless the read permission is also prepare. x is autograph for execute permissions.

To list information near a file, including its permissions, type:

You'll get output of the form:

                            <              permissions              >              1              owner              group              <              size              in              bytes              >              <              engagement              modified              >              <              filepath              >            

For example, if nosotros desire information on /usr/bin/python3.5 :

              $ ls -l /usr/bin/python3.5 -rwxr-xr-ten 1 root root 4460272 Aug xx /usr/bin/python3.5            

First matter we can notice is that the owner of the file is a user named root . (FYI, root is a name for an account that has access to all commands and files on a Linux arrangement. Other accounts may also take "root" privileges.) The file's group is as well root .

The permissions are -rwxr-xr-x . These permissions are listed in user, group, and others order. In this example, the owner, root , can read ( r ), write ( w ), and execute ( x ) the file. Users in the root group and all other users can read and execute the files.

Exercises¶

By default, whatsoever files or directories that you create volition have your username as both the user and the group. (If you lot run groups , yous'll notice that there is a grouping with the aforementioned proper noun equally your username. Yous are the only member of this group.) On our Linux machines, by default, new files are set to requite read and write permissions to user and group and no permissions to other. New directories volition be set to have read, write and execute permissions for user and group.

  1. Verify this claim by running ls -fifty backups/copy2.txt and ls -ld backups in your lab1 directory.

The -d flag tells ls to list the directory, instead of its contents. Observe that that the first alphabetic character in the permissions string for backups is a d, which tells us that backups is directory. A regular file would have a - in that spot.

One time you have verified the merits, go ahead and remove the backups directory using the command: rm -r backups .

Changing Permissions, Owner, & Group¶

chmod <permissions> <path-proper name> set the permissions for a file/directory
chmod <changes> <path-name> update the permissions for a file/directory
chown <username> <path-proper noun> change the owner of a file to username
chgrp <group> <path-proper name> modify the grouping of a file
cat <path-proper name> impress the contents of a file to the last

Each permission has a unique value: read = 4, write = two, execute = one. You can describe the permissions of a scope using the sum of its permissions' values. For case, if a file has read and write permissions for the user scope, its permissions can be described as half-dozen (4 + 2 = 6).

Y'all can describe the permissions of a file overall using these values for each scope. For example, 761 describes the permissions for a file with read, write, and execute permissions for the user telescopic, read and write permissions for the group scope, and simply execute permissions for the others scope.

To change permissions, we employ the chmod control. This control tin can be used in 2 means. We can set the permissions for a file using the iii-digit number described in the previous paragraph or by calculation to and/or removing permissions from the current settings. To utilize the latter arroyo, yous specify the scope using a combination of u , g , and o , the permission using r , westward , and x , and either + or - to bespeak that y'all want to add or remove a permission. For case uo+rw indicates that you lot want to add read and write permissions for the user and others groups.

We can demonstrate this using the cat control to print file contents to the terminal (we'll make utilize of an unfamiliar operator, >, but conduct with us):

                $ repeat "Hullo!" > testfile $ ls -l testfile -rw-rw---- ane username username seven Aug 23 11:22 testfile $ cat testfile Hello! $ chmod 222 testfile #set merely write permissions for all scopes $ ls -l testfile --w--due west--w- one username username vii Aug 23 eleven:22 testfile $ cat testfile true cat: testfile: Permission denied $ chmod u+r testfile #give user scope read permissions              

In this last example, we have added user read permissions to testfile .

Past the mode, don't get hung up on memorizing the permission numbers. If the symbolic version makes more sense to you, use it.

To modify the possessor of a file or directory (if you lot are the owner or root), use the command:

                                chown                <                new                possessor                >                <                path                to                file                >              

To alter a file's grouping (if you lot are the owner or root), use the command:

                                chgrp                <                new                group                >                <                path                to                file                >              

It is unlikely that you will demand to employ these ii commands for this course.

Exercises¶

  1. Run echo "Hello!" > testfile to construct testfile . Await at the permissions using ls -50 .
  2. Change the permissions on testfile to allow and read access for others. Run ls -l testfile to check the new permissions.
  3. Remove user and group write admission from testfile . Cheque the corrected permissions.
  4. Remove testfile using rm .

Other Shell Features¶

Wild Cards (using an asterisk)¶

Sometimes when we enter a string, we want part of it to be variable, or a wildcard. A mutual task is to list all files that finish with a given extension, such as .txt . The wildcard functionality, through an asterisk, allows to simply say:

The wildcard can represent a string of any length consisting of any characters - including the empty string.

It is of import to exist careful using wildcard, particularly for commands like rm which cannot exist undone. A command similar:

                $ rm *             ### DO NOT RUN THIS COMMAND!              

will delete all of the files in your working directory!

Exercises¶

  1. Navigate to your cmsc12100-aut-xix-username directory. What do y'all see when you run ls pa* ? What virtually ls pa*/* ?
  2. What do you await to see when you lot run the command ls ../pa* from within your cmsc12100-aut-xix-username/lab1 directory?

Environment Variables¶

printenv print the current land of the environment variables
consign <VARNAME>="some value" define an environment variable

Sometimes, when nosotros have text (a path, for example) that we use frequently, we give it a name by assigning it to a variable for convenience.

The command:

gives a long list of defined variables. Try it in your terminal to see what happens!

When nosotros want to define new variables, we utilise the = operator and consign command. As you found from using printenv, is typical to name environment variables in all capital letter letters, then we could define a new variable as merely:

It is important not to add together spaces; the commands:

                $ NEWVARIABLE =~/Desktop $ NEWVARIABLE= ~/Desktop $ NEWVARIABLE = ~/Desktop              

would all exist misinterpreted past the concluding; spaces within quotes are allowed, if accurate. Nosotros add a $ earlier the name of an environment variable in a control to employ its value:

                $ NEWVARIABLE=~/Desktop $ cd NEWVARIABLE bash: cd: NEWVARIABLE: No such file or directory $ cd $NEWVARIABLE $ pwd /home/username/Desktop              

A variable created equally above is only available to the current trounce. It is a local variable, so future shells (such as those that you create when you open up a new terminal window using Ctrl-Shift-North ) volition not have admission to it. In club to save variables, we need to consign them using the export command. Variables that are exported are called environment variables, which are mostly alleged as follows:

                $ export NEWVARIABLE=~/Desktop              

For this environs variable to take upshot on all futurity shells, the export statement needs to be added to a special file called .bashrc in your abode directory (this file gets run every time you lot offset a new shell)

Exercises¶

  1. Define an surround variable and apply echo , which takes a list of values as command-line arguments and echos them to the screen, to see its value.
  2. Open a new terminal window and use echo in that window to see whether the variable is yet defined.

Note that environment variables defined at the command-line are only available until y'all log out. They will not be bachelor the next time you log in.

Man Pages¶

A human page (short for manual page) documents or describes topics applicable to Linux programming. These topics include Linux programs, certain programming functions, standards, and conventions, and abstract concepts.

To get the man page for a Linux command, y'all tin type:

So in order to become the man page for ls , you would type:

This command displays a man folio that gives data on the ls command, including a description, flags, instructions on use, and other information.

Each man folio has a description. The -thousand flag for man allows yous to search these descriptions using a keyword. For case:

This searches all the descriptions for the keyword printf and prints the names of the man pages with matches.

Running Commands Sequentially¶

It is often user-friendly to chain together commands that you want to run in sequence. For example, recall that to print the working directory and list all of the files and directories contained inside, you would use the following commands:

                $ pwd /home/username/ $ ls Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos              

You could also run them together, like then:

                $ pwd ; ls /home/username/ Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos              

First, pwd is executed and run to completion, and and so ls is executed and run to completion. The two examples higher up are thus equivalent, but the ability to run multiple commands together is a small convenience that could salvage you some time if there is a grouping of commands that y'all desire to execute sequentially.

Note

The trounce doesn't care about white infinite, and then information technology will run whatever of the post-obit as well:

                  $ pwd;ls $ pwd ;ls $ pwd; ls $ pwd       ;        ls                

Useful Keyboard Shortcuts¶

Used at the Linux prompt, the keyboard shortcut Ctrl-P volition roll dorsum to the previous command. If yous blazon Ctrl-P twice, you volition roll back by two commands. If you type Ctrl-P besides many times, you can use Ctrl-North to move forrad.

Here are few more useful shortcuts:

  • Ctrl-A volition move you to the beginning of a line.
  • Ctrl-E volition move you lot to the finish of a line.
  • Ctrl-U will erase everything from where you lot are in a line dorsum to the kickoff.
  • Ctrl-K volition erase everything from where you are to the end of the line.
  • Ctrl-l will clear the current terminal (your env variables will still be there, but the text display will exist gone)
  • Ctrl-shift-t will start a new terminal tab
  • Ctrl-shift-westward will close the current terminal tab

Play around with these commands. Being able to scroll back to, edit, and and so rerun previously used commands saves time and typing!

Redirection¶

The examples in this section will use commands that nosotros've not yet discussed. Refer to the man pages for information nigh unfamiliar commands.

As nosotros already know, commands like pwd , ls , and cat will print output to screen by default. Sometimes, however, we may prefer to write the output of these commands to a file. In Linux, we can redirect the output of a program to a file of our choosing. This operation is done with the > operator.

Try the following case and compare your output with ours:

              $ cd $ impact test-0.txt $ ls > test-i.txt $ cat test-1.txt Desktop Documents Downloads Music Pictures Public Templates test-0.txt examination-ane.txt Videos $ echo "Hello World!" > test-2.txt $ cat test-two.txt Howdy World! $ cat test-2.txt > test-i.txt; cat test-1.txt Hello Earth! $ rm test-*            

Two important things to notation:

  1. If y'all redirect to a file that does not exist, that file volition be created.
  2. If y'all redirect to a file that already exists, the contents of that file volition be overwritten.

You can employ the suspend operator ( >> ) to append the output of control to the stop of an existing file rather than overwrite the contents of that file.

Non simply can we redirect the output of a program to a file, we tin also accept a program receive its input from a file. This operation is done with the < operator. For instance:

              $ python3 my_echo.py < my-input.txt            

(Alter dorsum to your lab1 directory before y'all try this command.)

In full general, all Linux processes tin perform input/output operations through, at least, the keyboard and the screen. More specifically, in that location are three 'input/output streams': standard input (or stdin ), standard output (or stdout ), and standard error (or stderr ). The code in my_echo.py simply reads data from stdin and writes it back out to stdout . The redirection operators change the bindings of these streams from the keyboard and/or screen to files. We'll talk over stderr later in the term.

Pipage¶

In improver to the ability to directly output to and receive input from files, Linux provides a very powerful capability called piping. Pipe allows ane program to receive as input the output of another program, like so:

In this example, the output of program1 is used as the input of program2. Or to put it more technically, the stdout of program1 is continued to the stdin of program2 .

As another more concrete example, consider the man command with the -thousand pick that nosotros've previously discussed. Let's assume that you hadn't yet been introduced to the mkdir command. How would you await for the command to create a directory? Commencement attempts:

                $ human being -k "create directory" create directory: nothing appropriate $ man -k "directory" (a bunch of mostly irrelevant output)              

Every bit nosotros can see, neither of these options is particularly helpful. Nevertheless, with piping, we tin combine human being -k with a powerful command line utility chosen grep (see man pages) to discover what we need:

                $ human being -yard "directory" | grep "create" mkdir (two)            - create a directory mkdirat (2)          - create a directory mkdtemp (iii)          - create a unique temporary directory mkfontdir (i)        - create an index of X font files in a directory mklost+plant (8)     - create a lost+found directory on a mounted Linux second extended fil... mktemp (1)           - create a temporary file or directory pam_mkhomedir (8)    - PAM module to create users domicile directory update-info-dir (8)  - update or create index file from all installed info files in directory vgmknodes (8)        - recreate volume group directory and logical volume special files              

Nice.

Exercises¶

  1. Use piping to chain together the printenv and tail commands to display the last 10 lines of output from printenv .
  2. Replicate the higher up functionality without using the | operator. (hint: Use an intermediate file.)

Remote Admission¶

We'll finish up with a description of some useful commands. If you run out of time, you can skip this role and render to information technology later.

There are two primary tools for accessing a remote computer through the command line: 1 for running commands on the remote computer, and ane for file transfer. The commencement of these commands is much more probable to be useful in this class.

SSH¶

SSH allows you to open up a final session on a computer remotely, and is a major motivation for becoming expert with the terminal. The following command:

begins an SSH session, and allows you to admission all of your files and programs on the remote reckoner (as long as these programs tin can be executed through the crush). The command for SSHing into CSIL Linux computers is:

                $ ssh CNETID@linux.cs.uchicago.edu              

You should try this now and enquire a question if you take trouble, as information technology is something y'all may have to exercise for your CS classes here.

To exit an SSH session, just use the control get out .

ssh is installed by default on Linux and OSX. PuTTY is a popular SSH customer for Windows.

SCP¶

While SSH allows you to log in to some other computer, SCP provides the ability to transfer files betwixt computers. scp is useful, merely do not utilize information technology to move files in your cmsc12100-aut-19 repository betwixt machines. It is much safer to employ Git to manage the files in your repository.

In full general, SCP is chosen as:

                $ scp user@host1:/path/to/file1 user@host2:path/to/file2              

and copies a file from one figurer (the first argument) and places it in the second figurer (the second argument).

If y'all want to copy a file to your local calculator, you lot can simply specify the second argument as a file path, without the username or domain proper name; the same principle applies for copying from your local computer.

If you want to leave the file named as it was, you don't need to specify the file name in the 2nd argument.

An case of using SCP would exist:

                $ ls Desktop Downloads $ scp username@linux.cs.uchicago.edu:~/cmsc12100/assignment1/Grade.txt . $ ls Desktop Downloads Grade.txt              

Recall that a unmarried dot ( . ) refers to the current directory.

Final Notes¶

Sometimes, a program will run indefinitely or misbehave. When this happens, you tin can blazon Ctrl-C to transport an interrupt point to the running program, which usually causes it to stop. On occasion, you may need to type Ctrl-C a few times. Typing Ctrl-D sends an end of input indicate, which tells the plan that no more data is coming.

Log out¶

Clicking on your name in the top correct corner of the screen will requite y'all a menu. To log out, choose "Log Out" from this carte du jour and then click the "Log out" option in the widget that pops upwardly (rather than the pause option).

Brand sure to always log out from your motorcar before y'all leave CSIL.