A Simple Tutorial on Linux – Part-1

Learn Linux

We have started this series of tutorials for Linux which is divided into two blog posts. Each one of them will cover basic concepts with practical examples. Also, we have provided the quiz on some of the topics that you can attend for free.

In the first part of the series, we will learn the following topics in detail

  • Linux Operating System
  • Linux Files & Process
  • The Directory Structure
  • Permissions
  • Process

Introduction

Linux is a Unix like operating system. It is open source and free. We might sometimes use the word “Unix” instead of Linux.

A user can interact with Linux either using a ‘graphical interface’ or using the ‘command line interface’.

Learning to use the command line interface has a bigger learning curve than the graphical interface but the former can be used to automate very easily. Also, most of the server side work is generally done using the command line interface.

Linux Operating System

The operating system is made of three parts:

1. The Programs

A user executes programs. AngryBird is a program that gets executed by the kernel, for example. When a program is launched, it creates processes. Program or process will be used interchangeably.

2. The Kernel

The Kernel handles the main work of an operating system:

  • Allocates time & memory to programs
  • Handles File System
  • Responds to various Calls

3. The Shell

A user interacts with the Kernel via the Shell. The console as opened in the previous slide is the shell. A user writes instructions in the shell to execute commands. Shell is also a program that keeps asking you to type the name of other programs to run.

Linux Files & Processes

Everything in Unix is either a file or a process.

Process

When you run a program, a process is created. Every process is identified by a number called process ID. To check the processes you are running, execute “ps” command on the shell. You can think of the process ID to be a sequence number given by the operating system. It may be different at a different execution of the same program.

File

A file is a sequence of data. A file could be created by users using word processors or text editors or by the program to keep the information. A program is kept in the form of a file and when it is run by the kernel, it loads as a process.

A file is generally written on the disk so that it exists even after the computer restarts. It is saved in a disk – either hard disk drive (HDD – cheaper and slower) or solid state drive (SSD – faster but costlier).

A file is identified by a name called file path. In Unix, everything is represented as file:

  1. Devices such as Mouse, Keyboard
  2. Programs are saved as file
  3. Disk and Monitor

Attend the quiz on Process

The Directory Structure

A file is kept inside a directory. A directory can have a file or another directory inside it. It is like an inverted tree.

The top-level directory is “/” called root. “/” directory does not have a parent. /A/B means B is inside A which is inside top-level directory “root”.

List Files and Directory

To see the list of files use the command:

ls

Relative & Absolute Paths – Change the Directory

There are two ways to represent a file/directory path:

1. Absolute

This way of representing a file/directory is independent of current directory of the user. Such paths start with “/”. Examples: /etc/passwd.

2. Relative

Relative to the current working directory. Examples: ../../etc/passwd

You can change directory using “cd” command. Every user is given a separate home directory.

Home Directory & Current Directory

Inside the console, you are always in the directory. On login, by default, you land in your home directory.

To see the present working directory use: pwd

To change the directory to your home directory use only “cd command without any arguments.

Attend the quiz on directory

Create Directory

You can create a directory using mkdir command in the following way:

mkdir directoryname

Delete a file

To delete a file you can use ‘rm‘ command. To delete the directory you have to have “-r” switch with rm command.

rm -r directoryname

Create File

A file is a sequence of bytes and represents data. It is found in a directory. A file could contain any kind of data: an executable program, data representing movies, music, pictures or plain text.

You can create an empty file using the touch command. Please note that the extension of the file doesn’t matter much in Unix.

touch myemptyfile.txt

Create Text File Using Nano

You can use a text editor to edit or create a text file. There are many editors in Linux such as nano, pico, vi, emacs. Let’s try to use nano for creating a file

1. Launch nano to edit:

nano myfirstfile.txt

2. Type the following text into it:

He walked into his exile

3. Press “Control+x” to save and then confirm by typing y

Copy File

To copy files and directories we use the cp command. Following is the way to make a copy of a file.

cp myfirstfile.txt myfirstfile_copy.txt

Move Files & Directories

mv command is used to move or rename files and directories.

rename myfirstfile.txt to firstfile.txt

mv myfirstfile.txt firstfile.txt

Seeing inside the File – cat, tail, head

To see what is inside a text file, you can use either cat, tail, or head.

Using cat you can see the whole content of the file:

cat myfirstfile_copy.txt

Do not use this command to look inside a huge file. For the huge file, you can use the “more” command which would display the content of a file in a paginated way:

more myfirstfile_copy.txt

tail shows you the last few lines of a file

tail myfirstfile_copy.txt

By default tail shows you only last 10 lines, you can change it using command line option. For example, to see last 20 lines, you can use

tail -20 myfirstfile_copy.txt

If you want to chase a file – continuously print newlines appended to the file – use the following command:

tail -f myfirstfile_copy.txt

If you are interested in the first few lines, you can use head.  By default head shows you only first 10 lines, you can change it using command line option. For example, to see first 20 lines, you can use

head -20 myfirstfile_copy.txt

Attend the quiz on commands

Use find command

Sometimes you might want to search for a particular file based on various attributes of a file such as size or name etc. The find command comes in very handy for such use cases.

For example, to find all the text files in current directory, you can use this command

find . -name '*.txt'

Use grep command

If you want to locate a word in files, you can use the grep command. Grep lists all the lines from files in which a particular word exists. Examples of grep

grep myword file1 file2

If you want to search in files recursively – inside every subdirectory of a directory, use the following command

grep -r myword directory

If you want to search case insensitive, use “-i” switch

grep -i myword file1 file2
Practical Use Cases

grep is a very powerful tool. It can somewhat behave like where clause in sql queries.

Few Examples are:

  1. On a web server you could filter only the errors from a log file
  2. Say you have a directory containing the temperature of various cities and you are looking for temperatures of the city having name as nyc, you could easily do: grep nyx tempdirectory/*

Use wc command

To find the number of characters, words, and lines, use the wc command.

If you are only looking for the number of lines you could use:

wc -l

Attend the quiz on wc command

Permissions – Overview

Permissions in unix are integral part of the operating system.

You can see the details of a file using ls -la command:

ls -la myfile

And you can see the details of all files in the current directory by using:

ls -la

Every file has an owner (also referred as a user), a group and permissions. A user can be part of many groups. A group can have many users.

The permission attributes of a file signify who can do what.

 

Using chmod To Change

You can change the permissions of a file using chmod command: chmod permission_cmd myfile

You can allow or disallow the user (u), group (g) or other(o) the following actions: read (r), write (w) and execute (x).

So, if you want to allow the user (the person who owns it) to execute the file:

chmod u+x myfile

And to disallow the user (the person who owns it) to execute the file:

chmod u-x myfile

Say you want to give the rw permissions to owner and group of a file, you will have to use the following command:

chmod u+r,u+w,g+r,g+w myfile

or

chmod u+rw,g+rw myfile

To give members of a group ability to modify a file, use:

chmod g+w myfile

Assignment – Owner can change, others can only read

Create a new file called myemptyfile. You can use:

touch myemptyfile

Check its permissions using:

ls -l myemptyfile

Using chmod, give it permissions such that owner of a file should be able to modify. Everyone else should be able to only read.

Hint: You can use the following command to remove the write permissions from group and others:

chmod o-w,g-w myemptyfile

Assignment – Everyone can execute

Create a new file called myemptyfile. You can use:

touch myemptyfile

Check its permissions using:

ls -l myemptyfile

Using chmod, give it permissions such that the owner of a file should be able to modify. Everyone should be able to execute but no one can modify the file.

Assignment – Nobody other than owner can read the file

Create a new file called myemptyfile. You can use:

touch myemptyfile

Check its permissions using:

ls myemptyfile

Using chmod, give it permissions such that nobody other than the owner of a file can read the file.

Assignment – Only Group Members can read

Create a new file called myemptyfile. You can use:

touch myemptyfile

Check its permissions using:

ls myemptyfile

Using chmod, give it permissions such that owner of a file should be able to read and modify, members of groups can only read, others can’t read or modify.

Permissions – Numeric

You can also use numbers to represent the permissions.

So, for a file with permissions rwxr-xr-x, the owner has rwx permission which 4+2+1=7 and group and others have r-x which means 4+0+1 = 5. Thus the command you would use to set such permission would be: chmod 755 myfile.

Attend the quiz on permission – numeric

Permissions – Advanced

There is a special user called root on Unix systems which has all the privileges which can give permission to any user on any file.

Some users who are allowed to act on behalf of the root are called sudoers. This list of sudoers can be edited using the sudoedit command. Such users are allowed to run commands as if they are root using: sudo .

You can change the owner of a file using chown and change the group of a file using the chgrp command. Please note that changing the owner of the file is possible only with the administrative account.

Attend the quiz on permissions – advanced

Process

Every program or command is a sequence of instructions stored as a file. You run it on the shell or by any other means such as double-clicking it from the user interface.

When it is run, the content of the program is read from a file and loaded into the memory and then instructions are executed by the operating system.

All the commands that we have been using ls, cat were programs.

Attend the quiz on process

Find information about processes

While a program is running, it is called a process. A process is uniquely identified by PID – process id. To find out the information about all the processes, you can use the following commands: pstree, top, ps.

ps lists the processes of the system. Without any arguments, it lists only your processes. To see all of the processes by all users, run

ps aux

The first column mentions the user id who has started the process.

To continuously monitor all processes, use “top” command. This command is usually used to monitor which processes are running, taking up most CPU or Memory.

To learn more about command use man command name. So, please go through manual using command

man ps

man top

Background Processes

So far whenever we ran a command, if it is taking time, we had to wait till it finishes before we could type another command. If a process is taking time and does not require input from you, you would like to run it in the background.

To run a program in the background, put a ‘&’ at the end of the command:

mycmd &

Send a process in background

There is a command called sleep which can make you wait for specified seconds. Please send sleep command in background using:

  sleep 1000 &

To kill any running process, please use the command:

kill processid

Basically, “kill” lets you send a signal to the running process. For example, to send terminate signal, you can use

kill -9 processid

More – Interacting with processes

To send a running process to background, first press ‘Ctrl+z’ to suspend it and then type bg to send the suspended process to background.

When you exit the shell or disconnect, the processes you were running get killed. To keep a process running in the background even if the shell has been disconnected, use

nohup

or

screen

Process hierarchy

A process (parent) can execute another process (child). When a parent process is killed all the child processes are automatically killed. The main system process that is started first is numbered as 1. If this process is killed the system will shut down.

To see the tree of processes, you can use the command

pstree

A user can kill only the processes that the user has executed not the processes of other users. Only root can kill the processes of other users.

Attend the quiz on process hierarchy

In the second part of the Linux series, we will learn how to write a shell script followed by other important concepts and commands.