Linux Commands for Beginners Part 2: Exploring the CLI

beginner-linux-part-2_featured-image

This article, which continues from Beginner Linux Administration, will focus mainly on the command line interface, also known as the CLI. We will explore features using ls, grep, piping and other tools used for generating info, creating files and directories, and a brief introduction to permissions.

In general you may be using a GUI to do everything, but using the CLI will give you a deeper, more profound understanding of how your Linux machine works. You’ll be surprised at how much you can learn about the OS as well as where certain files are located, why, how to edit, and accessing them quickly and more efficiently.

Using ls

Using ls is useful for quickly displaying files and directories, who created them, who owns them, and whether I have permission to read, write, and execute. You can also use it to quickly display a particular file. For example:

I’ll first start in the root directory (so issue the command cd /) then type the command ls.

All “ls” does is display the files and directories that you are in. Not much info as you can see, but there are several different options you can use along with “ls” to get the info you are looking for.

Exploring the CLI 1

Let”s change to the /etc directory and run the ls command: cd /etc

You’ll notice when you run “ls” this time you see too many options and you can’t read all of them. So what we will do is execute “ls” but we will pipe the output of ls to the less command.

Piping
Redirects the output of one program into the input of another. The symbol is |. The command to the left is sent as input to the command to the right of the pipe.

Type ls | less

Exploring the CLI 2

Exploring the CLI 3

The files are displayed with colons at the bottom. Use the arrow keys to scroll up and down. All you have to do is hit the “q” button to get out of less.

Less
Less is a program that allows backward and forward movement. It is similar to opening a file in an editor but it doesn’t have to load the whole file before viewing which is helpful if you are dealing with large log files.

You can also just use less to view your files. For example we want to view the passwd file so we can quickly view all of the newest users added to the system.

[The passwd file stores essential info containing system’s accounts with user and group ID’s, home directory, shell, etc.]

Make sure you’re in the /etc directory and type “less passwd.”

Exploring the CLI 4

If you scroll down to the bottom (or type “G”) you will notice the last few users added to the system. Type “q” to quit less and let’s try it with the file called group.

Type less group

Exploring the CLI 5

Go ahead and scroll around and then type “q” to quit.

Exploring the CLI 6

Basically, less is just an alternate way to view files and directories but it is much more convenient than viewing data then using the cat command. We will learn how to edit this data later using vi.

You can issue the commands “cat passwd” and “cat group” to see the differences.

Also, there are other keystrokes you can use with less to quickly navigate within a document. You can always issue the command “man less” for a complete breakdown of what less does, and the other options available.

Exploring the CLI 7

Using ls with Options

So, we’ve used “ls” on its own and we’ve used it with a pipe and less. Now we’ll use ls to find specific files we are looking for, as well as displaying more information associated with said file.

Let’s go back to the root directory, cd / and then type ls

Exploring the CLI 8

Then type in ls –l

Exploring the CLI 9

You’ll notice everything is displayed in long format which now includes info about permissions, group, size, date, and filename.

Permissions – drwxr-xr-x
Directories – 2
User – root
Group – root
Size – 4096
Date – May 5 04:03
Directory or File – bin

    The permissions break down is in a set of 3 levels. User, Group, and Other. The firsts set of rwx’s is for the user, the second if for the group, the third is for everyone else.

  • Read – you can read the file
  • You can write or make changes
  • Execute – you can execute the file

We will get into this in more detail and change permissions levels later using the chmod script.

Directories – The amount of directories within the directory
User – The user that owns the file
Group – Group assigned to the file or directory
Size – This should be obvious
Date – Date of last modification
Directory or file – Name of the directory or file

Other options to use with “ls” are:

-a – Displays all files, even those that are hidden
-g – Same as l except the owner is not printed
-r – Reverse order of how files are displayed
-m – Output is listed across the page

There are quite a bit of options and you can also use these in combination with each other.

Such as “ls –lar.”

Exploring the CLI 10

As well as “ls –lar | less.”

Exploring the CLI 11

As you can see, the “ls” command is just a simple way of displaying output in the command line interface but with the many different features associated, you can display the info to how you want it presented.

Let’s finish off this lesson with the grep command.

    Grep – is a command that searches for a specific string in a file and prints out matching a match to given strings or words.

You can use grep with “ls” to locate a file you need but are unsure of the exact title.

If I’m in the /etc directory and am looking for a particular host file.

I will issue the command “ls |grep host.”

Exploring the CLI 12

Then you can use “cat”, “less”, or even an editor like vi to view or alter the specific file you are looking for.

Let’s say you want the information on the user Roman that is in the passwd file. I would issue the command “grep roman passwd.”

Exploring the CLI 13

As you can see it will just give me the information associated with the search word “roman.”

If you’re not in the directory of the file you are looking for you can always add the directory info in front of the file you are searching.

“grep igby /etc/group” – For user igby (replace with your own)

or

“grep roman /etc/passwd”

or

“grep roman /etc/hosts”

Exploring the CLI 14

These are just simple commands that can be used for a multitude of purposes, but if you’re new to using the CLI you can play around with them and get a little more comfortable using them in different directories and your own files that you’ve created.

Hopefully this article has been informative for you. We will advance on the commands we’ve talked about in the next lessons, as well as get into the many others that are available.

0
 in LINUX

Comments