linux_course_doc/certificates/essentials/lpi_essentials_lesson_02.md

286 lines
10 KiB
Markdown
Raw Permalink Normal View History

2021-04-10 14:04:47 +02:00
# Lesson 2.1.1
## Guided Exercises
1. Split the lines below into the components of command, option(s)/parameter(s) and argument(s):
* `cat -n /etc/passwd`
* `ls -l /etc`
* `ls -l -a`
* `cd /home/user`
2021-04-10 14:04:47 +02:00
1. Find what type the following commands are: (extenal commands or builtin)
* `pwd`
* `mv`
* `cd`
* `cat`
* `exit`
2021-04-10 14:04:47 +02:00
1. Resolve the following commands that use quotes:
* `echo "$HOME is my home directory"`
* `touch "$USER"`
* `touch 'touch'`
2021-04-10 14:04:47 +02:00
## Explorational Exercises
1. With one command and using brace expansion in Bash (review the man page for Bash), create 5 files numbered 1 to 5 with the prefix game (game1, game2, ...).
1. Delete all 5 files that you just created with just one command, using a different special character (review Pathname Expansion in the Bash man pages).
1. Is there any other way to make two commands interact with each other? What are those?
# Lesson 2.1.2
## Guided Exercises
1. Create a local variable `number`.
1. Create an environment variable `ORDER`, using one of the two above methods.
2021-04-10 14:04:47 +02:00
1. Display both the variable names and their contents.
1. What are the scopes of the previously created variables?
## Explorational Exercises
1. Create a local variable `nr_files` and assign the number of lines found in the `/etc/passwd` file. Hint: Look into the command `wc` and command substitution and dont forget about quotation marks.
1. Create an environment variable `ME`. Assign the `USERNAME` variables value to it.
1. Append the `HOME` variables value to `ME`, having the : delimiter. Display the contents of the `ME` variable.
1. Using the date example above, create a variable called `today` and assign the date for one of the time zones.
1. Create another variable called `today1` and assign the systems date to it.
2021-04-10 14:04:47 +02:00
# Lesson 2.2
## Guided Exercises
1. Use the `man` command to find out what each command does:
* `ls`
* `cat`
* `cut`
* `cd`
* `cp`
* `mv`
* `mkdir`
* `touch`
* `wc`
* `passwd`
* `rm`
* `rmdir`
* `more`
* `less`
* `whereis`
* `head`
* `tail`
* `sort`
* `tr`
* `chmod`
* `grep`
1. Open the `ls` info page and identify the MENU.
2021-04-10 14:04:47 +02:00
* What options do you have?
* Find the option that allows you to sort the output by modification time.
1. Display the path to the first 3 README files. Use the `man` command to identify the correct option for locate.
1. Create a file called test in your home directory. Find its absolute path with the `locate` command.
2021-04-10 14:04:47 +02:00
1. Did you find it immediately? What did you have to do in order for locate to find it?
1. Search for the test file that you previously created, using the `find` command. What syntax did you use and what is the absolute path ?
2021-04-10 14:04:47 +02:00
## Explorational Exercises
1. There is one command in the table above that doesnt have a man page. Which one is it and why do you think that the command doesnt have a man page?
1. Using the commands in the table above, create the following file tree. The names that start with a capital are Directories and the ones in lower case are files.
2021-04-10 14:18:32 +02:00
2021-04-10 14:15:38 +02:00
```
User
├── Documents
| ├──Hello
| | ├──hey2
| | ├──helloa
| | └──ola5
| └──World
| └──earth9
├── Downloads
| ├──Music
| └──Songs
| ├──collection1
| └──collection2
├── Test
| └── passa
└── test
```
2021-04-10 14:04:47 +02:00
1. Display on the screen the present working directory, including the subfolders.
1. Search within the tree for all files that end with a number.
1. Remove the entire directory tree with a single command.
# Lesson 2.3.1
## Guided Exercises
1. For each of the following paths, identify whether it is absolute or relative:
* /home/user/Downloads
* ../Reports
* /var
* docs
* /
1. Observe the following file structure. Note: Directories end with a slash (/) when tree is invoked with the -F option. You will need elevated privileges in order to run the tree command on the root (/) directory. The following is example output and is not indicative of a full directory structure. Use it to answer the following questions:
2021-04-10 14:18:32 +02:00
2021-04-10 14:15:38 +02:00
```
$ sudo tree -F /
/
├── etc/
│ ├── network/
│ │ └── interfaces
│ ├── systemd/
│ │ ├── resolved.conf
│ │ ├── system/
│ │ ├── system.conf
│ │ ├── user/
│ │ └── user.conf
│ └── udev/
│ ├── rules.d/
│ └── udev.conf
└── home/
├── lost+found/
└── user/
└── Documents/
12 directories, 5 files
```
2021-04-10 14:04:47 +02:00
* A user enters the following commands:
2021-04-10 14:18:32 +02:00
2021-04-10 14:15:38 +02:00
```
$ cd /etc/udev
$ ls -a
```
* What will be the output of the `ls -a` command?
2021-04-10 14:04:47 +02:00
1. Enter the shortest possible command for each of the following:
* Your current location is root (/). Enter the command to navigate to lost+found within the home directory (example):
* Your current location is root (/). Enter the command to navigate to the directory named /etc/network/.
* Your current location is /home/user/Documents/. Navigate to the directory named /etc/.
* Your current location is /etc/systemd/system/. Navigate to the directory named /home/user/.
1. Consider the following commands:
2021-04-10 14:18:32 +02:00
2021-04-10 14:15:38 +02:00
```
$ pwd
/etc/udev/rules.d
$ cd ../../systemd/user
$ cd ..
$ pwd
```
* What is the output of the final `pwd` command?
2021-04-10 14:04:47 +02:00
## Explorational Exercises
1. Suppose a user has entered the following commands:
2021-04-10 14:18:32 +02:00
2021-04-10 14:15:38 +02:00
```
$ mkdir "this is a test"
$ ls
this is a test
```
2021-04-10 14:04:47 +02:00
* What cd command would allow you to enter this directory?
1. Try this again, but after typing in cd this, press the TAB key. What is now displayed on the prompt?
* This is an example of autocompletion, which is an invaluable tool not only for saving time, but for preventing spelling errors.
1. Try to create a directory whose name contains a `\` character. Display the directorys name with ls and delete the directory.
2021-04-10 14:04:47 +02:00
# Lesson 2.3.2
## Guided Exercises
1. Use the following file structure to answer the following three questions:
2021-04-10 14:18:32 +02:00
2021-04-10 14:15:38 +02:00
```
/
├── etc/
│ ├── network/
│ │ └── interfaces/
│ ├── systemd/
│ │ ├── resolved.conf
│ │ ├── system/
│ │ ├── system.conf
│ │ ├── user/
│ │ └── user.conf
│ └── udev/
│ ├── rules.d
│ └── udev.conf
└── home/
├── lost+found/
├── user/
│ └── Documents/
└── michael/
└── Music/
```
2021-04-10 14:04:47 +02:00
* What command will navigate into the network directory regardless of your current location?
* What command can user enter to navigate into their Documents directory from `/etc/udev`? Use the shortest possible path.
2021-04-10 14:04:47 +02:00
* What command can user enter to navigate into michael 's Music directory? Use the shortest possible path.
1. Consider the following output of `ls -lh` to answer the next two questions. Note that directories are indicated with a `d` at the beginning of the line.
2021-04-10 14:18:32 +02:00
2021-04-10 14:15:38 +02:00
```
drwxrwxrwx 5 eric eric 4.0K Apr 26 2011 China/
-rwxrwxrwx 1 eric eric 1.5M Jul 18 2011 img_0066.jpg
-rwxrwxrwx 1 eric eric 1.5M Jul 18 2011 img_0067.jpg
-rwxrwxrwx 1 eric eric 1.6M Jul 18 2011 img_0074.jpg
-rwxrwxrwx 1 eric eric 1.8M Jul 18 2011 img_0075.jpg
-rwxrwxrwx 1 eric eric 46K Jul 18 2011 scary.jpg
-rwxrwxrwx 1 eric eric 469K Jan 29 2018 Screenshot from 2017-08-13 21-22-24.png
-rwxrwxrwx 1 eric eric 498K Jan 29 2018 Screenshot from 2017-08-14 21-18-07.png
-rwxrwxrwx 1 eric eric 211K Jan 29 2018 Screenshot from 2018-01-06 23-29-30.png
-rwxrwxrwx 1 eric eric 150K Jul 18 2011 tobermory.jpg
drwxrwxrwx 6 eric eric 4.0K Apr 26 2011 Tokyo/
-rwxrwxrwx 1 eric eric 1.4M Jul 18 2011 Toronto 081.jpg
-rwxrwxrwx 1 eric eric 1.4M Jul 18 2011 Toronto 085.jpg
-rwxrwxrwx 1 eric eric 944K Jul 18 2011 Toronto 152.jpg
-rwxrwxrwx 1 eric eric 728K Jul 18 2011 Toronto 173.jpg
drwxrwxrwx 2 eric eric 4.0K Jun 5 2016 Wallpapers/
```
* When you run the command `ls -lrS`, what file will be at the beginning?
* Please describe what you expect to see as the output for `ls -ad */`.
2021-04-10 14:04:47 +02:00
## Explorational Exercises
1. Run the `ls -lh` command in a directory that contains subdirectories. Note the listed size of these directories. Do these file sizes seem correct to you? Do they accurately represent the contents of all files inside that directory?
1. Here is a new command to try: `du -h`. Run this command and describe the output that it gives you.
1. On many Linux systems, you can type in `ll` and get the same output as you would if you typed `ls -l`. Please note however that `ll` is not a command. For example, `man ll` will give you the message that no manual entry exists for it. This is an example of an `alias`. Why might aliases be useful to a user?
2021-04-10 14:04:47 +02:00
# Lesson 2.4
## Guided Exercises
1. Given the following, select the directories that would be created by the command `mkdir -p /tmp/outfiles/text/today /tmp/infiles/text/today`.
2021-04-10 14:18:32 +02:00
2021-04-10 14:15:38 +02:00
```
$ pwd
/tmp
$ find
.
./outfiles
./outfiles/text
```
2021-04-10 14:04:47 +02:00
* /tmp
* /tmp/outfiles
* /tmp/outfiles/text
* /tmp/outfiles/text/today
* /tmp/infiles
* /tmp/infiles/text
* /tmp/infiles/text/today
1. What does `-v` do for `mkdir`, `rm`, and `cp`?
2021-04-10 14:04:47 +02:00
1. What happens if you accidentally attempt to copy three files on the same command line to a file that already exists instead of a directory?
1. What happens when you use `mv` to move a directory into itself?
2021-04-10 14:04:47 +02:00
1. How would you delete all files in your current directory that start with old?
1. Which of the following files would `log_[a-z]_201?_*_01.txt` match?
* `log\_3\_2017_Jan_01.txt`
* `log_+_2017_Feb_01.txt`
* `log_b_2007_Mar_01.txt`
* `log_f_201A_Wednesday_01.txt`
2021-04-10 14:04:47 +02:00
1. Create a few globs to match the following list of file names:
* doc100
* doc200
* doc301
* doc401
## Explorational Exercises
1. Use the `cp` man page to find out how to make a copy of a file and have the permissions and modification time match the original.
1. What does the `rmdir -p` command do? Experiment with it and explain how it differs from `rm -r`.
1. **DO NOT ACTUALLY EXECUTE THIS COMMAND:** What do you think `rm -ri /*` will do? **(HONESTLY, DO NOT ATTEMPT TO DO THIS!)**
1. Other than using `-i`, is it possible to prevent `mv` from overwriting destination files?
1. Explain the command `cp -u`.