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.
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.
The ls command is short for list, and lists files. Type it in, then click enter to run it.
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.
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.
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.
If we wanted to see them long form, we’d type ls -l /etc
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.
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.
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.
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.
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.
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.
To go back to home we can just type cd ~.
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 ...
You can string this together, doing ../.. makes you go up two directories.
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.
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.
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.
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.
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.
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.
If you exit the editor and have made changes, it will ask you if you want to save.
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.
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.
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.
If we were to do this:
It would not only move it to the documents directory but also rename the file to hello.txt.
mv also works for directories.
Once again, to move it back we just switch them.
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.
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.
We can see that it copied the file within it.
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.
rm can’t remove a directory by itself. We need to use -r again.
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.
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:
Or, you can create a nested directory, however levels deep, by using -p with mkdir.
Users and Permissions
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.
You can learn which user you are by looking at the command prompt (right now I’m treehouse), or by typing the command whoami.
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.
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.
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.
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.
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.
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.
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.
To add a permission for every, omit the first argument for ugo, then the change.
To remove one:
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.
It’s used because there are 8 possible combos of rwx. You’ll see three numbers written, corresponding to u, g, then o.
To remember the values, each is assigned a value, just add them up. – is zero.
You can use this with chmod. Right now hello.txt adds up to 666. To add x to each ugo, we can do 777.
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.
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.
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.
If you wanted to read or write to it now you’d just have to su as mike, or reassign it back to treehouse.
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.
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.
Note that you can only use sudo if it’s been enable for you as a user.