You are on page 1of 11

Navigating through the system at the command line can get confusing at times, especially

when a directory name occurs in several places. Fortunately, Linux includes a simple com-
mand that tells you exactly where you are in the file system. It’s easy to remember that
this command is pwd because it is an abbreviation of print working directory. You simply
type pwd at any point to get the full path of your location. For example, typing pwd after
following the preceding instructions shows /home/yourusername, meaning that you are
currently in your home directory.
Using the pwd command can save you a lot of frustration when you have changed directory
half a dozen times and have lost track of where you are.

A linux file is in the form of a hierarchy.


DIRECTORIES
/ - it is the root directory
/bin – essential commands
/boot – bootloader files
/dev – device files
/etc – system configuration files
/home – user home directories
/lib – shared libraries, kernel modules
/lost+found – recovered files (if found after a file system check)
/media – mount point for removable media such as DVDs, floppy disk etc.
/mnt – usual mount point for local, remote file systems, file systems that are additional to the standard such as dvd rom or another hdd
/opt – add-on software packages
/proc – kernel information, process control
/root – superuser (root) home
/sbin – system commands (mostly root only)
/sys – real-time information on devices used by the kernel
/tnp – temporary files
/usr – software not essential for system operation such as applications
/var – variable files relating to services that run on a system, but whose content are expected to change regularly during normal operation

Creating users
A user is anyone who uses or operates the system.
There are two ways to create a user
1. Adduser 2. Useradd
With user add, you’ll have to create the directories that come with it. It is usually used by experts to customize the system.
Sudo adduser newusername

How to create a group


We create groups for security reasons, to prevent certain people from having access to certain files.
Addgroup
Every user is given a user ID. The same goes for groups. IDs are assigned to make everything unique. It helps the system to differentiate between
similar names. How do we find these IDs. The configuration files contain the user IDs. The name of the specific file that stores that kind of information
is /etc/Password
1. cd etc – we want to retrieve a file from etc (system configuration files)so we use cd
2. You should use the command cat etc to output the content of the path passwd which contains the user ID
We can also change user ID using the command usermod -u newid username
(modifying the user ID)

PERMISSIONS
There are three permissions we give to files and directories.
1. Read – being able to see the content of the file. R 4
2. Write – being able to modify content of the file. W 2
3. Execute – could contain a program setup or link which you can execute. X 1
4. Read and write – 6
5. Read and execute – 5
6. Write and execute – 3
7. Read, write and execute – 7

Another way to display permissions is ls -id name of file

There are three categories of people that we can give the permissions to
1. owner - u 2. group - g 3. others - o
The command that can give us detailed information about a specific file, directory or the command – (ls -l)
To give the owner permission to execute,
u+x
u owner
+ to add a permissions
x execute permissions

assigning permissions using pneumonic code


mkdir
ls -ld

The only can only read, the group can only read and execute and the others can
mmanuella@emmanuella-HP-EliteBook-840-G2:~$ touch emmanuella
emmanuella@emmanuella-HP-EliteBook-840-G2:~$ ls -ld emmanuella
-rw-rw-r-- 1 emmanuella emmanuella 0 K-Ɔ 16 08:44 emmanuella
emmanuella@emmanuella-HP-EliteBook-840-G2:~$ chmod 775 emmanuella
emmanuella@emmanuella-HP-EliteBook-840-G2:~$ ls -ld
drwxr-x--- 18 emmanuella emmanuella 4096 K-Ɔ 16 08:44 .
emmanuella@emmanuella-HP-EliteBook-840-G2:~$ ls -ld emmanuella
-rwxrwxr-x 1 emmanuella emmanuella 0 K-Ɔ 16 08:44 emmanuella
emmanuella@emmanuella-HP-EliteBook-840-G2:~$ chmod 555 emmanuellaayerh
emmanuella@emmanuella-HP-EliteBook-840-G2:~$ ls -ld emmanuella
-rwxrwxr-x 1 emmanuella emmanuella 0 K-Ɔ 16 08:44 emmanuella
emmanuella@emmanuella-HP-EliteBook-840-G2:~$ chmod 555 emmanuella
emmanuella@emmanuella-HP-EliteBook-840-G2:~$ ls -ld emmanuella
-r-xr-xr-x 1 emmanuella emmanuella 0 K-Ɔ 16 08:44 emmanuella
emmanuella@emmanuella-HP-EliteBook-840-G2:~$ umask 002 emmanuella
emmanuella@emmanuella-HP-EliteBook-840-G2:~$ ls -ld emmanuella
-r-xr-xr-x 1 emmanuella emmanuella 0 K-Ɔ 16 08:44 emmanuella
emmanuella@emmanuella-HP-EliteBook-840-G2:~$ umask
0002
emmanuella@emmanuella-HP-EliteBook-840-G2:~$ umask 022
emmanuella@emmanuella-HP-EliteBook-840-G2:~$ umask
0022

You cannot delete a primary group unless you delete the secondary groups
emmanuella@emmanuella-HP-EliteBook-840-G2:~$ sudo addgroup computerscience
[sudo] password for emmanuella:
Adding group `computerscience' (GID 1004) ...
Done.
emmanuella@emmanuella-HP-EliteBook-840-G2:~$ usermod -G computerscience
Usage: usermod [options] LOGIN
change group – chgrp ; you can use this command to change a group to which a file belongs
chown – to change the owner of a file.

Adding A user to a group


emmanuella@emmanuella-HP-EliteBook-840-G2:~$ usermod G computerscience emmanuella
Usage: usermod [options] LOGIN

HOME SYSTEM_VERSION kofi visitor

Don’t put spaces on either sign of the equal sign when assigning a variable. X = 10 is wrong. It will be seen as a command and not a variable.
Variables are case sensitive.
You can define a null variable as follows. eg. boss=””
Do not use question mark, asterisk and other symbols to name your variables. A wild tag
How do you print and accesds a command. What is the echo used for – for printing a variable or value etc
What is the difference between echo x and echo $x? The dollar sign prints what is in the variable. If not it will just print x.

Operators

Writing our first shellscript


cat > MyFirst
“I love Ubuntu Operating System”

To display, ./MyFirst
The set command is used to access system variables.

Introduction to kernel
The kernel is the HEART of the linux operating system. Without it, the operating system is useless. It manages the resources of the operating system,
eg. the facilities to store data, printing, memory allocation, file management etc. It is the kindle that decides who will use a particular resource, how
long it will be used and when access will be given to use that resource. The uses of the kernel are summarized into five functions.
1. Input-output management
2. Process management
3. Device management
4, File management
5. Memory management

INTRODUCTION TO SHELL SCRIPTING


The shell accepts our instructions in human readable form and translates it into computer binary language. An environment is provided for human
interaction.
Shell is an INTERPRETER (Command Language Interpretor) that executes commands read from the standard input device (keyboard) or from a a file.
Linux uses many shells depending on which linux version you are using.
1. BASH (Bourne-Again Shell)
2. CSH (C Shell) – It originated form the University of California. Its programming syntax is similar to that of the c language hence the name
CSHELL.
3. KSH (Kourne Shell)
Bash is the most common shell in Linux. It is a freeware (it is free to download, but we can’t view the source code. It maintains its copyright). It
originated from the AT&T Bell Labs where UNIX originated from.
Any of these read commands from the mouse or keyboard, will tell the operating system what the user wants and then the operating system will run
those commands and give output to the user.
If you are giving commands from the keyboard, it is called Command Line Interface (hence the dollar sign).
To find our shell type you are using, you use the ECHO command. It is something like print to screeen or output. You can also use cat to display
content, but echo prints exactly your content.

From the above picture we are using the Bourne Again Shell (bash).
How do we use this shell?
You start using the shell as long as soon login to your system. It was translating our commands into computer readable form.
There is a command that will tell us more apart from the man command.
So, you type the command name , space, double hyphen and help. COMMAND_NAME –help

Since it is too much, we can add a command MORE so that we can view it line by line.

Another one is info but it come before the command. Info will not display in the same screen of the same terminal. You can press q to quit.

PROCESSES
A process is any kind of program or task carried out by your computer.
1. ls -IR
It requests to name files in the directory and all subdirectory based on your current directory.
IN LINUX immediately you start a process, it is assigned a process ID (PID) It starts from 0 to 65535.
So why is a process required?
Ubuntu Linux is a multi-user and multitasking operating system. You can run two or more processes simultaneously and two or more users can use it at
the same time.

LINUX COMMANDS RELATED TO PROCESSES


To see currently running process, ps.

To stop any process, we use the command kill. You can use the name of the process or the process ID.

Kill is used for stopping processes that are running in the background.
To get information about all running processes, use ps -ag. It lists processes and their description.
To stop all processes except your shell, use kill 0.
Note that you can only kill processes that you created yourself not those created by the kernel unless you are an administrator or a sudo user where you
can kill some processes.

REDIRECTION OF STANDARD OUTPUT/INPUT OR INPUT-OUTPUT REDIRECTION


There are three REDIRECTOR SYMBOLS for sending output to the screen. (output/input or input/output)
1. > 2. < 3. >>

1. >
Syntax - linux_command > filename
To output linux command results to file, command > filename.

If there is already content in it, it will override it.


You can use nano, cat command to type something into a file.

If you want to append the file and not override it, ls >> file.
The less than sign is used to receive input.

PIPES
A pipe is a a way to connect the output of one program to the input of another program without any temporary file. Redirectors output the commands in
a file but in this case, we have two processes and the output of one will be the input of the other one.
A pipe is nothing but a temporary storage place where the output of one command is stored and then passed as the input for the second command.
Syntax
command1 | command2
Output of the ls –help will be given as input to the more command. The ls --help information is to be shown MORE.

The who command shows the user who is logged in at the moment.

Piping who into sort will print the sorted list of users.

The wc command shows us the number of users who are logged into the system.
The output of the who command is given as input to the wc command so that it will display the number of users who are logged on to the system.

This will show the number of files or directories in the current directory.

Grep command is used to search for a command like a filter tool. If the user is logged on at the moment the name of the user will show else nothing
will happen. NOTE The space is very important.

FILTER
If a linux command accepts its input from a standard input and produces its output on a standard output, it is known as a filter.
Standard input is any approved way of giving an input to a system and the results from them is standard output. A filter performs some kind of
processes on the input and produces an output that the user wants to see out of the lot.
The filter performs processes on the input and gives an output. It created the file called example which was not in existence and put the command in
there.

INTRODUCTION TO SHELL PROGRAMMING


Shell program is a series of linux commands (all the stuff we’ve been learning from day one). Shellscript is just like batch file in ms box? Shellscript
takes input from the user file and outputs them on the screen. It is useful to create our own commands that can save us a lot of time and to automate our
own commands. Shell is an interpreter that executes commands read from the standard input device (keyboard) or from a file.

VARIABLES IN LINUX
Sometimes to process our data or information, it must be kept in the computer’s RAM. Ram is divided into small locations and each location is having
a unique number called MEMORY LOCATION or MEMORY ADDRESS, which is used to hold our data. A programmer can give a unique name to
this memory location or address called memory variables or VARIABLES. There are two types of variables
1. System variables
These are variables that are created by the system – linux itself. They are defined in CAPITAL LETTERS.
Examples
You can view all the system variables with the command SET. To find the type of shell that you are using.

2. User Defined Variables


They are created and maintained by the users. They are in LOWER LETTERS. But you can define a variable and include capital letters.
If you want to declare a variable, you need the variable name and whatever you have to assign to the variable. eg. x=15

RULES FOR NAMING VARIABLES – BOTH UDV AND SYSTEM VARIABLES.


1. Variable name must begin with alphanumeric character or underscore character followed by one or more alphanumeric character(s). eg. Valid shell
variables are as follows. HOME, SYSTEM_VERSION, kofi, visitor, _account.
2. Don’t put spaces on either side of the equal sign when assigning values to a variable. eg. x = 10 COMMAND CANNOT BE FOUND
3. Variables are case sensitive. NO=10 is different from nO=10
4. You can define a null variable as follows. eg. alberta= “ ”
5. Do not use symbols to name your variables.

The files starting with a dot are hidden files which usually contain system configuration files. A cron job is a command in linux which schedules a
command to be executed in the future.

ls -al include details such as the file/directory permissions, owner and group, size, and the date and time it was last modified.

emmanuella@emmanuella-HP-EliteBook-840-G2:~$ cd Desktop
emmanuella@emmanuella-HP-EliteBook-840-G2:~/Desktop$ pwd
/home/emmanuella/Desktop
emmanuella@emmanuella-HP-EliteBook-840-G2:~/Desktop$ cd ..
emmanuella@emmanuella-HP-EliteBook-840-G2:~$ pwd
/home/emmanuella
To change the group of a file or directory, use chmod newgrp filename.

emmanuella@emmanuella-HP-EliteBook-840-G2:~$ touch Downloads/afileindownloads


emmanuella@emmanuella-HP-EliteBook-840-G2:~$ ## that was for creating a file in a new location other than your current working directory
emmanuella@emmanuella-HP-EliteBook-840-G2:~$

rm-r is used to delete a file along with its contents.


Less is used to display contents of files that are way too much to be read in just a page.
Word count ig
wc -m MySecond can be used to get a more accurate value for the bytes.
BASIC COMMANDS

▶ cat—Prints the contents of a file


▶ cd—Changes directories
▶ chmod—Changes file access permissions
▶ cp—Copies files
▶ du—Prints disk usage
▶ emacs—Edits text files
▶ find—Finds files by searching
▶ grep—Searches for a string in input or files
▶ head—Prints the first lines of a file
▶ less—Displays files or input interactively
▶ ln—Creates links between files
▶ locate—Finds files from an index
▶ ls—Lists files in the current directory

Working from a text-only system is useful both for working locally with a broken GUI and
for connecting to a remote, text-only system. If your Linux server is experiencing prob-
lems, the last thing you want to do is load it down with a GUI connection; working in
text mode is faster and more efficient.

▶ make—Compiles and installs programs


▶ man—Displays manual pages for reading
▶ mkdir—Makes directories
▶ mv—Moves files
▶ nano—Edits text files
▶ rm—Deletes files and directories
▶ sort—Takes a text file as input and outputs the contents of the file in the order you
specify
▶ ssh—Connects to other machines using a secure shell connection
▶ tail—Prints the last lines of a file
▶ vim—Edits text files
▶ which—Prints the location of a command

Many other commands are also used fairly often—cut, diff, gzip, history, ping, su, tar,
uptime, who, and so on—but if you can understand the ones listed here, you have suffi-
cient skill to concoct your own command combinations.

The most popular parameters for customizing the output of ls are as follows:
▶ -a—Includes hidden files
▶ -h—Uses human-readable sizes
▶ -l (lowercase L)—Enables long listing
▶ -r—Reverse order
▶ -R—Recursively lists directories
▶ -s—Shows sizes
▶ --sort—Sorts the listing

▶ Ctrl+U—Erases the entire line


▶ Ctrl+W—Erases word by word
▶ Left and right arrow keys—Move along the line to where the error is
▶ Ctrl+A—Moves the cursor to the beginning of the line
▶ Ctrl+E—Moves the cursor to the end of the line
▶ Ctrl+K—Erases everything to the right of the cursor’s position
▶ Ctrl+Y—Restores something you just deleted but shouldn’t have

You might also like