Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo
- - - - -

A simple lesson for beginners about Linux Terminal.


  • Please log in to reply
1 reply to this topic

#1 Naught McNoone

Naught McNoone

  • Members
  • 308 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:The Great White North
  • Local time:09:32 PM

Posted 14 November 2015 - 08:44 PM

A simple lesson for beginners about Linux Terminal.

This is a simple lesson for beginners about Linux Terminal. It is intended to introduce the novice to terminal and make him/her comfortable while using it.

This exercise is done using Linux Mint Cinnamon, and has been tested using Xubuntu.
There may be some variation, when done with other distributions.

To complete this lesson, you will require a functioning Linux installation.


1. Introduction and Background:
Terminal has such an ominous connotation. It sounds so final!
So, what is Terminal?

The name Terminal comes from the beginning of multi-user Unix systems. In the days before individual PC's and the GUI (Graphical User Interface,) the system consisted of a central computer which ran the core or kernel.

Users would log in the the system from a remote terminal, which was essentially nothing more that a keyboard and display, attached to the main computer by wire. You typed in your command, in text, and the result was “echoed” back to you.

You were at the end, or “Terminal” of the wire.

In 1967, the first terminal I ever used was an IMB Selectric typewriter, connected to a main frame in another part of the building. I typed my commands into the terminal, and it printed the result.


2. Terms used in this lesson:
<key> = The key to be pressed, for example <shift>, <alt>, <ctl>, &c.

<key1> + <key2> + <key3> = A combination of keys to be pressed simultaneously, for example <ctl> + <alt> + <t> are pressed at the same time to open an terminal on your desktop.

boldtext = A simple command to be typed as is and followed by the <enter> key, for example “ls”.

"boldtext -switch" = A command followed by an optional switch. It is to be typed without the quotation marks and followed by the <enter> key,
for example "ls -l"

"boldtext space more text" = A command followed by a string of text separated by spaces. It is to be typed without the quotation marks and followed by the <enter> key, for example "ls -l > list.txt"

 

3. Preparation:
You should begin at your desktop.

I suggest closing all windows, or moving them to another desktop to keep your screen uncluttered.

You will need to keep your browser open, to continue to view this lesson.

Lets start by opening a terminal on your desktop. You can do this by opening your menu, and selecting Accessories. There you will find an application named Terminal.

But there is a short cut built into your system. Press <ctl> + <alt> + <t> and watch what happens.

A window will open on your desktop, that looks something similar to the Windows CMD promt. (Actually, terminal came first, so the Windows CMD looks like the Terminal!)

You can now type a command, and see the results.

Lets take a minute to examine the terminal.

It looks like any other window, with a title bar that has minimize, maximize and close buttons in the upper right corner.

The text in the top left is your prompt. It should look something like this:

                user@computer ~ $

The prompt tells us something.

        user              = our user name
        computer      = our system name
        ~                   = our home directory
        $                   = bash is ready to accept a command

The user and system name are pretty well self explanatory.
The ~ can be followed by what ever subdirectory name you are in.
The $ is the bash prompt. Bash stands for “Bourne Again Shell.” The shell does a similar job to the old DOS shell, or the newer Windows CMD shell.
(Note:  It has nothing to do with Jason Bourne!)

Are we ready to go? Good, let's do this!

-------------------------------------------------------------------

4. Simple Commands:
Lets try a simple command to begin with.

The ~ character tells us we are in our home directory, but where exactly is that?

Type the command pwd and press <enter>.

The result should look something like this:

 

        user@computer ~ $ pwd
        /home/user

pwd is short for “Print Working Directory.” It will tell you the path to where ever you are in your system. So if you ever get lost, and need to know where you are, just type pwd, and it will tell you!
Lets try another command. Type ls and press <enter>.

 

user@computer ~ $ ls
Desktop Music Public Documents PDF Templates Downloads Pictures Videos
user@computer ~ $

 

ls is short for “List Segments.” It is a throw back to the old MULTICS operating system, in which files were referred to as “segments.” So what you are actually saying is “List Files.”

The command displays a list of files and subdirectories in you current directory. In your home directory you will find subdirectories called Desktop, Documents, Music, &c.

This is easy, isn't it! Lets go on.
-------------------------------------------------------------------


5. Command Switches:
A switch is an option that can follow a command. It tells the command to change from the default output, to something else.

Enter the ls command with the -l switch like this, “ls -l”.

user@computer ~ $ ls -l
drwxr-xr-x 2 user user 4096 Nov 13 19:07 Desktop
drwxr-xr-x 6 user user 4096 Nov 5 20:36 Documents
drwxr-xr-x 2 user user 4096 Nov 14 12:32 Downloads
drwxr-xr-x 2 user user 4096 Oct 14 22:54 Music
drwx------ 2 user user 4096 Oct 18 17:32 PDF
drwxr-xr-x 3 user user 4096 Nov 12 17:47 Pictures
drwxr-xr-x 2 user user 4096 Oct 14 22:54 Public
drwxr-xr-x 2 user user 4096 Oct 14 22:54 Templates
drwxr-xr-x 2 user user 4096 Nov 3 18:17 Videos
user@computer ~ $

We have changed the output of the ls command to give us more information than just the file name. We now have permissions, owner, group, size, date, and time. (More about those things in another lesson.)

Here is another switch to try. “ls -a” will give us a list of all files, including hidden ones! Here is what your output will look like. Don't worry if it is not exactly the same as this.

user@computer ~ $ ls -l
.                       .gconf                     .linuxmint                   Public
..                      .gimp-2.8                .local                          .pyrenamer
.adobe              .gksu.lock               Templates                  .bash_history
.gnome2            .themes                  .bash_logout              .gnome2_private
.macromedia     .thumbnails             .cache                        gota.jnlp
.mozilla              Videos                   .cinnamon                   .ICEauthority
.mplayer             .config                   .icons                         Music
.dbus                  install_ubuntu.sh   .nv                              Desktop
.kde                    .wine                    .dmrc                           lesson
PDF                    .Xauthority            Documents                 Pictures
.xsession-errors   Downloads           .pki                             .dvdcss
.profile
user@computer ~ $ ls -l

Warning!
Do not attempt to access or change any hidden files at this time.
Your system may become unstable, broken and/or unusable!

 

In Linux, simply placing a dot at the beginning of a file makes it a hidden file! More about the Linux file system later. Read on.

So, how do I find out about which switch is which for what command?

You use the “help” switch, of course!

Enter the command “ls --help”.

user@computer ~ $ ls --help

Hey! A whole bunch of text just flashed by, and I could not read it! What do I do now?

The help switch gave us a list of switches and options that we can use with the ls command. But it was so much information, that we could not read it all in our small window.

So, now we are going to learn another trick in terminal.

Hold down the shift key and press page up. <shift> + <pgup>

What happens? You are now able to scroll back through your terminal, and review the commands and output from this session.

To go forward, use <shift> + <pgdn>.
To go straight to the beginning of the session, use <shif> + <home>.
To go straight to the end of the session, use <shif> + <end>.

As you scroll up, using the page up, you will be able to view the output of the help switch.

 

Are you ready for this.  You can combine switches with a command.  Look at the -h and the -G switch. -h is also listed as “--human-readable” and -G is listed as “--no-group”.

Lets combine it with the -l switch. Do not forget that switches are CASE sensitive.

user@computer ~ $ ls -l -h -G
drwxr-xr-x 2 user 4.0K Nov 13 19:07 Desktop
drwxr-xr-x 6 user 4.0K Nov 5 20:36 Documents
drwxr-xr-x 2 user 4.0K Nov 14 12:32 Downloads
drwxr-xr-x 2 user 4.0K Oct 14 22:54 Music
drwx------ 2 user 4.0K Oct 18 17:32 PDF
drwxr-xr-x 3 user 4.0K Nov 12 17:47 Pictures
drwxr-xr-x 2 user 4.0K Oct 14 22:54 Public
drwxr-xr-x 2 user 4.0K Oct 14 22:54 Templates
drwxr-xr-x 2 user 4.0K Nov 3 18:17 Videos
user@computer ~ $

We see now that our group name has been omitted and that the size is displayed in K, M, or G.

Guess what! Switches are stackable, just like drop scones! 

You can stack the switches like this. “ls -lhG” Try it! Same result.

 

Note that many switches are standard, and will apply to many different commands. But not all commands are the same, and each has it's own unique set of switches and options that work with it.

To find out more about a specific command, you can use the “man” command as a reference.

 

Type the command “man ls

You are now looking at the text based man page for the ls command!
You can use the up and down arrow, pgup, pgdn, home or end keys to navigate through it.
To quit, and go back to your bash prompt, just type “q”.

    Manual page ls(1) line 1 (press h for help or q to quit)

Every command you have available to you has a man page! There you will find information about syntax, usage, switches, and options. If you get stuck on using a command, try the man page before you post for help. It will save you lots of time.

Moving on . . .
-------------------------------------------------------------------


6. Commands with Strings attached:
This is almost exactly what it sounds like. You are putting a condition on the command! But in this case, a string means a line that follows the command, giving the command more instructions about what it should do.

Strings can be plain text, symbols, pipes, and in some cases, even other commands.

A simple string can be one word, that a command needs to perform it's job.

Enter the following command “mkdir lesson”. Now, one of two things will happen.

You should get this:

user@computer ~ $ mkdir lesson
user@computer ~ $

Which appears to be nothing. However, something did happen. You made a new subdirectory called “lesson” in your home directory.

Do the ls command, and you will see it!

If the command had failed, you would have gotten and error, like this:

user@computer ~ $ mkdir lesson
mkdir: cannot create directory ‘lesson’: File exists

This error means that the directory already exists. You can not have two directories or files with the same name in the same location. So the Make Directory command, mkdir, came back with an error. (You did the lesson on permissions, didn't you?)

If you did not get the error the first time, try it again, and you will!

Another command that requires a string attached to it is the Change Directory command, or “cd”.

Enter the command like this, “cd lesson”.

user@computer ~ $ cd lesson
user@computer ~/lesson $

Notice that our prompt changed! It now shows us in the lesson subdirectory of our home directory. Remember our pwd command from the beginning? Try it now.

user@computer ~/lesson $ pwd
/home/user/lesson
user@computer ~/lesson $

Lets try a combination of a switch and a pipe, to create a new file.

Type the following command “ls -lR ~ > list.txt”. Remember, use UPPER CASE R. Also on a standard US keyboard, the “tilde” or ~ key is located to the left of the 1 key on the top row.

user@computer ~/lesson $ ls -lR ~ > list.txt
user@computer ~/lesson $

Again, nothing appeared to happen. But we did not get an error message, so something must of worked.

Do the ls command with no switches.

user@computer ~/lesson $ ls
list.txt
user@computer ~/lesson $

We now have a file in our subdirectory called list.txt.
Lets use the Catenate, or “cat" command on the list.txt file.

user@computer ~/lesson $ cat list.txt

I am not going to list the output, here. But you can see that you now have a list of all the files, by directory, in you home directory. Awesome, isn't it!

By the way, cat does more than just list the contents of a file. But that is for a more advanced lesson.
-------------------------------------------------------------------

7. Summary and Practice:

Time to conclude and let you loose on your own.

I hope that I have helped you become more comfortable with the command line, and given you an understanding of what terminal is used for.

I encourage you to learn more on your own. There are lots of primmers out there, just use Google to find them.

Use the man pages. That is why they are there.

If you have a question, and can't find the answer yourself, use the Linux help forum. There are plenty of experienced people there who can provide guidance and tutoring.

Here are few commands that are (mostly) harmless you can practice with.

pwd          – displays current working directory
dir            – displays the contents of the current directory
rename    – renames a file
mv           – moves a file
cp            – copies a file
mkdir       – make a new directory
cd            – changes to another directory
rm            – removes (deletes) a file
rmdir        – remove an empty directory
clear        – clears the screen
help         – gives a list of commands built into the bash shell (be careful)

And of course, the final command you need to know is “exit”.

user@computer ~/lesson $ exit

This will end the session and close the terminal window! It is the preferred way of exiting a shell terminal, rather than clicking on the x in the upper right corner.

Feed back is welcome.

Cheers!

Naught McNoone.



BC AdBot (Login to Remove)

 


#2 Naught McNoone

Naught McNoone
  • Topic Starter

  • Members
  • 308 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:The Great White North
  • Local time:09:32 PM

Posted 28 August 2018 - 07:01 PM

Update:

 

On LIne:  Type the following command “ls -lR ~ > list.txt”.

 

Should Read:  Type the following command “ls -lR /home/username/  > list.txt”.

 

NOTE:  The ~ is no longer required in front of the > to redirect the output to the file.

 

"ls [OPTION]... [FILE]... > /path/filename" with give the same result.

 

Thank you to user johnmdg for questioning that!

 

Cheers!

 

Naught


Edited by Naught McNoone, 28 August 2018 - 07:11 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users