Diferencia entre revisiones de «Basic Administration»

De Wiki de Sistemas Operativos
Saltar a: navegación, buscar
(initial)
Línea 336: Línea 336:
  
 
= Step 2: Screen print and output redirection =.
 
= Step 2: Screen print and output redirection =.
 +
 +
== echo ==
 +
 +
It is used to print a line of text on the screen or environment variables. It also allows us to write to files.
 +
 +
<syntaxhighlight lang="bash">
 +
echo <option> <text>
 +
</syntaxhighlight>
 +
 +
* Curious options:
 +
 +
-e <PALABRA \bPALABRA> Spaces between words are removed.
 +
 +
-e <WORD \bWORD> Line breaks are added.
 +
 +
 +
An environment variable is an edited variable with some value, by default there are some created, such as the HOME variable, which contains the user's directory:
 +
 +
<syntaxhighlight lang="bash">
 +
echo $HOME
 +
</syntaxhighlight>
 +
 +
We will take advantage and create a variable ourselves:
 +
 +
<syntaxhighlight lang="bash">
 +
ME="my name"
 +
</syntaxhighlight>
 +
 +
We check:
 +
 +
<syntaxhighlight lang="bash">
 +
echo $ME
 +
</syntaxhighlight>
 +
 +
== Output redirection ==
 +
 +
> and >> is used to redirect an output. For example, using the command echo,
 +
we can send a text inside a file:
 +
 +
<syntaxhighlight lang="bash">
 +
echo "This is a test" > file.txt
 +
</syntaxhighlight>
 +
 +
The difference between > and >> is that > overwrites whatever is in the file and
 +
adds the content, and >> does not overwrite, it just adds the new content.
 +
 +
Let's do the test:
 +
 +
<syntaxhighlight lang="bash">
 +
echo "This is a test" > file.txt
 +
echo "This is a test2" > file.txt
 +
echo "This is a test" >> file2.txt
 +
echo "This is a test2" >> file2.txt
 +
</syntaxhighlight>
 +
 +
In any case, if the file does not exist, it is created.
 +
 +
= Step 3: Advanced file and folder handling =
 +
 +
== cat ==
 +
 +
* Displays the contents of a file:
 +
 +
<syntaxhighlight lang="bash">
 +
cat file.txt
 +
cat <file1> <file2>
 +
</syntaxhighlight>
 +
 +
* Creates a file:
 +
 +
<syntaxhighlight lang="bash">
 +
cat ><filename>
 +
</syntaxhighlight>
 +
 +
SAVE FILE: Press ENTER and then CTRL + D
 +
 +
* Add more lines to the file:
 +
 +
<syntaxhighlight lang="bash">
 +
cat >><filename>
 +
</syntaxhighlight>
 +
 +
* Concatenate files:
 +
 +
<syntaxhighlight lang="bash">
 +
cat <file1> <file2> > <file3> <file3>
 +
</syntaxhighlight>
 +
 +
* Edit files:
 +
 +
<syntaxhighlight lang="bash">
 +
cat
 +
 +
hello
 +
what's up
 +
 +
cat > <file4>
 +
</syntaxhighlight>
 +
 +
== grep ==
 +
 +
Used to locate matches of a certain pattern in files and displays the match, if found, on screen.
 +
 +
<syntaxhighlight lang="bash">
 +
grep <option> <pattern> <files to search for>.
 +
</syntaxhighlight>
 +
For example, to search for the pattern ''test'' in the file ''file.txt''.
 +
 +
<syntaxhighlight lang="bash">
 +
$ grep -n test file.txt
 +
1:This is a test
 +
</syntaxhighlight>
 +
 +
* Main command options:
 +
 +
-c : Writes the number of lines found.
 +
 +
-i : Is case insensitive.
 +
 +
-l : Displays the names of the files containing the searched characters.
 +
 +
-n : Each line is preceded by its number in the file.
 +
 +
-s : Messages indicating that a file cannot be opened are not displayed.
 +
 +
-v : Displays only lines that do not meet the condition.
 +
 +
== wc ==
 +
 +
Used to count the number of words, characters, lines or bytes contained in a file.
 +
 +
The most commonly used options are:
 +
 +
* -l: displays the number of lines contained in the file.
 +
* w: shows the number of words.
 +
-m: shows the number of characters
 +
-c: shows the number of bytes.
 +
 +
Let's try and count everything in the file file2.txt:
 +
 +
<syntaxhighlight lang="bash">
 +
wc -l file2.txt
 +
wc -w file2.txt
 +
wc -m file2.txt
 +
wc -c file2.txt
 +
</syntaxhighlight>
 +
 +
We can count more than one file at a time:
 +
 +
<syntaxhighlight lang="bash">
 +
wc -l file.txt file2.txt
 +
</syntaxhighlight>
 +
 +
It will show us the count of each file and the total.

Revisión del 15:55 2 nov 2021

To perform this practice we will use one of the virtual machines that we generated in the previous practice.

In this practice we will learn how to use the command interpreter (also known as command line) and to learn basic notions of Linux system administration.

Step 0: Brief introduction to the Linux system == Basic structure of the file system

Basic structure of the file system

In a Linux system, all folders and files in the file system hang from the root folder which is represented by the /.

/
├── bin.
├── usr
│ ├─── local.
│ ├─── bin
│ └── ...
├── dev.
│ ├─── sda
│ ├─── sda1
│ └── ...
├─── home.
│ ├─── practice.
│ │ ├─── topic1.pdf
│ │ ├─── bulletin1.pdf.
│ │ └── ...
│ ├─── professor.
│ └── ...
└─── etc.
    ├─── firefox
    ├─── libvirt
    ├── ...
    └── ...

As you can see, the file system employs a tree structure.

Basic notions

-Applications with compound names: use hyphen between words.

-Names with blank spaces: write between double quotes.

-Blank spaces to separate commands (e.g.: install several packages )

General format of a command: command [-options] [arguments] -'important''''

-IMPORTANT': Difference between uppercase and lowercase.

Folders . and .

Every folder on a Linux system has two pseudofolders, the ''''' and the '''''':

  • The pseudofolder ''''''' refers to the parent folder that contains this folder.
  • The pseudofolder '''''''' refers to the current folder, it is therefore a self-reference.

In the case of the root folder, the pseudofolder '.' and '''' refer to the root folder itself, it is therefore an exception.

Current working directory and the command cd

Each command interpreter has a current working directory ('c'urrent working 'directory). The current working folder can be modified with the command cd (change directory, in English).

For example:

$ cd ..

It would place us in the parent folder, using a relative path.

If we retype from /home " cd .. " takes us to /$

To place us in the root, we can use an absolute path:

$ cd /

In case we want to get back to the user folder, it would be enough to invoke cd' without further or ' cd ~

$ cd

'VERY IMPORTANT': cd.. is not the same as cd ... ', the space after the cd is required.

Absolute paths, relative paths

Absolute or relative paths are used to refer to a file or folder.

Absolute paths always start with /, so they take the root folder (/) as a reference point. For example, an absolute path to the test folder that is stored in the user's ubuntu' folder is '/home/ubuntu/test/.

The relative path takes the current working folder as reference. To find out the current working folder we have the command pwd'.

$ pwd
/home/ubuntu

Based on the current working folder, using the pseudofolder ''''''', relative paths can be constructed. For example, to reference a file 'file.txt in the temporary folder, we can use the relative path ../../tmp/file.txt'.

Although it is probably more convenient to use an absolute path in this case, which would be /tmp/file.txt'.

Command interpreter

The shell is a textual application launcher that uses the keyboard as an input device. Using the keyboard, you type the name of the program you want to launch and when you press the enter key, the shell executes the program. By default, the command interpreter used by Ubuntu is bash'.

man: see the man page of a command

The man' command will be useful for viewing man pages.

Its basic structure is:

man <command>

Although it is also used with the option '' -a ' of All, which also displays entries beginning with '''':

man -a <command>

This program takes as input the name of the command you want to query its man page, for example:

man ls

It gives us man page information for the ls' command.

To exit the manual page, press the q' key (the first letter of the word quit', in English).

quit = exit

Usually, programs offer help options, such as --help'.

man --help

The compact version can also be used when specifying options:

man -h

In general, all commands usually offer a helper option.

Step 1: Basic operations with the file system

ls

Lists the files and folders. If nothing is specified, displays the files and folders contained in the current working folder.

ls

If a path is specified, displays the folders and files contained in that path, for example:

$ ls /
bin boot data dev etc home initrd.img initrd.img.old lib lib64 lost+found media mnt opt proc root run sbin snap srv sys tmp usr var vmlinuz vmlinuz.old

When using / as the absolute path, it displays the contents of the root folder.

The most commonly used options of this program are usually:

  • -a: shows hidden files and folders. In Linux, any file or folder whose name starts with . is considered hidden. This also includes the pseudofolders . and . in the list.
  • -l: displays in list and provides data such as last modified date, owner, group, size in bytes and name:
$ ls -a

. ..  .bash_history .bash_logout .bashrc .bashrc .profile .ssh

Both options can be combined:

$ ls -la /

total 92
drwxr-xr-x 24 root root root 4096 Aug 28 09:50 .
drwxr-xr-x 24 root root root 4096 Aug 28 09:50 ...
drwxr-xr-xr-x 2 root root root 4096 Aug 22 12:37 bin
drwxr-xr-x 4 root root root 4096 Aug 22 12:37 boot
drwx------ 3 root root 4096 Aug 28 09:50 data
drwxr-xr-x 18 root root root 3780 Aug 28 09:46 dev
[...]

Other useful options are:

  • -s : shows the size in blocks of each file.
  • -t : brings the day and time of modification
  • -R : lists also the subfolders
  • --color : shows the content colored


The permissions' are used on the basis of three capabilities:

  • Read, which is coded with r (read).
  • Writing, which is coded with w' (write). (write).
  • Execution, which is coded with x (execute). (execute).

The first column shows the permissions that encode:

  • Type of entry: d'' indicates directory (folder).
  • Permissions of the owner, first third of rwx
  • Group permissions, second string of rwx
  • Permissions for the rest of the world, third string of rwx.

Next, the owner and group are displayed, followed by the last modification date and the name of the file or folder.

The help in ls only works with ls --help not with ls -h

mkdir

Creates a new directory.

$ mkdir test

Creates a test folder in the current directory.

Let's check that it has been created properly:

$ ls
test

With the -p' option, it allows to create successive nested folders at once, for example:

$ mkdir -p a/b/c

Which creates the following folder structure.

a
└── b
    └── c

touch

Creates a new empty file (no contents) with the name we specify.

$ touch file.txt

We check that the file has been created correctly.

$ ls
file.txt

cp

Copies a file or directory.

$ cp file.txt file_copy.txt

To copy a folder and its contents we have to use the -r (recursive) option.

$ cp -r test test_copy
  • We check the result with the ls command.
  • If we are in the destination folder, the structure would be as follows:
$ cp <source path name> <file name>

mv

Move file or folder.

mv file_copy.txt file2.txt

It is also used for renaming.

mv test_copy test_copy2

rm

  • Deletes an empty file or folder:
rm <name>
  • To delete a non-empty folder and its contents:
rm -r <name>

IMPORTANT': Never do ' rm -r *

  • EXERCISES:
  1. Create the following structure within the /tmp directory (use the mkdir, touch and cp commands):
/tmp
├── folder1.
├── folder2
│ ├─── folder1.txt
│ └└── file2.md
└── folder3.
    ├── file1.txt
    └─── file2.md
  1. Based on the previous exercise, change the structure to the following (use mv and rm commands):
/tmp
└── folder.
    ├─── file1.txt
    └─── file2.md

= Step 2: Screen print and output redirection =.

echo

It is used to print a line of text on the screen or environment variables. It also allows us to write to files.

echo <option> <text>
  • Curious options:

-e <PALABRA \bPALABRA> Spaces between words are removed.

-e <WORD \bWORD> Line breaks are added.


An environment variable is an edited variable with some value, by default there are some created, such as the HOME variable, which contains the user's directory:

echo $HOME

We will take advantage and create a variable ourselves:

ME="my name"

We check:

echo $ME

Output redirection

> and >> is used to redirect an output. For example, using the command echo, we can send a text inside a file:

echo "This is a test" > file.txt

The difference between > and >> is that > overwrites whatever is in the file and adds the content, and >> does not overwrite, it just adds the new content.

Let's do the test:

echo "This is a test" > file.txt
echo "This is a test2" > file.txt
echo "This is a test" >> file2.txt
echo "This is a test2" >> file2.txt

In any case, if the file does not exist, it is created.

Step 3: Advanced file and folder handling

cat

  • Displays the contents of a file:
cat file.txt
cat <file1> <file2>
  • Creates a file:
cat ><filename>

SAVE FILE: Press ENTER and then CTRL + D

  • Add more lines to the file:
cat >><filename>
  • Concatenate files:
cat <file1> <file2> > <file3> <file3>
  • Edit files:
cat

hello
what's up

cat > <file4>

grep

Used to locate matches of a certain pattern in files and displays the match, if found, on screen.

grep <option> <pattern> <files to search for>.

For example, to search for the pattern test in the file file.txt.

$ grep -n test file.txt
1:This is a test
  • Main command options:

-c : Writes the number of lines found.

-i : Is case insensitive.

-l : Displays the names of the files containing the searched characters.

-n : Each line is preceded by its number in the file.

-s : Messages indicating that a file cannot be opened are not displayed.

-v : Displays only lines that do not meet the condition.

wc

Used to count the number of words, characters, lines or bytes contained in a file.

The most commonly used options are:

  • -l: displays the number of lines contained in the file.
  • w: shows the number of words.

-m: shows the number of characters -c: shows the number of bytes.

Let's try and count everything in the file file2.txt:

wc -l file2.txt
wc -w file2.txt
wc -m file2.txt
wc -c file2.txt

We can count more than one file at a time:

wc -l file.txt file2.txt

It will show us the count of each file and the total.