New terms from treehouse – Console Foundations Part 2 – Processes, Environment and Redirection, and Installing Software

Processes

Processes are a core component of the POSIX operating systems. They are simply a running instance of a program. So if we run nano on a text file, it creates a new instance, aka a process. We can open different files with nano because even though they are using the same program, they’re using different instances.

Screen Shot 2014-11-02 at 2.12.48 PM

Processes have info attached to them, like a unique id, permissions, etc.

top is a tool that let’s you see what processes are running, like window’s task manager or apples activity monitor. Run it by typing top then enter. It will then take up the full screen and tell you the processes that are running and the resources they’re consuming, like memory or cpu.

Screen Shot 2014-11-02 at 2.17.39 PMThe top section says the number of tasks and the number running, sleeping, stopped etc, along with the total/available/in use amounts of CPU and memory.

Each row is a process. The first column is the process ID (PID). On the right you can see which command each one is. init is the first thing that runs, is responsible for bringing the entire system up, and everything runs off of it.

Occasionally you’ll see top appear at the top, as it uses a bit of CPU.

Screen Shot 2014-11-02 at 2.22.19 PM

For the help, click ?.

Screen Shot 2014-11-02 at 2.35.41 PM

You can sort using F or O.

Screen Shot 2014-11-02 at 2.35.35 PM

CPU is starred, indicating that we’re currently sorting by that. To switch, type the letter of the option you want, then hit enter.

Screen Shot 2014-11-02 at 2.37.21 PM

To exit, just hit q.

ps is another tool that tells us the processes running in our current session. bash is the program that’s interpreting our keystrokes and formatting the output on the screen. ps is what we’re running to see the other processes, and since it’s one it makes sense it would show up here.

Screen Shot 2014-11-02 at 2.40.54 PM

If you add aux to ps is will show you a list of all the processes for the user “x”, which is all users, so everyone on the machine. This isn’t an interactive program like top, it simply displays all of them in the console. It’s used to tell you what’s running, and the PID for a specific task.

Screen Shot 2014-11-02 at 2.43.31 PM

To do that, we’ll open a new tab and run top. Now, we see top in the list when we run ps aux.

Screen Shot 2014-11-02 at 2.45.32 PM Screen Shot 2014-11-02 at 2.45.27 PM

The pst/1 and 0 column tell us which tab it’s running under.

If you want to find something specific in there, simply write ps aux, then a space, then the | (pipe) character. This let’s us combine programs together, so we take the output of ps aux, which lists the processes, and runs it through a new tool called grep, which let’s us filter for things in certain lines.

Screen Shot 2014-11-02 at 2.50.29 PM

Notice that the process running it, grep, shows up in the list as well because top is in there.


Pausing and Resuming

When you run some programs, they take up the entire window, hiding the command line, but you can do multiple things at the same time. A job is a process that you own and started from your console window.

We wrote nano demo.txt, which runs nano and opens up a new file.

Screen Shot 2014-11-02 at 2.56.55 PM

How do you get back to the command line without closing nano or opening a new tab? We can pause this job. To do that, press ctrl+z.

Screen Shot 2014-11-02 at 3.00.39 PM

First it tells us we can return to nano with fg. Then it will list our jobs. The first column is the job number. Note that this is NOT the process ID, but rather the job number. Then is gives us the state, which right now is Stopped, aka paused. Finally, the name of the job.

To return, use the fg (foreground) command, which brings you back to the most recently stopped program.

The jobs command prints a list of the jobs in this session. Notice that it says wd ~ to let us know the working directory it started from, because we’ve gone to a different one.

Screen Shot 2014-11-02 at 3.06.13 PM

If we open up top, we can pause that as well, and you can see now it has a job number of 2.

Screen Shot 2014-11-02 at 3.07.29 PM

If we type fg now it will take us to top, since it was the most recent job paused. If we wanted to go to the demo.txt job, simply type fg then the job number, 1. Notice the plus an minus next to their job number. Plus is the thing that’s going to be opened when you type fg, as it was the most recently opened than paused job.

If we were to close nano, top’s job number would still be 2. They don’t change if you close something else.

One last trick. If you type an & after top and run it, notice it doesn’t come up. If we run jobs, we can see that it was opened and just put in the background for us.

Screen Shot 2014-11-02 at 3.13.13 PM


Killing Processes

Sometimes a process goes out of control and you can’t quit it with normal means. Be careful, some of the methods here can shut down your machine or cause data corruption.

Normally when you exit a program it stops running but there may be cases where it’s still running and you don’t have direct access to it, as it’s not running from your terminal. A good example is when you’re running servers. You’ll need to first find the process ID using the techniques listed above.

This is then done using signals, which are messages sent to a process by the OS. The term (terminate) signal requests that the process terminates after any clean up. This is usually sent to something to shut it down.

We’ve used signals before. Using ctrl+z to pause a process is an example of one. We can also use ctrl+c to send the term signal to the current process. This is a consistent way to signal to the process you’re running that you want to terminate it.

So what if what you want to close isn’t in your current terminal? Let’s open a new tab and run top in it. Then, we’ll run jobs in the first tab. Since top is not in this session, it doesn’t show up, as jobs are per session.

Screen Shot 2014-11-02 at 3.24.34 PM

So how would we kill this? First we’d find the PID, using the ps aux | grep “top” method from before, which will return the PID for us. Remember to ignore the self referencing grep one. To kill it, we then use the kill command. Make sure to be careful with this, as if you type the wrong PID you could kill a system process by accident.

Based on your permissions, you may only be able to kill processes you own, but if you’re using sudo you can kill anything, so be even more careful.

So, we kill is, run ps aux again and see that it’s no longer running.

Screen Shot 2014-11-02 at 3.29.56 PM

By default, kill sends the term signal to the process. But sometimes a process can get in a state where it’s not even paying attention to the signals it’s receiving. What we can do is send an option with the kill command. By default, you’re basically sending “kill -TERM”.  The last resort if that doesn’t work is “kill -KILL” or “kill -SIGKILL”. These accept numbers as well, so you may also see it as “kill -9”. This isn’t like term where it tells it to clean up after it’s done, but rather is telling it to end it immediately. Avoid this when you can, and only use it when the term signal isn’t working.

Screen Shot 2014-11-02 at 3.35.35 PM

But when we go to check on the process on tab 2, we see something weird – the command line is within the previous text. This is because it hasn’t been cleared, and it will be overwritten if we write over it.

Screen Shot 2014-11-02 at 3.36.42 PM

This shows how kill is different. The normal terminate signal would clear out what was there before and place the command line appropriately. kill didn’t give it that chance.

The -STOP signal pauses it, but causes a similar issue where the command line just pops up.

Screen Shot 2014-11-02 at 3.39.45 PM

Still, you can clear that, run jobs and see that it’s exactly as if you pressed ctrl+z.

Screen Shot 2014-11-02 at 3.40.26 PM

So, kill is more about sending signals than closing a process every time.


Environment and Redirection

Environment Variables are like variables in any programming language.  In the console, they will be written in all CAPS and will contain strings. Some ones the system will expect are HOME, your home directory, and PATH, which is a path to you directories, the PS1 defines the format of your command line prompt.

To see what your env variables are, you can use the command env. It will print all of them out for you. They’re all capitalized, and are set equal to a value. A lot of these are set by config files on the system, but others we should recognize like the ones mentioned above. The PS1 one has a /u and /w then a $. These are special characters that allow us to inset dynamic info into our prompt. /u is replaced with the user’s username, and the /w with the current working directory, which allows are prompt to change as we traverse directories.

LS_COLORS is the colors set for the console.

Screen Shot 2014-11-03 at 11.41.22 AMThe echo command prints out the arguments that it’s given. Writing a $ before a variable name will return the value of that variable. So writing echo $HOME will print HOME’s value.

Screen Shot 2014-11-03 at 11.48.23 AM

Writing cd $HOME would take us to /home/treehouse.

To change a variable, type its name, then an equals sign, then the new value in quotes (so we can write a string with spaces and special characters). Notice how the prompt has changed once we’ve done that. This is because the command line is using the value of PS1 for what it displays. We can change this to anything really.

Screen Shot 2014-11-03 at 11.52.27 AM Screen Shot 2014-11-03 at 11.55.23 AM

bash is run automatically when you start a new instance. If we type the bash command, it will start a new instance for us, which ignores the changes we set in the previous instance to PS1. If we exit this instance, we can see it then takes us back to the original instance.

Screen Shot 2014-11-03 at 11.58.51 AM

To create a new variable, simply write one (in all caps) and set it equal to something. Let’s do that. But what if we open a new instance of BASH and echo the variable we made prior? We get nothing. This is because a env variable will by default stay in its own session. The new bash is a child of the original one, but the variable is not passed down.

Screen Shot 2014-11-03 at 12.02.21 PM

To pass it down, use exporting. Simply write export before the variable you set, and it will be available to child instances.

Screen Shot 2014-11-03 at 12.05.36 PM

Variables are useful for controlling programs. The ls command uses the LS_COLORS variable to determine how the results it prints out are colored.

Screen Shot 2014-11-03 at 12.06.52 PM

The PATH variable is important. It is a list of directories separated by colons, and is the list of directories to search for when we run an executable. For example, when we run echo, this is a command, and is a file that exists on our computer. To find out where, use the command which.

So, we could type /bin/echo to use echo, but no one wants to use absolute paths, so we use the PATH. If we type in a command that’s not a full path to the command, PATH will search these directories in that order for it.

Screen Shot 2014-11-03 at 12.11.59 PM

Sometimes we install executables that are not in these directories, but we want them to be available to us in our path. So to add to it, we can simply type export PATH=, then the new directory, then a colon, then $PATH since that contains everything else anyways. This will put the new directory in the front.

Screen Shot 2014-11-03 at 12.15.06 PM

If we start a new bash instance, it will be there, but we don’t want to do it this way, because usually it will be part of a start up script, and we’ll lose it if we have to restart our computer. In this case it’s bashrc, which we can edit with nano.

Screen Shot 2014-11-03 at 12.16.09 PM

Screen Shot 2014-11-03 at 12.20.00 PM

Here you can add it and save it. Usually you do this because a program you’re installing has asked you to.

Screen Shot 2014-11-03 at 12.22.13 PM


Find and Grep

Find and Grep are useful for finding files. find locates a file based on its name. You type find, then the directory for it to base its search off of. The current directory is represented by a dot. Here this will search the current one and all of its child directories. Then, -name, then the name of the file in quotes (the quotes aren’t always required but it’s good practice to add them for strings and spaces and characters). The results are listed one per line, and will give us the path to the files found.

Screen Shot 2014-11-03 at 12.32.23 PM

If you wanted to search through your entire system, which may take a long time, you can use / for the directory, which is the root directory.  Here, we get a bunch of permission denied messages, which are error messages, because there are a lot files/directories our treehouse user has no access to.

Screen Shot 2014-11-03 at 12.36.15 PM

To specify multiple directories to search from, just separate them by spaces.

Screen Shot 2014-11-03 at 12.37.50 PM

How about to search for something inside a file? You’d use grep, which lets you search inside a file for a pattern. It stands for Global Regular Expression Print. Every time it finds the pattern, it will print the line it appears on. You type grep, then pattern you’re searching for, then the file(s) you want to search through.

Screen Shot 2014-11-03 at 12.42.16 PM

You can also have it print out the line number it found it on by adding -n after grep.

Screen Shot 2014-11-03 at 12.44.24 PM

By default grep is case sensitive, but you can turn this off with -i.

Screen Shot 2014-11-03 at 12.45.21 PM

To look for lines WITHOUT a pattern, use -v. Here we look for lines that don’t contain the letter e.

Screen Shot 2014-11-03 at 12.46.53 PM

To read the grep or find manual, type man grep or man find.


Pipes and Redirection

When you run a program and create a process there’s a standard way of inputting and outputting: standard in and standard out.

Screen Shot 2014-11-03 at 1.03.47 PM

The out is usually text printed out in the console, and the in is the keyboard. You can change these around though. The out can be a file, so the output text is stored there. Or, the in can be a file. The in’s and out’s are always plain text, which lets you make the output of one process the input of another, which is called piping.

Screen Shot 2014-11-03 at 1.06.39 PM

You can have a bunch of small programs that each do one thing, then put them all together to make something more powerful.

Let’s do grep again. If we don’t enter a file name and hit enter, you’ll notice the command prompt doesn’t look normal. This is because grep is using the standard input to wait for data to be entered into it. If you type hello world and hit enter, it will highlight the lines where it found that pattern. Note that after that it’s still receiving input, and if you type just hi, you won’t get anything back. Standard in is what we write, out is what it prints back.

It will continue to wait for input from us until it finds the special end of file marker, which you can enter by doing crtl+d.

Screen Shot 2014-11-03 at 1.14.30 PM

Now, like we’ve done prior, we can also use a file as our standard input. Here we’ll do it with redirection, represented by a less than sign. Afterwards you write the name of the file that will be the new standard input. This isn’t that different that without it because grep also takes file name inputs, but some programs may only accept input via standard in, so this is how you’d do it.

Screen Shot 2014-11-03 at 1.18.20 PM

To change the output from the terminal print out to an output file, using a greater than symbol to redirect the output. Here, I forgot to put in a standard input, so I had to type it in. After we’re done, we close it with ctrl+d. Now there’s a new file called hello.grep, which contains the output.

Screen Shot 2014-11-03 at 1.22.06 PM

If we do this with the standard in, we don’t have to type it in, as the in this time is the hello.txt file.

Screen Shot 2014-11-03 at 1.23.26 PM

Notice that it overwrote the original copy. To append rather than overwrite, simply use two greater than signs rather than one.

Screen Shot 2014-11-03 at 1.26.33 PM

Remember the find sudeors from before with all the permissions denied? This is because there are two output streams from the process. One is what you’re looking for, like the results of the search, and the second is extra info like if errors occur. The second is called standard error, which also prints to the terminal. You can redirect that using 2>.

Screen Shot 2014-11-03 at 1.31.39 PM

If we want to see the errors, we can now just open the log.

But what if we don’t care about the errors? We can redirect it to a special file that will delete anything written into it – /dev/null.

Screen Shot 2014-11-03 at 1.33.47 PM

Now for piping. ps aux tells us all the processes running, and there are a lot of them. If we combine it with grep with |, we can use grep to search. ps aux provides the output, which will be the input of grep.

Screen Shot 2014-11-03 at 1.36.11 PM

Screen Shot 2014-11-03 at 1.37.26 PM

The sort command sorts the lines of its standard input and sends it to its output.

Screen Shot 2014-11-03 at 1.39.43 PM

We can then send the sorts output to a file like before using a redirect.

Screen Shot 2014-11-03 at 1.40.11 PM


Installing Software

Building Software from Source

For linux, a package manager let’s you instill, update and remove software with simple commands. Sometimes one, or the version you want of the package, isn’t available though, so you can downloading the source code and install it yourself.

Today we’ll be intalling SQLite. First we’ll install some tools to let us do that. You’ll only need to do this once for your machine. We’ll need sudo, then apt-get, which is part of the package manager system, then update, which is a command that will update the package manager database on our computer. So, when we go to install our build tools we’ll have the latest version.

Screen Shot 2014-11-03 at 2.04.45 PM

It will then fetch a bunch of urls, which are the databases that hold the packages that are available for our ubuntu system.

Screen Shot 2014-11-03 at 2.03.53 PM

We now do sudo apt-get install, which will install a package by name, which here is build-essential. Enter Y to proceed.

Screen Shot 2014-11-03 at 2.06.53 PM

To confirm if they were installed, use which make. Remember which tells you where a program is on the system. make is a program used for building things. The build-essential package included make, so since it’s here we know it was successful.

Now to actually install SQLite. It offers the source code as a zip, or a tar.gz file. The latter is like a zip file, but is usually for an archive. We’ll grab it’s link address. We’ll then use the curl program, which is used for making requests to the internet, and -O, will save the file to a file on our machine.

It saved it to ls, but we can make a directory for it using mkdir, and move it with mv.

Screen Shot 2014-11-03 at 2.14.08 PM

Now to untar the file, which is like unzipping one. The tags to extract it are -xvf. x is extract, v is verbose output and f is point to the file we want to extract.

Screen Shot 2014-11-03 at 2.17.35 PM

After installing, there’s now a directory next to our tar file. Let’s enter that and see the files inside.

Screen Shot 2014-11-03 at 2.19.00 PM

First we need to run configure. We’ll do ./configure, because the file is inside of our current directory. This runs through our system prepares for some more files that will be used to actually build the program.

Screen Shot 2014-11-03 at 2.22.08 PM

It’s created a special file called a MakeFile, which specifies how to build the program.

Screen Shot 2014-11-03 at 2.22.57 PM

To execute it, use the program called make from before when we installed build-essential. Make sure you’re in the same directory as your make file.

Screen Shot 2014-11-03 at 2.26.00 PM

Now we’ve built it, but not yet installed it. We need to run a different make task called make install. It’s already built the program so it won’t recompile everything, it will just move everything that it has to somewhere accessible in our path. Some of those may not be writable using our current user, so we’ll use sudo as well.

Screen Shot 2014-11-03 at 2.28.37 PM

To confirm that it installed, we’ll go home, the use which to see if it’s there, and even run it.

Screen Shot 2014-11-03 at 2.30.57 PM

To summarize what we did:

Screen Shot 2014-11-03 at 2.30.28 PM


Introduction to Package Managers

These make making installs way easier and make updates and uninstalls easier as well. Linux uses apt (advanced packaging tool) as its package manager. There are a variety of these for the different flavors of linux. OSX10 has its own called homebrew.

First, run sudo apt-get update like before. Now, let’s install git, a version control system. If we try to run just that, we get an error, because it’s not installed yet, which actually tells you how to install it.

Screen Shot 2014-11-03 at 2.38.31 PM

Another way to find out how to install it would be to use apt-cache, with the search command, which let’s us search based on a pattern.

Screen Shot 2014-11-03 at 2.39.50 PMScreen Shot 2014-11-03 at 2.39.56 PM

There are a lot of results, each with a name and a brief description, that you can use and look through to find the correct one.

Screen Shot 2014-11-03 at 2.41.56 PM

It gives you some info on what it will install, and it understands what tools this install may depend on, so it lists them under the “extra packages will be installed” part. Press Y to continue.

Screen Shot 2014-11-03 at 2.43.48 PM

Now, we can check for it using which git like before, and we can run it just by typing git.

Screen Shot 2014-11-03 at 2.44.47 PM

To update the software, write sudo apt-get upgrade. You typically want to run this after you’ve done an update. If you haven’t, our system won’t be aware of any new packages. You can see near the bottom it tells you that 172 will be upgraded, as well as the amount that will be downloaded and installed. Hit y to run it.

Screen Shot 2014-11-03 at 2.47.26 PMTo uninstall a program, write sudo apt-get remove, then the name of the package. Remember the two packages that were installed because git needed them? The message tells you they won’t be installed unless you use apt-get autoremove instead.

Screen Shot 2014-11-03 at 2.53.17 PM

Advertisements

New terms from treehouse – Console Foundations Part 1 – Getting Started with the Console and Users and Permissions

Getting Started with the Console

The console is another way to interact with your computer. You type in commands and get text back, it’s not like a Graphical User Interface (GUI). Why learn it? Some tools you might need my not have a GUI. Unix is an OS from the 60s that a lot of modern ones are based on, like GNU/Linux and Apple’s Darwin. Most servers runs some kind of linux based OS. They share a standard called POSIX, Positive Operating System Interface.

Windows is not a POSIX compatible OS. It works differently, making it a less desirable choice for web development.


Running Commands

We type in commands, and it will print out info as a response. It goes from the top down, so the latest info is at the bottom. The command line is where you type your commands. You’ll see some text followed by a blinking rectangle. The text is the prompt, which can give you some contextual info. treehouse is who I’m logged in as, and the wiggly horizontal line is the tilde, and is the directory we’re in. The $ tells us where the prompt ends and where our input begins.

Screen Shot 2014-10-24 at 1.41.46 PM

The ls command is short for list, and lists files. Type it in, then click enter to run it.

Screen Shot 2014-10-24 at 1.45.37 PM

The output gives two things. It means in the folder we’re in right now, there’s a folder called documents (in blue), and a file called hello.txt. Colors aren’t necessary, but can be helpful. Now let’s write ls -l. -l prints the long form of the list, giving us more info.

Screen Shot 2014-10-24 at 1.48.56 PM

The total 8 means there are 8 files in this folder that are not being listed by default. The line of letters and dashes specifies the permissions of this particular file. For drwxrwxr-x, d means directory, then there are three triplets of rwx, for read, write and execute. Each triplet means a differential level of people you can access them. The first triplet is for the user who created it. The second is a group, which is like a user but let’s you have multiple users in a group, an each file has a group associated with it. The third is for public permissions, and in this case you can see the w is omitted, so they can’t write.

For -rw-r—–, no one can execute it because it’s a text file.

There are then two treehouse’s list, the first being the user, and the second being the group. The 4096 and 416 is the size, listed in bytes. However, the 4096 isn’t the size of its contents, but rather this is a number they give to directories. Then, the date of the file and its name.

So why are the other 6 files not showing? If you start a file name with a dot, it will by default not show up in an ls listing. This is useful because it let’s us create hidden files. The -a option let’s us see those too. They’re usually configuration files made by other programs, and you usually don’t need to modify them.

Screen Shot 2014-10-24 at 1.59.50 PM

We can pass an argument as well. Currently we’ve been listing our current directory, but we can do any other one as well. /etc is the name of another folder.

Screen Shot 2014-10-24 at 2.02.30 PM

If we wanted to see them long form, we’d type ls -l /etc

Screen Shot 2014-10-24 at 2.04.07 PM

The user root is the admin of our computer, which we normally don’t use so we don’t accidentally delete an important system file. To clear the backlog of commands and responses, type clear.


Moving around the filesystem

Folders are usually called directories here. You’re always at some place in the folder tree when you’re in the command line. The pwd (print working directory) command displays your current position in the file system.

Screen Shot 2014-10-24 at 2.20.44 PM

In linux, your home directory is listed as home/yourusername. In Mac is users/yourusername. This is a special directory and it has its own shortcut, the ~.

The cd (change directory) command lets you change where you are. Type cd, then where you want to go. Some consoles have tab completion, where you start typing something and hit tab to complete it. Below, I hit doc then tab. If there are multiple options you can tab through them with tab. The trailing slash is in case you want to go into a directory inside of documents.

Screen Shot 2014-10-24 at 2.25.58 PM

Notice now our prompt has changed, and now displays ~/documents. If we check pwd it tells us we’re at home/treehouse/documents, and if we check this folder with ls there’s a new text file in here.

Screen Shot 2014-10-24 at 2.26.57 PM

Note that if we were to try and type cd documents now, it wouldn’t make any sense, because there’s no documents folder where we currently are in the directory. If we try, we get a response telling us there is none. -bash is the program we’re interacting with now, and is called our shell.

Screen Shot 2014-10-24 at 2.29.30 PM

That would be an example of a relative path. We can move around with an absolute path though. It starts with a / or a tilde, meaning we’re starting at the top, or the root, of our document tree. cd /home/treehouse takes us home.

Screen Shot 2014-10-24 at 2.32.50 PM

If we were at /home, and wanted to go to documents. We could write /home/treehouse/documents, or just use a tilde instead and do ~/documents.

Screen Shot 2014-10-24 at 2.35.19 PM

To go back to home we can just type cd ~.

Screen Shot 2014-10-24 at 2.36.19 PM

To go back down to documents form here, we can do cd documents. But what if we want to go back up? Simply type cd ...

Screen Shot 2014-10-24 at 2.37.59 PM

You can string this together, doing ../.. makes you go up two directories.

Screen Shot 2014-10-24 at 2.39.10 PM

If you typed home/treehouse/.., you’re actually saying you want to go up a level from the treehouse directory, which would be home again.

Screen Shot 2014-10-24 at 2.40.23 PM


Reading Files

So how do we see what’s inside that hello.txt file? We can use less, a program that displays the contents of a file. It takes as its argument the file you want to view. Notice we’re using a relative path since we’re where the file is at. We could do an absolute path as well and list out home/treehouse etc.

Screen Shot 2014-10-28 at 1.57.09 PM

Screen Shot 2014-10-28 at 1.57.55 PM

Notice how less takes up our entire screen/console. You can use the arrows to move up and down. The cursor has changed, there are now some commands we can use to move around. Spacebar will scroll one page at a time. (END) tells us it’s the end of the file. Pressing q at any point will quit and take you back to the command line.

Screen Shot 2014-10-28 at 1.59.36 PM

Fun fact – since less is based of the more program from the original Unix, you can type that as well, and it will run like less, but will try to emulate the older more program. These programs are called pagers, because they show one page at a time. The cat program will print the content of one or more files to the contents, and can concatenate files together. Notice how it just printed it out and took us back to the command line.

Screen Shot 2014-10-28 at 2.05.58 PM Screen Shot 2014-10-28 at 2.05.29 PMTo concatenate just list the files you want to do that to.

Screen Shot 2014-10-28 at 2.07.17 PMScreen Shot 2014-10-28 at 2.07.22 PM


Editing Files

nano is a text editor that works on the console, and is named after the old Unix program pico. Just type nano then the text you want to edit.

Screen Shot 2014-10-28 at 2.26.41 PMScreen Shot 2014-10-28 at 2.26.45 PM

It’s another full screen program. You can move around with arrows, type and click enter to make spaces. There are shortcuts at the bottom. The carrot means the control key. To get help, press control+G, which takes you to another screen with more commands.

Screen Shot 2014-10-28 at 2.31.34 PM

If you exit the editor and have made changes, it will ask you if you want to save.

Screen Shot 2014-10-28 at 2.32.22 PM

vim and Emacs are other, more powerful options, but have a steeper learning curve.


Moving and Deleting Files

Let’s say we want to rename a file hello.txt to hi.txt. Instead of thinking of it as renaming it, think of it as moving it from the hello.txt location to the hi.txt location. mv is the move command, and takes two arguments, from where we want to move something, and where we want to move it.

Screen Shot 2014-10-28 at 2.38.56 PM

To move it from one directory (remember, in the console that means a folder) to another), it works the same way. To move it into documents, just write that as the second argument with a / after it. It will move it from the current directory to the new one with the same file name.

Screen Shot 2014-10-28 at 2.41.22 PM

Note that ls documents/ didn’t move us into the documents directory, but rather just listed what was in there. To move it back, just switch the arguments. We can use tilde, since we’re going home/treehouse, .., to go up a directory, or finally ., which is the current directory we’re in.

Screen Shot 2014-10-28 at 2.43.55 PM

If we were to do this:

Screen Shot 2014-10-28 at 2.45.01 PM

It would not only move it to the documents directory but also rename the file to hello.txt.

mv also works for directories.

Screen Shot 2014-10-28 at 2.46.43 PM

Once again, to move it back we just switch them.

Screen Shot 2014-10-28 at 3.55.06 PM

The cp command copies things for you, leaving the original one in place. One again it takes two arguments, the first being what you want to copy, the second being want you want to name it/move it to.

Screen Shot 2014-10-28 at 3.57.27 PM

cp only works for files though. To copy a directory, you need to type -r, which will recursively copy all the things within the directory.

Screen Shot 2014-10-28 at 3.59.43 PM

We can see that it copied the file within it.

Screen Shot 2014-10-28 at 4.00.38 PM

The rm command removes a file or directory. Be very careful, there isn’t a way to undo this. It’s not like your OS that has a recycle bin.

Screen Shot 2014-10-28 at 4.03.15 PM

rm can’t remove a directory by itself. We need to use -r again.

Screen Shot 2014-10-28 at 4.06.06 PM

To make a directory, use the mkdir command, which stands for make directory. The argument it takes is the name of the directory/path you want to make.

Screen Shot 2014-10-28 at 4.07.30 PM

To make a directory inside of other ones. mkdir documents/notes/console wouldn’t work because there isn’t a notes directory yet. You can either make notes then console:

Screen Shot 2014-10-28 at 4.09.56 PM

Or, you can create a nested directory, however levels deep, by using -p with mkdir.

Screen Shot 2014-10-28 at 4.11.13 PM


Users and Permissions

Creating Users

You can set up multiple users, each with their own login, password and home directory. It’s a place for each user to put their stuff. There are files outside of the home directory that are used by the computer and typically not accessible to users.

Had to do some code to get this to work apparently.

Screen Shot 2014-10-28 at 4.18.20 PM

You can learn which user you are by looking at the command prompt (right now I’m treehouse), or by typing the command whoami.

Screen Shot 2014-10-28 at 4.19.51 PM

The adduser command let’s you add a new one. The argument it takes is the name of the user you want to add. However, you also need to be running in a special mode called sudo. It will then ask you for the password of the person you’re currently logged in as. In the example below we forgot the name argument the first time. It didn’t ask for the p/w again though because it was recently typed in.

Screen Shot 2014-10-28 at 4.23.39 PM

So it created a user mike, then a group mike, then a home directory for him, then copied some files to get it started. Now we put in the p/w for that user, in this case html5.

Screen Shot 2014-10-28 at 4.25.16 PM

Now we put some info in about the user, which isn’t mandatory. Then you can review the info to see if it’s correct and press Y for yes, n for no. Y is capitalized, which is a convention meaning it’s the default option if we don’t hit anything. So, if we want yes we don’t have to type it, we can just press enter.

Screen Shot 2014-10-28 at 4.27.50 PM

To switch to this new user, you can use the su (switch user) command. You enter their p/w, and then you’ll be then. Notice how the prompt has changed. We can use whoami to see that we’re mike, and pwd to see we’re in the home directory. When you switch to a new user, you remain in the same directory that you were in before, in this case home/treehouse, which is NOT mike’s home directory. To go there we type cd.

Screen Shot 2014-10-28 at 4.32.20 PM

So how do we switch back to the treehouse user? We can use su treehouse, or the exit command, which takes us out of the switched user that we were in before. Notice how we’re still in home/treehouse – where we were as mike has not affected where the treehouse user was.

Screen Shot 2014-10-28 at 4.34.37 PM


File Permissions

Read, Write and Execute (rwx) are self explanatory. Execute only comes into play with programs. They are defined for three different entities:

1. User (u) – who made it

2. Group (g) – each user has a primary group that matches their username, but can be a part of many different groups.

3. Other users (o) – for those who are not the user owner, or in the group.

They are abbreviated u, g and o, in that order, giving you 9 permissions for each file. ls -l let’s you see those permissions.

Screen Shot 2014-10-28 at 4.44.44 PM

The first digit tells you whether it’s a directory. Then u, g and o’s permissions in triplets. A dash means they don’t have that permission. The chmod command changes the permissions of a file or directory. Then the user and group names are listed. The txt file doesn’t have an x permission because it’s a text file.

It takes a few arguments, first who we’re changing this for (u,g,o). Then whether add or remove the specified permission, using a + for add or a – for remove. Then, the permission you want to change. Finally, the file or directory you want to change this for. Here, we gave o the permission to write on hello.txt.

Screen Shot 2014-10-28 at 4.49.44 PM

To add a permission for every, omit the first argument for ugo, then the change.

Screen Shot 2014-10-28 at 4.51.17 PM

To remove one:

Screen Shot 2014-10-28 at 4.52.20 PM

Permissions are usually represented using octal notation, which is base 8 instead of 10. When you add one to 7, the answer is still 8, but is represented by 10.

Screen Shot 2014-10-28 at 4.53.52 PM

It’s used because there are 8 possible combos of rwx. You’ll see three numbers written, corresponding to u, g, then o.

Screen Shot 2014-10-28 at 4.55.13 PM

To remember the values, each is assigned a value, just add them up. – is zero.

Screen Shot 2014-10-28 at 4.56.17 PM

You can use this with chmod. Right now hello.txt adds up to 666. To add x to each ugo, we can do 777.

Screen Shot 2014-10-28 at 4.58.56 PM

Another example.

Screen Shot 2014-10-28 at 5.00.46 PM


File Ownership

The chown command can change the owner of a file or directory. The arguments you put in are the new owner, then the file or directory. It won’t work in this case unless we put sudo, which will override any permissions. Here, we’ve reassigned hello.txt to mike.

Screen Shot 2014-10-28 at 5.17.49 PM

Since we’re signed in as treehouse, and now only have the group permissions, we can only read it now, and not write or execute it. So the cat command would work, but the nano one would tell us there’s no write permission, so we could change but wouldn’t be able to save with control+o.

Screen Shot 2014-10-28 at 5.21.02 PM

Now, when you try to exit it will still ask you if you’d like to save, but you can’t, so just choose No.

To change the user and the group, put a colon between them.

Screen Shot 2014-10-28 at 5.23.03 PM

If you wanted to read or write to it now you’d just have to su as mike, or reassign it back to treehouse.


Sudo

What if you want to change things but don’t have permission? There is a special user on every computer called root, who overrides every permission on every file and can do anything. If you know their password, you could su as them, but this is less than ideal, because it requires three steps, and being root can be dangerous if you do something by accident. It’s better to work as a normal user and only use the permissions when needed. It’s better to use the sudo command, which lets you run a command with the privileges of root. This is also better because you can give certain people permission to use sudo, rather than let them know the password to root.

Example: before when we were changing the ownership of hello.txt. When you first use sudo, it will require a p/w to be used. It’s not the password of the root user, but rather the users password. It’s a system that allows certain users to use it. This helps ensure that someone might not be logged in as you, and gives you a moment to think about whether you want to do the command. It won’t prompt you for the p/w for a specified amount of time.

Screen Shot 2014-10-28 at 5.33.01 PM

Fun shortcut – !! represents the last command that was run. It will print the command that was run, and also it’s output. Sometimes referred to as a bang. Good use case: If you forgot sudo for a command, you just can type sudo !! rather than having to recall it with up arrow and move the cursor over.

Screen Shot 2014-10-28 at 5.39.52 PM

Note that you can only use sudo if it’s been enable for you as a user.