linux_course_doc/certificates/essentials/lpi_essentials_lesson_02.md

10 KiB
Raw Permalink Blame History

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
  2. Find what type the following commands are: (extenal commands or builtin)
    • pwd
    • mv
    • cd
    • cat
    • exit
  3. Resolve the following commands that use quotes:
    • echo "$HOME is my home directory"
    • touch "$USER"
    • touch 'touch'

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, ...).
  2. 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).
  3. 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.
  2. Create an environment variable ORDER, using one of the two above methods.
  3. Display both the variable names and their contents.
  4. 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.
  2. Create an environment variable ME. Assign the USERNAME variables value to it.
  3. Append the HOME variables value to ME, having the : delimiter. Display the contents of the ME variable.
  4. Using the date example above, create a variable called today and assign the date for one of the time zones.
  5. Create another variable called today1 and assign the systems date to it.

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
  2. Open the ls info page and identify the MENU.
    • What options do you have?
    • Find the option that allows you to sort the output by modification time.
  3. Display the path to the first 3 README files. Use the man command to identify the correct option for locate.
  4. Create a file called test in your home directory. Find its absolute path with the locate command.
  5. Did you find it immediately? What did you have to do in order for locate to find it?
  6. Search for the test file that you previously created, using the find command. What syntax did you use and what is the absolute path ?

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?

  2. 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.

    User
    ├── Documents
    |   ├──Hello
    |   |   ├──hey2
    |   |   ├──helloa
    |   |   └──ola5
    |   └──World
    |       └──earth9
    ├── Downloads
    |   ├──Music
    |   └──Songs
    |       ├──collection1
    |       └──collection2
    ├── Test
    |   └── passa
    └── test
    
  3. Display on the screen the present working directory, including the subfolders.

  4. Search within the tree for all files that end with a number.

  5. 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
    • /
  2. 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:

    $ 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
    
    • A user enters the following commands:
    $ cd /etc/udev
    $ ls -a
    
    • What will be the output of the ls -a command?
  3. 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/.
  4. Consider the following commands:

    $ pwd
    /etc/udev/rules.d
    $ cd ../../systemd/user
    $ cd ..
    $ pwd
    
    • What is the output of the final pwd command?

Explorational Exercises

  1. Suppose a user has entered the following commands:

    $ mkdir "this is a test"
    $ ls
    this is a test
    
    • What cd command would allow you to enter this directory?
  2. 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.
  3. Try to create a directory whose name contains a \ character. Display the directorys name with ls and delete the directory.

Lesson 2.3.2

Guided Exercises

  1. Use the following file structure to answer the following three questions:

    /
    ├── etc/
    │   ├── network/
    │   │   └── interfaces/
    │   ├── systemd/
    │   │   ├── resolved.conf
    │   │   ├── system/
    │   │   ├── system.conf
    │   │   ├── user/
    │   │   └── user.conf
    │   └── udev/
    │       ├── rules.d
    │       └── udev.conf
    └── home/
        ├── lost+found/
        ├── user/
        │   └── Documents/
        └── michael/
            └── Music/
    
    • 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.
    • What command can user enter to navigate into michael 's Music directory? Use the shortest possible path.
  2. 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.

    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 */.

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?
  2. Here is a new command to try: du -h. Run this command and describe the output that it gives you.
  3. 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?

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.

    $ pwd
    /tmp
    $ find
    .
    ./outfiles
    ./outfiles/text
    
    • /tmp
    • /tmp/outfiles
    • /tmp/outfiles/text
    • /tmp/outfiles/text/today
    • /tmp/infiles
    • /tmp/infiles/text
    • /tmp/infiles/text/today
  2. What does -v do for mkdir, rm, and cp?

  3. 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?

  4. What happens when you use mv to move a directory into itself?

  5. How would you delete all files in your current directory that start with old?

  6. 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
  7. 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.
  2. What does the rmdir -p command do? Experiment with it and explain how it differs from rm -r.
  3. DO NOT ACTUALLY EXECUTE THIS COMMAND: What do you think rm -ri /* will do? (HONESTLY, DO NOT ATTEMPT TO DO THIS!)
  4. Other than using -i, is it possible to prevent mv from overwriting destination files?
  5. Explain the command cp -u.