fbpx
Linux 101

Your first Linux commands – Lesson 4 – Your first contact with the Linux command-line

Commands covered: ls, pwd, cd, chmod

Okay, so you have Linux up and running. Fire up a terminal so you can work on the command line. You should see the command prompt as shown in this video. First, let’s check out where we are in the file system.

pwd (“print working directory”)
Print the current working directory. Now let’s check the contents in this directory.

ls (“list directory contents”)
For this, we use the ls command. Notice how the listing is ordered in a wide format. Okay, that’s a concise list, but we would like it to be a bit more readable. So we type in ls -l instead. -l stands for long format. All commands have switches that can be used to change the behavior of the command. To quickly see what switches you have for a command, type in the command and –help. Let’s try it.

ls --help

Okay, there we see all the switches available for the ls command.
Let’s list the home directory in long format and sort the output according to modification time. We type ls -lt and this is what we get.

man (“interface to reference manuals”)
Whenever you want to understand how to use a Linux command, you can use the man command. With that, you can open the manual for a particular command. Let’s type in man ls.

Knowledge Check

Directory and file rights

If we list the contents of the directory again using ls -l, we see that there are plenty of characters and hyphens in front of each listed file and directory. Notice the output when I list the content in my current directory. And note that you could have other users as owners as well as other files with specific file permissions. At the left-most part, we see toggles for the rights for users and user groups. Let’s go over the following example:


-rw-r—r-- 1 ubuntu ubuntu 8980 May 1 2015 examples.desktop

The first hyphen signifies that this is an ordinary file. If you see the attribute d in this place, which means that the item is a directory. You might not have this file on your particular Linux distribution, so pick any file in your home directory. This particular file was on an Ubuntu Linux installation.

File permissions for (owner, group, and other)

The following bits are organized in groups of 3 bits each, namely user, group, other, abbreviated as ugo. The rights that can be assigned to a file or directory are read, written, and execute. And this is the order that the rights are written:
rwxrwxrwx (owner, group, all users)
So the file permissions are listed for owner, group, and other in that order. Using these combinations of r, w, and x, you can specify whether each user category has the ability to read, write and execute the file.

Where a hyphen is displayed that means that the corresponding file/directory right is revoked. For instance, if we look at the file examples.desktop above:
-rw-r—r--
Here we can see that the user has read and write rights, the user group has read rights, and all others have read rights.
We also see that the file is owned by the user ubuntu that is a member of the group ubuntu.
ubuntu ubuntu (user group)

An example for limiting file permissions

Let’s say we want to close down the reading rights to only this user, ubuntu. In other words, we want to revoke read rights for the group ubuntu (which could have more user members than this user ubuntu) as well as all other users.
We want these rights:

rw- --- ---

To change the file permissions we need the command chmod. This will take the file or directory to change permissions for as well as the new rights, like this:


chmod 600 examples.desktop

Oops, wait a minute! How does 600 give us those memberships? Well, we’re using the octal number system here. You could also use the character combinations ugo with a plus or minus sign for the particular right. Examples:

o+x -> Other group gets execute permission.
u-r -> User group is denied read permission.
a+w -> All groups are given the write permission.

It is entirely up to you if you want to edit permissions with the characters or with octal numbers.

Knowledge Quiz!

Let’s list the file again.
-rw--—--- 1 ubuntu ubuntu 8980 May 1 2015 examples.desktop
That worked like a charm! But let’s change back the rights again, as this file should be readable for Group and Other as well.
chmod 644 examples.desktop
That should do it. Great job, thanks for watching this lesson and let’s continue with lesson five, “Let’s move around the filesystem a bit“.