Tutorial3: Advanced File Management / Quoting Special Characters

From CDOT Wiki
Revision as of 16:19, 15 January 2020 by Msaul (talk | contribs) (LINUX PRACTICE QUESTIONS)
Jump to: navigation, search

ADVANCED UNIX / LINUX FILE MANAGEMENT

Main Objectives of this Practice Tutorial

  • Understand the difference between absolute / relative / relative-to-home pathnames
  • Become productive using various pathname types for Unix/Linux File Management
  • Understand the following Ambiguous Filename expansion (FNE) Symbols: * / ? / [ ] / [! ]
  • Become productive using FNE for Unix/Linux File Management
  • Understanding quotation symbols: Backslash \ , single quotes ' ' , double quotes " "
  • Understand the purpose of quoting special characters for File Management and issuing Linux commands


Tutorial Reference Material

Course Notes
Pathname Type / Filename Expansion / Quoting Reference
YouTube Videos
Course Notes:


Pathname Types

Filename Expansion Symbols

  • Asterisk * , Question ? ,
    Character Class [ ] and [! ]


Quotation Symbols Instructional Videos:

Pathname Types

A pathname is a fully-specified location of a unique filename within the file system.

The concept of a pathname relates to every operating system including Unix, Linux, MS-DOS, MS-Windows, Apple-Macintosh, etc.


It is important to understand file pathnames since you need to save or access a file without ambiguity because there may be several files by that name in various directories.

For example: Accessing the cars.txt file - which file to access? It depends on the location of the file

/home/userid/uli101/cars.txt
/public/uli101/samples/cars.txt
/etc/data/cars.txt


Absolute Pathnames

Directories in red display the FULL path from the root directory to the bin directory (i.e. the absolute pathname: /bin.
Directories in red display the FULL path from the root directory to the examples directory (i.e. the absolute pathname: /home/userid/uli101/examples.

An absolute pathname is a path to the file or directory always beginning from the root directory (i.e. / ).


This type of pathname is referred to as absolute because the pathname always begins from the root directory, regardless the location or your current directory. In other words, this type of pathname requires that you always provide the FULL pathname starting with the root directory.


Advantages of using Absolute Pathnames:

  • Useful if you do not know your current directory location
  • Easier to understand the FULL layout of pathname


Example:
mkdir /home/userid/uli101 will create the uli101 directory in the home directory of the user called: userid. This command is using an absolute pathname.

Relative Pathnames

Directories in red display the path from the current directory location (which is xyz100) to the bin directory (i.e. the relative pathname: ../../../bin.
Directories in red display the path from the current directory location (which is uli101) to the examples directory (i.e. the relative pathname: uli101/examples or ./uli101/examples

A relative pathname is a path to a file or directory that begins from your current directory. This is called relative because it is used to locate a specific file relative to your current directory.

NOTE: In order to use relative pathnames, it is absolutely necessary that you know the location of your current directory!


Relative Pathname Symbols:

. A period symbol "." represents the current directory
.. Two consecutive period symbols ".." represents the parent directory (one level up)


Advantages of using Relative Pathnames:

  • Possible shorter pathname (less typing)


For example: mkdir uli101 or mkdir ./uli101 will create the uli101 directory in your current directory.

Relative-to-home Pathnames

Directories in red display the path from the home directory of the current user (which is userid) to the examples directory (i.e. the relative-to-home pathname: ~/uli101/examples.
Directories in red display the path from the current directory location (which is uli101) to the examples directory (i.e. the relative pathname: uli101/examples or ./uli101/examples

You can specify a pathname as relative-to-home by using a tilde and slash at the start, e.g. ~/uli101/notes.html

The tilde character ~' is replaced by your home directory (typically /home/current-user-id)

You can immediately place a username after the tilde to represent another user’s home directory. For example: ~jane = /home/jane


Advantages of using Relative-to-home Pathnames:

  • Possible shorter pathname


Examples:

mkdir ~uli101 will create the uli101 directory in current user's home directory.
ls ~jane will display contents of jane's home directory (/home/jane).


NOTE: Deciding which type of pathname to use depends on many factors including: knowledge of current directory, knowledge of directory structure, currently directory location, and type of file management command that is being used.

Filename Expansion

Sometimes when issuing Linux commands, it could be more efficient to use a trick to process several files that share the same characteristic
(for example, the same extension, or the same file naming structure).


Examples:

You issued the ls command to view all of the files contained in your current directory:


ls
a.txt b.txt c.txt 1.txt 2.txt 3.txt abc.txt work.txt webpage.html picture.png


You now want to list just text files (i.e. files with the extension ".txt").
One method to do this is to issue the ls command and specify each file pathname as a separate argument:


ls a.txt b.txt c.txt 1.txt 2.txt 3.txt abc.txt work.txt
a.txt b.txt c.txt 1.txt 2.txt 3.txt abc.txt work.txt


But this method is inefficient: it requires a LOT of typing and knowing which filenames to include as separate arguments.
You can use a special character to indicate to the Bash shell to match all files that end with the extension ".txt":

Globbing is the process of expanding filenames as separate arguments that match an argument that uses a wildcard symbol (such as *).

ls *.txt
a.txt b.txt c.txt 1.txt 2.txt 3.txt abc.txt work.txt

As you can see, the last Linux command you issued requires the least amount of keystrokes (more efficient).


How Does this Work? (Process of Globbing)


File globbing is a feature provided by the UNIX/Linux shell to represent multiple filenames by using special characters called wildcards with a single file name. A wildcard is essentially a symbol which may be used to substitute for one or more characters. Therefore, we can use wildcards for generating the appropriate combination of file names as per our requirement.
Reference: https://www.linuxnix.com/10-file-globbing-examples-linux-unix/


As shown in the diagram on the right, when the ls command is issued with a filename expansion symbol (like *), the Bash shell searches for files that match the symbol (in this case all filenames that end with the extension ".txt") and expands the argument using the wildcards with those filenames as separate arguments. You do not see this happen in the shell, it is a process that occurs "behind the scenes".

Below are the most common Filename Expansion symbols and how they are used for filename expansion:

Filename Expansion SymbolPurpose
*Asterisk (*) to represent 0 or more characters
?Question mark (?) to represent exactly one character (any character)
[ ]Square brackets ([ ]) to represent and match for the
character enclosed within the square brackets.
It represents ONLY ONE character - it's like a
Question Mark (?) but with conditions or restrictions
[! ]Square brackets containing an exclamation mark immediately after
the open square bracket ([! ]) to represent and match and
OPPOSITE character for the character enclosed within the square brackets.

Quoting Special Characters

x

INVESTIGATION 1: ABSOLUTE / RELATIVE / RELATIVE-TO-HOME PATHNAMES

Dir12.png

The best way to learn about pathname types is to issue many Linux file management commands using each type (absolute, relative, relative-to-home) and see which ones (or combination) is the most efficient (requiring the least number of keystrokes).


Perform the Following Steps:

  1. Login your matrix account.

  2. Issue a command to confirm you are located in your home directory.

    Let's create the following directory structure under your home directory by issuing the mkdir command using only absolute pathnames.

    NOTE: The command you issue below will be VERY LONG... just keep typing and let the text continue of separate lines. When using these absolute pathnames, start each one from the root directory (/) and replace the text "youruserid" with your actual login id.

  3. Issue the following Linux command to create the directory structure displayed to the right using absolute pathnames:

    mkdir -p /home/youruserid/tutorial3/practice/commands /home/youruserid/tutorial3/practice/examples /home/youruserid/tutorial3/notes/lesson1 /home/youruserid/tutorial3/notes/lesson2

    Let's remove this directory structure, and issue the same command using a relative-to-home pathname.

  4. Issue the following Linux command (enter "y" at each prompt to remove ALL contents):
    rm -ri /home/youruserid/tutorial3

  5. Issue a command to confirm that the tutorial3 directory (and its contents) no longer exist. You should know how to do this.

    Let's recreate the same directory structure, but use a relative-to-home pathname. You usually generate the ~ character by Holding down SHIFT and press the button to the left of the number 1 above the text on your keyboard.

  6. Issue the following Linux command to create the same directory structure using relative-to-home pathnames:

    mkdir -p ~/tutorial3/practice/commands ~/tutorial3/practice/examples ~/tutorial3/notes/lesson1 ~/tutorial3/notes/lesson2

    Let's remove the tutorial3 directory and its contents and issue the mkdir command with relative pathnames to create the same directory structure.

  7. Issue the same command as you did in step 4 to remove tutorial3 and its contents safely.

  8. Issue a Linux command to confirm you removed the tutorial3 directory and its contents.

  9. Issue the following Linux command to create the same directory structure using relative pathnames:

    mkdir -p tutorial3/practice/commands tutorial3/practice/examples tutorial3/notes/lesson1 tutorial3/notes/lesson2

    QUESTION: Which command (pathname type) that you performed in steps 3 , 6 , and 9 required the LEAST number of keystrokes (i.e. characters)?

Dir13.png
You might start to think that issuing Linux file management commands are better using relative or relative-to-home pathnames instead of absolute pathnames, but this assumption may not always be correct.
Since the current directory location was your home directory, then it makes sense that using relative or relative-to-home pathnames are more efficient. On the other hand, what if we changed the location to a different directory? Then perhaps, using an absolute pathname would be preferable.
When performing the next series of steps, refer to the tree diagram on the right. It is HIGHLY RECOMMENDED to always refer to a tree diagram when issuing Linux using different pathnames. Learning to reference a tree diagram on a quiz, midterm or final exam can help to prevent errors and loss of marks!


Perform the Following Steps:

  1. # Make certain that your current directory is your home directory.

  2. Issue the following Linux command to run a checking script to ensure you created the CORRECT directory structure displayed as in the diagram:
    bash /home/murray.saul/scripts/week3-check-1

  3. If you encounter errors and re-run the checking script until you receive a congratulations message.

  4. Issue a Linux command to change to the examples directory in your recently-created directory structure.

  5. Issue a Linux command to confirm you are located in the examples directory.

  6. Remembering that we are located in the examples directory, issue the following Linux command using a relative pathname to display files in the /bin directory: ls ../../../../../bin

  7. Now issue the following Linux command using an absolute pathname: ls /bin

    Which type of pathname would be the best to use in this situation?

    NOTE: Although it would work, using the previous command using the relative-to-home pathname would work, but it would look weird. Try to issue the command yourself!

  8. Let's copy the file called ls which is contained in the /bin directory to your home directory by using the cp command. First, issue the following Linux command to copy the ls command from the /bin directory to your home directory using absolute pathnames: cp /bin/ls /home/youruserid

  9. Now let's issue the previous command using just relative pathname (remember, our current directory location is examples):
    cp ../../../../../bin/ls ../..

    TIP: For relative pathnames that move up multiple parent directories such as these, it is HIGHLY RECOMMENDED to view the tree diagram and check for the correct number of .. symbols. Students commonly make mistakes and lose marks on these type of questions!

  10. Let's issue the command using one absolute pathname and a relative pathname: cp /bin/ls ../..

  11. Let's issue the same command using one absolute pathname and a relative-to-home pathname: cp /bin/ls ~

    Which of the following file type combinations requires the LEAST number of keystrokes to copy the ls file to your home directory?

  12. Let's copy the ls file from the /bin directory to your current directory (i.e. examples): cp /bin/ls .

  13. x

    Let's run a checking script to make certain you performed the above tasks correctly.

  14. Make certain that your current directory is your home directory.

  15. Issue the following Linux command to run a checking script: bash /home/murray.saul/scripts/week3-check-2

  16. If you encounter errors and re-run the checking script until you receive a congratulations message, and proceed to the next INVESTIGATION.

INVESTIGATION 2: FILENAME EXPANSION

You will now get practice issuing Linux file management commands using filename expansion symbols.
We will be using the directory structure that was created in the previous INVESTIGATION.

Dir12.png
Perform the following steps:
  1. Issue a Linux command to move to the commands directory
    (i.e. under practice directory as shown in diagram to the right).

  2. Issue a Linux command to confirmed that you have moved to the commands directory.

  3. Issue a Linux command to create the following empty regular files in the commands directory:
abc.txt
def.text
hij.TxT
1a4.txt
123.TXT
456.txt
6u9.txt
ab2.html
1234.txt
abcdef.txt
abcde.txt

  1. Before proceeding, let's run a checking script to make certain that you created these regular files correctly:
    Make certain that your current directory is your home directory.

  2. Issue the following Linux command to run a checking script:
    bash /home/murray.saul/scripts/week3-check-3

  3. If you encounter errors and re-run the checking script until you receive a congratulations message, and continue in this section.

    NOTE: To become comfortable with using filename expansion symbols, an excellent practice method is to create multiple files (you have already done that), look at the Linux command using filename expansion symbols and view the listing of files in the current directory that match and then write down on a paper the expected output of commands that use filename expansion symbols. You can then run the command to see the results in order to check your work.

    Let's try this for each of the situation below:

  4. Issue the following Linux command: ls ???.txt
    Write down the expected output on paper, then issue the command to check your answer.

  5. Issue the following Linux command: ls ?????.txt
    Write down the expected output on paper, then issue the command to check your answer.

  6. Issue the following Linux command: ls ??????.txt
    Write down the expected output on paper, then issue the command to check your answer.

  7. Issue the following Linux command: ls [0-9].txt
    Write down the expected output on paper, then issue the command to check your answer. Did the command work? What does this teach you about the character class [ ] symbol?

  8. Issue the following Linux command: ls [0-9][0-9][0-9].txt
    Write down the expected output on paper, then issue the command to check your answer.

  9. Issue the following Linux command: ls [a-z][a-z][a-z].txt
    Write down the expected output on paper, then issue the command to check your answer.

  10. Issue the following Linux command (using character class with UPPERCASE letters): ls [A-Z][A-Z][A-Z].txt
    Write down the expected output on paper, then issue the command to check your answer.

  11. Issue the following Linux command (using character class using alpha-numeric characters): ls [a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9].txt
    Write down the expected output on paper, then issue the command to check your answer.

  12. Issue the following Linux command: ls *.txt
    Write down the expected output on paper, then issue the command to check your answer. Did ALL text files get listed? Why not?

  13. Issue the following Linux command: ls *.[tT][xX][tT]
    Write down the expected output on paper, then issue the command to check your answer.Did ALL text files get listed this time? If so, why?

  14. NOTE: We have just been using filename expansion symbols just with the ls command.
    Filename expansion symbols can be used for ANY Linux file management command (e.g. cat, more, less, cp, mv, rm, ls, etc.).

    Let's get some practice issuing these other Linux file management commands.

  15. Issue the following Linux command: file *.[tT][xX][tT]
    Write down the expected output on paper, then issue the command to check your answer.

  16. Change to the commands directory using an absolute pathname.

  17. Issue a Linux command to confirm that you are now in the commands directory.

  18. Issue the following Linux command: cp /bin/l.* .
    View the contents of the contents directory. What did this command do?

  19. Issue the following Linux command: rm *

    View the contents of the contents directory. What did this command do?

  20. Issue the following Linux command: cp /bin/l? .
    View the contents of the contents directory. What did this command do?

  21. Issue the following Linux command: rm l[!s]
    View the contents of the contents directory. What did this command do?

  22. Use a text editor (nano or vi) to create the file called ab in the commands directory that contains the line of text below,
    and then save editing changes to this file:
    This is file ab

  23. Use a text editor (nano or vi) to create the file called cd in the commands directory that contains the line of text below,
    and then save editing changes to this file:
    This is file cd

  24. Use a text editor (nano or vi) to create the file called ef in the commands directory that contains the line of text below,
    and then save editing changes to this file:
    This is file ef

  25. Issue the following Linux command: cat ??

    View the contents of the contents directory. What did this command do? Why does the output look strange?

  26. Issue the following Linux command: cat [!l][!s]

    View the contents of the contents directory. What did this command do? Does the output look better? If so, why?

  27. Before proceeding, let's run a checking script to make certain that you created these regular files correctly:
    Make certain that your current directory is your home directory.

  28. Issue the following Linux command to run a checking script:
    bash /home/murray.saul/scripts/week3-check-4

  29. If you encounter errors and re-run the checking script until you receive a congratulations message, and continue in the next INVESTIGATION.

INVESTIGATION 3: QUOTING SPECIAL CHARACTERS

x


Perform the Following Steps:
  1. X



LINUX PRACTICE QUESTIONS

The purpose of this section is to obtain extra practice to help with your assignment #1, quizzes, your midterm, and your final ezam.

Here is a link to the MS Word Document of ALL of the questions displayed below but with extra room to answer on the document to simulate a quiz:

https://ict.senecacollege.ca/~murray.saul/uli101/uli101_week3_practice.docx

Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).


Review Questions:


When answering Linux command questions on this side or the back side of this page, refer to the following Inverted Tree diagram. The uli101 directory is contained in your home directory. Assume that you just logged into your Matrix account. Directories are underlined.


Dir13.png





































  1. X
  2. X
  3. X
  4. X
  5. X
  6. X
  7. X
  8. X