You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »

Welcome! If you're reading this, you're interested in learning about using Linux, perhaps the most famous open-source operating system. While we won't be delving into how Linux operates, we will be looking at how we can interact with it using the command line.

Many of these commands may additionally work on your local system, especially if you are using a Mac product. However, we will assume that you are working on the server, a Linux environment where these commands are known to work

Setup Script

The first thing you'll want to do when logging into the server is to source the C2S2 setup script. This can be done with the command below (this formatting will always indicate a command to run):

source setup-c2s2.sh

You should now see "C2S2" in your prompt, indicating that you've successfully sourced the setup script! This won't affect most of the commands we run in this section, but is used to add elements to your Linux environment that you may need for C2S2 (we'll learn how it does this later!) Note that if you aren't taking any other ECE classes that use the server, you may wish to source this setup script every time you log in; this can be done with the following command:

source setup-c2s2.sh --enable-auto-setup

If you close out of this window and re-log into the server, you should now see that "C2S2" is located in your prompt automatically. If you ever wish to disable this feature (such as for sourcing a class' setup script), you can do so with:

source setup-c2s2.sh --disable-auto-setup

Hello, World!

Often times, when first learning something in computer science, your first goal is to get it to say "Hello, World!" For us, this is relatively easy, and introduces the first command we'll learn about, echo . The echo command simply takes whatever text you give it, and displays it (or prints it) on your terminal:

echo "Hello, World!"

In this case, we modified the function of echo by giving it an argument - some piece of data that we passed in. In this case, our argument was the text "Hello, World!"

If you ever want to read more about a command, you can use the man  command (which stands for manual). man takes in the name of another command as an argument:

man echo

Click "q" once you're done looking - don't worry about all of the details. Most (if not all) developers never memorize all the possible arguments for Linux commands - that's why the manual pages are there to help you remember!

Directories

In Linux, our entire computer structure is one intricate set of folders. We give folders a special name for folders as well - directories. In addition, each Linux terminal has a directory associated with it. If you want to see your terminal's current directory, you can use the pwd  command, which stands for Print Working Directory:

pwd

You should get something back that looks like /home/acm289 (except using your NetID instead of mine (smile)). On our server, this is the directory that you start in; notice how it tells you the structure of the folders as well. The "/" indicates that we're going inside a new directory. In this case, the home directory contains our current directory (acm289 for me). All folders are contained within the / directory - for Linux systems, this is always the base of our folder hierarchy

Just knowing what directory we're in is nice, but we also want to know what's inside it! To list the contents, we'll use the ls command. For some reason, Confluence won't display just ls properly😅, but you should still run it here.

Note that ls prints the entire contents of the directory to your terminal. ls also comes with some important arguments that we can pass. In particular, these arguments begin with a dash ("-"); arguments that begin with these are often known as flags. They don't add any new data, but instead send a signal to the command that modifies how it executes.

One flag we can pass to the ls command is the l flag; this will print out a lot more information about each file and folder:

ls -l

We'll delve a lot into these details later, but right off the bat, you can see the date when each item was created. Additionally, if you look at the long string of characters to the left, notice that some of them start with "d" - this indicates the directories within your current working directory!

The other main flag that we'll pass is the a flag. Normally, items with names that begin with a dot (".") are hidden from us, as they usually contain some configurations and stuff that isn't used to often. However, we may wish to see these on occasion, which the a flag (for "all") allows us to do:

ls -a

Notice how there's more items listed now!

Navigation

Now that we know where our terminal is located (what directory it's in), as well as the contents of that directory...what happens if we want to move around to other directories? To do this, we should first create a directory that we want to go into - let's create one called c2s2! We can do this using the mkdir command (stands for makdirectory), which takes in the name of the new directory that we want

mkdir c2s2

Now let's ls again.

You should be able to see your new directory named c2s2. To move into a directory, we use the cd command, with an argument of the directory that we want to move into:

cd c2s2
pwd

Now, you should see pwd print something along the lines of /home/acm289/c2s2 - this shows how we changed the directory our terminal is in!

See if you can create another directory named test inside of c2s2, change your terminal to be inside of test, and then use pwd to confirm!

As well as specifying the name of the directory, there are a few other special names for directories that are important to Linux:

  • ~ refers to your home directory - a special directory, and where your terminals open by default. On our server, this is our /home/<netid> directory
  • . refers to your current directory (the one that your terminal is in
  • .. refers to your parent directory - the directory that contains the one that you're in

Knowing this,

  • cd ~ will always navigate your terminal back to your starting directory in /home/<netid> 
  • cd . won't do anything - it changes your directory to the one you're currently in!
  • cd .. will change your directory to one higher up - the directory that contains the one where you are right now

Using the commands above, starting in the test folder from before, see if you can navigate back up to the c2s2 folder, and then back to your NetID folder

Additionally, we can chain these directories together using the slash notation we saw before! From your NetID folder, run the following command:

cd c2s2/test

Using pwd , you can see that it took you directly to the test directory without having to go through the c2s2 directory!

From the test directory, see if you can get back to your NetID directory in one command, without using cd ~ 

Great - you know have many tools at your disposal to navigate all around your Linux filesystem! Take some time playing with these to get comfortable navigating around and visualizing the directory hierarchy just from your terminal.

Absolute vs. Relative Paths

One last thing before we move on; first, navigate back to your home directory:

cd ~

Alright, let's see how we can get into the c2s2 directory - recall from before that we did this using cd c2s2. However, there's another way; we can specify the long path as well, such as pwd gives us. In the command below, replace <netid> with your NetID:

cd /home/<netid>/c2s2

Both of these options get us to the same folder, just in different ways. The "long" path (/home/<netid>/c2s2) is known as an absolute path; since it starts from our / directory, it will always point to the same folder, no matter what. In contrast, just typing cd c2s2 will take us to whatever c2s2 directory is under wherever our terminal happens to be at the time, meaning that the folder it takes us to could change! Since this is relative to the terminal's location, we call it a relative path; you'll often find these written as ./c2s2 (where . is our current directory), just to stress that the folder we go to depends on where we currently are. We commonly use relative paths when cd'ing about (cd c2s2 is synonymous to cd ./c2s2), as they're more convenient and easier to type. However, when specifying file locations to other people of programs, we may want to use absolute paths. Many of the commands that we use will take either.

  • No labels