CST8207: GNU/Linux Operating Systems I |
Lab Worksheet Four |
GLOB Patterns and Aliases |
This
Lab Worksheet contains some practical examples that will prepare you
to complete your Assignments.
You do not
have to hand in this Lab Worksheet. Make sure you complete the
separate Assignments on time. Quizzes and tests may refer to work
done in this Lab Worksheet; save your answers.
You must have an account on the Course Linux Server to do this lab. Log in to the server and use the shell. Review the Class Notes related to this worksheet as you work through it. Leave your work on the Linux server. Do not delete any work from the Linux server until the term is over and your marks are complete!
alias – (help alias) built-in bash command to create synonyms for command names.
bash – Shell aliases
bash – Shell wildcard (GLOB char) patterns: * ? [...] [!...]
sum – generate a checksum
unalias – remove an alias, or all aliases using -a
Log in to the Course Linux Server to do all commands in this lab. Set your bash PS1 shell prompt to show your login name, computer name, and the basename of your current directory, just as you did in the previous Lab. Leave your finished work on the server; do not delete it when you are finished the worksheet.
Use the echo command to see how patterns expand first, before you use them in a real command line, e.g.
echo /etc/pas* ; echo /dev/sd* ; echo /bin/*sh
* |
zero (0) or more characters |
? |
any one (1) character (including blanks, punctuation, and unprintable characters!) |
[aeAd] |
exactly one (1) character in the list between brackets: a, e, A, or d |
[!abc] |
exactly one (1) character that is not in the list of characters (sometimes written as [^abc]) |
[0-9] |
SEE WARNING! exactly one character in a range of characters determined by LOCALE |
[!0-9] |
SEE WARNING! exactly one character not in the LOCALE-dependent range of characters |
WARNING: DO NOT USE LETTER RANGES: Do not use letter ranges separated by a dash, e.g. [a-z], without first understanding your system LOCALE setting. The dash range [a-z] often includes the upper-case letters from B through Z and may not include A. Using digits [0-9] is the only safe range allowed.
Use
the echo
command to see how GLOB
patterns expand first,
before
you use them in a real command line:
$ echo /some/pattern* # echo the pattern on the screen before using the real command name!
$ rm /some/pattern* # now use the real command name with the verified pattern
As we do with most sections of each lab, we recursively remove and recreate a directory in which to do this section. This lets you return to this place and redo a section without having files left over from previous work.
[user@localhost ]$ cd ; rm -rf lab4.1 ; mkdir lab4.1 ; cd lab4.1
[user@localhost lab4.1]$ touch f1 f2 f3
[user@localhost lab4.1]$ touch f10 f20 f30
[user@localhost lab4.1]$ touch f11 f12 f13
[user@localhost lab4.1]$ touch f33 fffff
[user@localhost lab4.1]$ mkdir dir1 dir2 dir3
[user@localhost lab4.1]$ ls
dir1 dir2 dir3 f1 f10 f11 f12 f13 f2 f20 f3 f30 f33 fffff
[user@localhost lab4.1]$ ls | sum
33442 1 ( make sure you get this number, otherwise start over at the beginning of 4.1)
Use the echo command to see how patterns expand first, before you use them in a real command line.
[user@localhost
lab4.1]$ cp f? dir1
Which files have been copied to the directory dir1? (First try to answer without typing the command.)
_________________________________________________________________________
[user@localhost lab4.1]$ cp f?0 dir2
Which files have been copied to the directory dir2? (First try to answer without typing the command.)
_________________________________________________________________________
[user@localhost lab4.1]$ cp f?3 dir3
Which files have been copied to the directory dir3? (First try to answer without typing the command.)
_________________________________________________________________________
[user@localhost lab4.1]$ echo ? ?? ?????
One of the above three patterns fails to expand to any pathname. Which pattern, and why?
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
[user@localhost
lab4.1]$ cp
? ?? ????? dir3
Why does the above command line give an error message from the copy command?
________________________________________________________________________________________
________________________________________________________________________________________
[user@localhost
lab4.1]$ ls
/dev/tty*
The above
command shows all terminal devices configured in your Linux system.
Try it.
What command line, similar to the above, shows only
the five-character terminal device names,
i.e. shows
tty10 and tty63
and ttyS0, etc.
but does not show tty
or tty1
or tty9
etc.?
_________________________________________________________________________
This section depends
on the files created at the start of the previous section. Create
those files first.
[user@localhost lab4.1]$ cd ; rm -rf lab4.2 ; mkdir lab4.2 ; cd lab4.2
[user@localhost lab4.2]$ cp -a ../lab4.1/. . (note the single dot destination directory)
[user@localhost lab4.2]$ ls
dir1 dir2 dir3 f1 f10 f11 f12 f13 f2 f20 f3 f30 f33 fffff
[user@localhost lab4.2]$ ls | sum
33442 1 ( make sure you get this number, otherwise start over at the beginning of 4.1)
Use the echo command to see how patterns expand first, before you use them in a real command line.
[user@localhost lab4.2]$ rm dir?/* (clean out all files in these directories)
[user@localhost lab4.2]$ ls f*
What is the output of the last command, above? (First try to answer without typing the command.)
_______________________________________________________________________
[user@localhost lab4.2]$ ls -d d*
What is the output of the last command, above? (First try to answer without typing the command.)
_______________________________________________________________________
[user@localhost lab4.2]$ ls f*1 (that pattern contains a digit, not a letter)
What is the output of the last command, above? (First try to answer without typing the command.)
_______________________________________________________________________
[user@localhost lab4.2]$ ls -d *1 (that pattern contains a digit, not a letter)
What is the output of the last command, above? (First try to answer without typing the command.)
_______________________________________________________________________
[user@localhost lab4.2]$ ls -d *1* (that pattern contains a digit, not a letter)
What is the output of the last command, above? (First try to answer without typing the command.)
_______________________________________________________________________
Square brackets are shell metacharacters that indicate a GLOB pattern match for any one character in the list of characters between the square brackets. You can also match any one character that is not in the list by starting the list with an exclamation point !, e.g. [!abc] Circumflex is sometimes also allowed but doesn't work in all shells, so the exclamation point is preferred instead of using [^abc]
[user@localhost ]$ cd ; rm -rf lab4.3 ; mkdir lab4.3 ; cd lab4.3
[user@localhost lab4.3]$ touch hat help hit hot hut
[user@localhost lab4.3]$ ls | sum
07916 1 (make sure you get this number, otherwise start over at the beginning of 4.3)
Use the echo command to see how patterns expand first, before you use them in a real command line.
[user@localhost lab4.3]$ ls h[ao]t
What is the output of the last command, above? (First try to answer without typing the command.)
_______________________________________________________________________
[user@localhost lab4.3]$ ls h[aeiou]t
What is the output of the last command, above? (First try to answer without typing the command.)
_______________________________________________________________________
[user@localhost lab4.3]$ ls h[aeiou]*
Comparing with the previous output, which additional file is displayed? ___________________
Why? ______________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
[user@localhost lab4.3]$ cd ; rm -rf lab4.3b ; mkdir lab4.3b ; cd lab4.3b
[user@localhost lab4.3b]$ touch sda sdb sdc sdd
[user@localhost lab4.3b]$ touch sda1 sdb2 sdc3 sdd4
[user@localhost lab4.3b]$ ls | sum
61395 1 (make sure you get this number, otherwise start over at the beginning with rm above)
[user@localhost lab4.3b]$ ls sd[abc]
Which names are output by the last command, above? (First try to answer without typing the command.)
_______________________________________________________________________
[user@localhost lab4.3b]$ ls sda[1-4]
Which names are output by the last command, above? (First try to answer without typing the command.)
_______________________________________________________________________
[user@localhost lab4.3b]$ ls sd[bc][1-3]
Which names are output by the last command, above? (First try to answer without typing the command.)
_______________________________________________________________________
[user@localhost lab4.3b]$ echo sd[!ab][!12]
Which names are output by the last command, above? (First try to answer without typing the command.)
_______________________________________________________________________
List complements
sometimes work using a leading circumflex, as [^abc],
to mimic the behaviour of Regular Expressions, but the circumflex
doesn't work in all shells so [!abc]
is preferred in GLOB patterns.
DO NOT USE DASHED RANGES: Do not use character ranges separated by a dash, especially alphabetic ranges, without first understanding your system LOCALE setting. The range [a-z] often includes the upper-case letters from B through Z and may not include A. Using only digits in the range, e.g. [0-9], may be safe.
The following shows the difference on two example Linux systems, depending on the LOCALE environment:
$ touch a A b B y Y z Z
server$ echo [a-z] # Server gives only lower-case: a b y z
server$ echo [A-Z] # Server gives only upper-case: A B Y Z
desktop$ echo [a-z] # Desktop gives an incomplete mix: a B b Y y Z z
desktop$ echo [A-Z] # Desktop gives an incomplete mix: A a B b Y y Z
The Server uses a traditional English-only LOCALE that expects only ASCII (English) characters where the Desktop uses a modern International LOCALE (e.g. LC_CTYPE=en_CA.utf8) that also includes Unicode characters in the range. Do not use dashed character ranges, especially alphabetic ranges, without first understanding your system LOCALE setting. Verify GLOB patterns using echo ! For a fuller explanation on Internationalization problems, including the use of POSIX character classes such as [:lower:] and [:upper:] read:
http://teaching.idallen.com/cst8177/15w/notes/000_character_sets.html
Aliases are built-in to your current shell. They are not separate commands like date and ls. Aliases let you define your own command names. You can also redefine existing command names and/or add your favourite options to existing command names. Many Linux systems come with some shell aliases defined already. These predefined aliases may be set in system RC files (under /etc) or in a .bashrc file placed into your account HOME directory. Know what aliases are set in your account before you start typing!
[user@localhost ~]$ alias (list all current aliases - may be none)
In which man page are shell aliases documented? : ____________________________________
You can also use the help
feature of your shell to get help on any built-in command, e.g. help
alias
[user@localhost ~]$ alias myls='ls -alF --color=auto'
[user@localhost
~]$ alias
[user@localhost
~]$ ls
.. (two
dots - parent directory)
[user@localhost ~]$ myls .. (two dots - parent directory)
Describe how typing myls gives different output compared to typing ls:
________________________________________________________________________________________
________________________________________________________________________________________
[user@localhost ~]$ alias myls='ls -alF --color=auto'
[user@localhost ~]$ myls
[user@localhost ~]$ unalias myls
[user@localhost ~]$ alias
[user@localhost ~]$ myls
Record exactly the error message from using the undefined alias (undefined command name):
_______________________________________________________________________
Putting a backslash in front of a command name causes the shell to ignore any alias for that command.
[user@localhost ~]$ alias ls='ls -alF --color=auto'
[user@localhost ~]$ ls .. (two dots - parent directory)
[user@localhost ~]$ \ls .. (two dots - parent directory)
Describe how typing \ls gives different output compared to typing the aliased version of ls:
________________________________________________________________________________________
________________________________________________________________________________________
Given what you see above, what is the function of the backslash character \ in front of an alias?
________________________________________________________________________________________
Give a command to remove the ls alias: ________________________________________
What command will remove all current aliases (RTFM): _____________________________
Why doesn't man alias work? ____________________________________________________
How do mv and cp differ?
_________________________________________________________________________________
_________________________________________________________________________________
Describe the purpose of the whoami command (read the man page):
_________________________________________________________________________________
What is the purpose of the -i switch (option) to the ls command?
_________________________________________________________________________________
What is the purpose of the -d switch (option) to the ls command?
_________________________________________________________________________________
What command name and options copy an entire directory, including all the other files and sub-directories stored under it?
____________________________________________________________________
What command name do you use to delete/remove an empty directory?
____________________________________________________________________
What command name and options do you use to delete/remove a complete directory structure including all the other files and sub-directories stored under it?
____________________________________________________________________
What command name and options would find any pathname ending in the basename of passwd, found in any directory, starting from the ROOT directory?
____________________________________________________________________
What command name and options would find and print all the pathnames under the directory /etc?
____________________________________________________________________
What command would find and print all the pathnames owned by the root user under /var/spool:
____________________________________________________________________
How do you create a directory a/b/c/d/e including all the parent directories, in one command line?
____________________________________________________________________
Rewrite the pathname /usr/./bin/../local/./bin/../sbin as a simple absolute pathname, without using any “.” or “..”:
____________________________________________________________________
Page |
©2013 Algonquin College Shawn Unger, Todd Kelley, Ian Allen |
Version 18
|