Diferencia entre revisiones de «Basic Administration»

De Wiki de Sistemas Operativos
Saltar a: navegación, buscar
(Página creada con «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 interpre...»)
 
(initial)
Línea 138: Línea 138:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The compact version can also be used when specifying
+
The compact version can also be used when specifying options:
 +
 
 +
<syntaxhighlight lang="bash">
 +
man -h
 +
</syntaxhighlight>
 +
 
 +
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.
 +
 
 +
<syntaxhighlight lang="bash">
 +
ls
 +
</syntaxhighlight>
 +
 
 +
If a path is specified, displays the folders and files contained in that path, for example:
 +
 
 +
<syntaxhighlight lang="bash">
 +
$ 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
 +
</syntaxhighlight>
 +
 
 +
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:
 +
 
 +
<syntaxhighlight lang="bash">
 +
$ ls -a
 +
 
 +
. ..  .bash_history .bash_logout .bashrc .bashrc .profile .ssh
 +
</syntaxhighlight>
 +
 
 +
Both options can be combined:
 +
 
 +
<syntaxhighlight lang="bash">
 +
$ 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
 +
[...]
 +
 
 +
</syntaxhighlight>
 +
 
 +
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.
 +
 
 +
<syntaxhighlight lang="bash">
 +
$ mkdir test
 +
</syntaxhighlight>
 +
 
 +
Creates a test folder in the current directory.
 +
 
 +
Let's check that it has been created properly:
 +
 
 +
<syntaxhighlight lang="bash">
 +
$ ls
 +
test
 +
</syntaxhighlight>
 +
 
 +
With the '''-p'''' option, it allows to create successive nested folders at once, for example:
 +
 
 +
<syntaxhighlight lang="bash">
 +
$ mkdir -p a/b/c
 +
</syntaxhighlight>
 +
 
 +
Which creates the following folder structure.
 +
 
 +
a
 +
└── b
 +
    └── c
 +
 
 +
== touch ==
 +
 
 +
Creates a new empty file (no contents) with the name we specify.
 +
 
 +
<syntaxhighlight lang="bash">
 +
$ touch file.txt
 +
</syntaxhighlight>
 +
 
 +
We check that the file has been created correctly.
 +
 
 +
<syntaxhighlight lang="bash">
 +
$ ls
 +
file.txt
 +
</syntaxhighlight>
 +
 
 +
== cp ==
 +
 
 +
Copies a file or directory.
 +
 
 +
<syntaxhighlight lang="bash">
 +
$ cp file.txt file_copy.txt
 +
</syntaxhighlight>
 +
 
 +
To copy a folder and its contents we have to use the -r (recursive) option.
 +
 
 +
<syntaxhighlight lang="bash">
 +
$ cp -r test test_copy
 +
</syntaxhighlight>
 +
 
 +
* We check the result with the '''ls''''' command.
 +
 
 +
* If we are in the destination folder, the structure would be as follows:
 +
 
 +
<syntaxhighlight lang="bash">
 +
$ cp <source path name> <file name>
 +
</syntaxhighlight>
 +
 
 +
== mv ==
 +
 
 +
Move file or folder.
 +
 
 +
<syntaxhighlight lang="bash">
 +
mv file_copy.txt file2.txt
 +
</syntaxhighlight>
 +
 
 +
It is also used for renaming.
 +
 
 +
<syntaxhighlight lang="bash">
 +
mv test_copy test_copy2
 +
</syntaxhighlight>
 +
 
 +
== rm ==
 +
 
 +
* Deletes an empty file or folder:
 +
<syntaxhighlight lang="bash">
 +
rm <name>
 +
</syntaxhighlight>
 +
 
 +
* To delete a non-empty folder and its contents:
 +
<syntaxhighlight lang="bash">
 +
rm -r <name>
 +
</syntaxhighlight>
 +
 
 +
'''IMPORTANT'''': Never do '''' rm -r * '''''
 +
 
 +
* EXERCISES:
 +
 
 +
# 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
 +
 
 +
# 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 =.

Revisión del 15:54 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 =.