Introduction to Linux

LinuxLinuxBeginner
Practice Now

Introduction

This Lab introduces the history and some of the basic concepts of Linux, such as shell commands and shortcuts. If you have already learned that, you can skip this Lab and start the next one directly.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/tail("`File End Display`") linux/BasicSystemCommandsGroup -.-> linux/help("`Command Assistance`") linux/BasicSystemCommandsGroup -.-> linux/man("`Manual Access`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/pwd("`Directory Displaying`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills linux/cat -.-> lab-18001{{"`Introduction to Linux`"}} linux/tail -.-> lab-18001{{"`Introduction to Linux`"}} linux/help -.-> lab-18001{{"`Introduction to Linux`"}} linux/man -.-> lab-18001{{"`Introduction to Linux`"}} linux/cd -.-> lab-18001{{"`Introduction to Linux`"}} linux/pwd -.-> lab-18001{{"`Introduction to Linux`"}} linux/mkdir -.-> lab-18001{{"`Introduction to Linux`"}} linux/ls -.-> lab-18001{{"`Introduction to Linux`"}} linux/touch -.-> lab-18001{{"`Introduction to Linux`"}} shell/comments -.-> lab-18001{{"`Introduction to Linux`"}} shell/quoting -.-> lab-18001{{"`Introduction to Linux`"}} shell/globbing_expansion -.-> lab-18001{{"`Introduction to Linux`"}} linux/wildcard -.-> lab-18001{{"`Introduction to Linux`"}} end

History of Linux

Linux, originating in 1991 as an alternative to costly UNIX and MINIX, evolved into a robust operating system through Linus Torvalds' kernel and contributions from a broad community. The GNU project complemented Linux with essential software, culminating in its recognition as GNU/Linux. Milestones include the birth of UNIX in the 1960s, BSD's emergence in 1977, and Richard Stallman's GNU project in 1984. Linus Torvalds' development of the Linux kernel in 1991 marked a pivotal moment, leading to subsequent releases and the adoption of the penguin mascot. These events reflect Linux's dynamic history and its profound impact on the evolution of operating systems.

The graphical interface in UNIX/Linux distributions, such as Xorg, facilitates the desktop environment similar to Windows. Xorg, akin to a server like Apache, provides graphical interface services. It relies on X clients to complete the desktop environment. Users can customize their desktop by installing different environments like XFCE or LXDE. While comparing Linux and Windows operating systems, it's essential to focus on Linux system techniques, disregarding operational disparities. This course in LabEx aims to support learners in mastering both objectives effectively.

Linux Terminal

We do not operate a Linux system directly; instead, we through a process called Shell. In the figure below, the Linux system also provides a terminal emulator called the Terminal to achieve this in a window to complete the user input and display output. Gnome-terminal, Kconsole, xterm, rxvt, kvt, nxterm, and eterm are some of the most common terminal emulators. Now, the terminal program of our Lab is the built-in XFCE-terminal of the XFCE desktop environment. However, you should note that the Terminal and the console are different.

labex:project/ $
alt text

Linux multi-user functionality is facilitated by the /dev/tty device, which corresponds to the terminal in Linux. The system provides six default virtual consoles for command line interface access, which can be accessed on a physical machine using [Ctrl] + [Alt] + [F1] ~ [F6]. In addition, [Ctrl] + [Alt] + [F7] can be used to switch back to the graphical interface.

The user experience in the graphical interface is primarily influenced by the different shells rather than the versions of the terminal emulator. The shell acts as a user interaction interface and scripting language to control the system, providing a different functionality to the Windows command line.

Popular shells in UNIX/Linux include bash (sh), zsh, ksh and csh. In the context of Ubuntu, the default shell is bash and the default desktop environment is GNOME or Unity (based on GNOME). However, the specific environment referred to uses zsh and Xfce as the default shell and desktop environment respectively.

Input, Output, and Error

In Linux, the most important thing is a command, which contains three streams (data channels): input, output, and error.

In the figure, double-click the Xfce Terminal icon on the desktop to open the Terminal, and then the system will automatically run the shell program. Finally, we can enter a command to let the system run it:

labex:project/ $ cd ~
labex:~/ $ mkdir example
labex:~/ $ touch labex.py
labex:~/ $

Input

Open the Xfce terminal, enter the command after $, and press Enter.

## Create an empty file called "file", touch is a command
touch file

## Change the current directory, cd is a command
cd /etc/

## Print the current directory
pwd

Output

The output will return the results you want. For example, if you're going to see the contents of a file, it will return the contents of the file. When you fail, you can see where it goes wrong. For most of the commands, if successful, there will be no output because Linux's philosophy is: No result is the best result.

cd ~/project
echo "print('hello, labex')" > labex.py
ls
cat labex.py

you will view the output in the terminal.

labex.py
print('hello, labex')

Error

The error stream works similarly to the output stream. The underlying difference is that the error stream of a command is used when the command does not finish a successful execution. For a command, if successful, there will be no error messages.

echo "print('hello, labex')" > labex.py
ls
cat labex.p

you will see the error message cat: labex.p: No such file or directory in the terminal.

Shortcuts

Before learning the command line, it's helpful to become familiar with a few essential tips:

  • Understanding File System Navigation: Learning how to navigate the file system using commands such as cd (change directory), ls (list directory contents), and pwd (print working directory) is crucial for effective command line use.
  • Basic Text Editing: Familiarity with basic text editors like nano or vim can be beneficial for creating and editing files directly from the command line.
  • File and Directory Management: Understanding commands for creating, copying, moving, and deleting files and directories, such as mkdir, cp, mv, and rm, is essential for managing the file system.
  • Command Syntax and Help: Being able to understand and interpret command syntax, as well as utilizing the man command to access manual pages and documentation, is important for learning about specific commands and their options.
  • File Permission and Ownership: Learning about file permissions and ownership, as well as using commands like chmod and chown, is important for modifying access rights to files and directories.
  • Basic Shell Scripting: Acquiring knowledge of simple shell scripting, including using variables, loops, and conditional statements, can help automate repetitive tasks and improve productivity.

These foundational concepts and skills can provide a solid groundwork for mastering the command line and effectively utilizing the power of Linux and other Unix-like operating systems.

Tab

Use the Tab key to make up the command. When you need to remember the full name of a command, you can only enter the beginning of its name or alias. Pressing the Tab key can help you complete the command name:

image desc

Of course, not only making up the command but also making up the directory names and the command parameters is feasible:

image desc

Ctrl + C

If the cursor on the screen is flashing but cannot continue to enter the command or constantly produces a lot of outputs you do not want, how do we solve this issue? Or if you're going to immediately stop and restore to your controllable state, what shall we do? At this time, you can use Ctrl + C to force terminating the current program. (It will not make the terminal exit.)

Try entering the following command:

tail

Then you will find that your following input won't have any reaction. Now you can use Ctrl + C to interrupt the program. You might not know what the command is now, but in the follow-up lesson, we will explain precisely the tail command.

labex:~/ $ tail
^C
labex:~/ $

Some Other Shortcuts

Shortcut Key Function
Ctrl + d Keyboard input ends or exits the terminal
Ctrl + s Pause the current program and press any key to resume it
Ctrl + z Put the current program into the background
Ctrl + a Move the cursor to the beginning of the input line, equivalent to Home
Ctrl + e Move the cursor to the end of the input line, equivalent to End
Ctrl + k Remove content from the place the cursor is currently at to the end of the line
Alt + Backspace Delete a word forward
Shift + PgUp Scroll up the terminal
Shift + PgDn Scroll down the terminal

And you can use the up arrow key () to restore the commands you used before.

Wildcards

The wildcards in the command line are expressed using symbols such as * and ?, allowing users to match strings, file names, or parameters. These wildcards are handled by the shell and are only used in the parameter values of commands, not in the command names themselves.

When a wildcard is encountered in a parameter value, the shell searches for possible matches on the disk as path or file names. If a match is found, it is replaced with the actual path or file name. If no match is found, the wildcard is passed as an ordinary character to the command, which then processes it accordingly.

Ultimately, wildcards serve as a shell implementation of path extension, allowing users to work with multiple files or directories without explicitly naming them. Once the wildcards are processed, the shell completes the reorganization of the command and proceeds with further processing until the command is executed.

First, turn to the ~/project directory use the command touch to create two files:

cd ~/project
touch adsfasd.txt wergjlkas.txt

Name the files any way you like. If you forget the file names and you want to find them, use wildcards:

ls *.txt

View the output:

labex:project/ $ ls *.txt
adsfasd.txt  wergjlkas.txt

When you are creating files, if you need to create multiple files at once, for example: "love_1_linux.txt,love_2_linux.txt,... love_10_linux.txt", you can use the following command:

touch love_{1..10}_linux.txt

Use the wildcard to list the file:

labex:project/ $ touch love_{1..10}_linux.txt
labex:project/ $ ls love*.txt
love_10_linux.txt  love_3_linux.txt  love_6_linux.txt  love_9_linux.txt
love_1_linux.txt   love_4_linux.txt  love_7_linux.txt
love_2_linux.txt   love_5_linux.txt  love_8_linux.txt

Common wildcards in Shell:

character meaning
* Match any zero or more characters
? Match any single character
[list] Match any single character of the list
[!list] Match characters other than any single character of the list
[c1-c2] match any single character in the range c1 to c2 such as [0-9], [a-z]
{string1,string2,...} Match string1 or string2 (or more) with a string
{c1..c2} Match all characters or numbers in the range c1 to c2 such as {1..10}

Get Help

In Linux, if you encounter difficulties, you can use the man command, the abbreviation of Manual pages.

Manual pages are a popular form of online software documentation for UNIX or UNIX-like operating systems, including computer programs (along with libraries and system calls), formal standards and conventions, and even abstract concepts. The user can invoke the man pages by executing the man command.

You can get a description of a command in the following way:

man <command_name>

For example, if you want to see how the man command is used, you can type:

man man

The manual script contains a lot of content involving all aspects of Linux. To facilitate the search, the man manual is divided into eight sections:

Section Description
1 General order
2 System call
3 Library functions, covering the C standard library
4 Special files (usually devices in /dev) and drivers
5 File format and convention
6 Games and screensavers
7 Others
8 System management commands and daemons

To view the contents of a particular section, add the number of the corresponding section after the man:

man 1 ls

All man pages follow a rule optimized for simple ASCII text presentations, and this does not have any form of highlighting or font control.

If you want more help, you can also use the info command. But most of the time, man is enough. If you know the function of a command and you want to see some of its specific parameters, you can use the --help parameter. Most of the commands will have this parameter, such as:

ls --help

Summary

In this lab, we learned some basic Linux commands and how to use them. We hope you have a better understanding of the Linux operating system. If you have any questions, please feel free to ask in the comments section. We are happy to help you.