Skip to main content

File system Globbing

File Globbing: Path Name Expansion

The Bash shell has a path name-matching capability historically called globbing, abbreviated from the "global command" file path expansion program of early UNIX. The Bash globbing feature, commonly called pattern matching or "wildcards", makes managing large numbers of files easier. Using meta-characters that "expand" to match file and path names being sought, commands perform on a focused set of files at once.

Pattern Matching

Globbing is a shell command-parsing operation that expands a wildcard pattern into a list of matching path names. Command-line meta-characters are replaced by the match list prior to command execution. Patterns, especially square-bracketed character classes, that do not return matches display the original pattern request as literal text. The following are common meta-characters and pattern classes.
Pattern Matches
* Any string of zero or more characters.
? Any single character.
~ The current user's home directory.
~username User username's home directory.
~+ The current working directory.
~- The previous working directory.
[abc...] Any one character in the enclosed class.
[!abc...] Any one character not in the enclosed class.
[^abc...] Any one character not in the enclosed class.
[[:alpha:]] Any alphabetic character.(1)
[[:lower:]] Any lower-case character.(1)
[[:upper:]] Any upper-case character.(1)
[[:alnum:]] Any alphabetic character or digit.(1)
[[:punct:]] Any printable character not a space or alphanumeric.(1)
[[:digit:]] Any digit, 0-9.(1)
[[:space:]] Any one whitespace character; may include tabs, newline, or carriage returns, and form feeds as well as space.(1)
Note (1)pre-set POSIX character class; adjusts for current locale.
A sample set of files is useful to demonstrate expansion.
[student@desktopX ~]$ mkdir glob; cd glob
[student@desktopX glob]$ touch alfa bravo charlie delta echo able baker cast dog easy
[student@desktopX glob]$ ls
able  alfa  baker  bravo  cast  charlie  delta  dog  easy  echo
[student@desktopX glob]$ 
First, simple pattern matches using * and ?.
[student@desktopX glob]$ ls a*
able  alfa
[student@desktopX glob]$ ls *a*
able  alfa  baker  bravo  cast  charlie  delta  easy
[student@desktopX glob]$ ls [ac]*
able  alfa  cast  charlie
[student@desktopX glob]$ ls ????
able  alfa  cast  easy  echo
[student@desktopX glob]$ ls ?????
baker  bravo  delta
[student@desktopX glob]$ 
 

Tilde and Brace Expansion

Tilde Expansion

The tilde character (~), when followed by a slash delimiter, matches the current user's home directory. When followed by a string of characters up to a slash, it will be interpreted as a username, if one matches. If no username matches, then an actual tilde followed by the string of characters will be returned.
[student@desktopX glob]$ ls ~/glob

Command Substitution

Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed with a beginning dollar sign and parenthesis, $(command), or with backticks, `command`. The form with backticks is older, and has two disadvantages: 1) it can be easy to visually confuse backticks with single quote marks, and 2) backticks cannot be nested inside backticks. The $(command) form can nest multiple command expansions inside each other.
[student@desktopX glob]$ echo Today is `date +%A`.
Today is Wednesday.
[student@desktopX glob]$ echo The time is $(date +%M) minutes past $(date +%l%p).
The time is 26 minutes past 11AM.
[student@desktopX glob]$ 
alfa baker bravo cast charlie delta dog easy echo [student@desktopX glob]$ echo ~/glob /home/student/glob [student@desktopX glob]$

Brace Expansion

Brace expansion is used to generate discretionary strings of characters. Braces contain a comma-separated list of strings, or a sequence expression. The result includes the text preceding or following the brace definition. Brace expansions may be nested, one inside another.
[student@desktopX glob]$ echo {Sunday,Monday,Tuesday,Wednesday}.log Sunday.log Monday.log Tuesday.log Wednesday.log [student@desktopX glob]$ echo file{1..3}.txt file1.txt file2.txt file3.txt [student@desktopX glob]$ echo file{a..c}.txt filea.txt fileb.txt filec.txt [student@desktopX glob]$ echo file{a,b}{1,2}.txt filea1.txt filea2.txt fileb1.txt fileb2.txt [student@desktopX glob]$ echo file{a{1,2},b,c}.txt filea1.txt filea2.txt fileb.txt filec.txt [student@desktopX glob]$
 

Protecting Arguments from Expansion

Many characters have special meaning in the Bash shell. To ignore meta-character special meanings, quoting and escaping are used to protect them from shell expansion. The backslash (\) is an escape character in Bash, protecting the single following character from special interpretation. To protect longer character strings, single (') or double quotes (") are used to enclose strings.
Use double quotation marks to suppress globbing and shell expansion, but still allow command and variable substitution. Variable substitution is conceptually identical to command substitution, but may use optional brace syntax.
[student@desktopX glob]$ host=$(hostname -s); echo $host
desktopX
[student@desktopX glob]$ echo "***** hostname is ${host} *****"
***** hostname is desktopX *****
[student@desktopX glob]$ echo Your username variable is \$USER.
Your username variable is $USER.
[student@desktopX glob]$ 
Use single quotation marks to interpret all text literally. Observe the difference, on both screen and keyboard, between the single quote (') and the command substitution backtick (`). Besides suppressing globbing and shell expansion, quotations direct the shell to additionally suppress command and variable substitution. The question mark is a meta-character that also needed protection from expansion.
[student@desktopX glob]$ echo "Will variable $host evaluate to $(hostname -s)?"
Will variable desktopX evaluate to desktopX?
[student@desktopX glob]$ echo 'Will variable $host evaluate to $(hostname -s)?'
Will variable $host evaluate to $(hostname -s)?
[student@desktopX glob]$ 
 
 

Comments

Popular posts from this blog

Special Permissions in linux

The setuid permission on an executable file means that the command will run as the user owning the file, not as the user that ran the command. One example is the passwd command: [student@desktopX ~]$ ls -l /usr/bin/passwd -rw s r-xr-x. 1 root root 35504 Jul 16 2010 /usr/bin/passwd In a long listing, you can spot the setuid permissions by a lowercase s where you would normally expect the x (owner execute permissions) to be. If the owner does not have execute permissions, this will be replaced by an uppercase S . The special permission setgid on a directory means that files created in the directory will inherit their group ownership from the directory, rather than inheriting it from the creating user. This is commonly used on group collaborative directories to automatically change a file from the default private group to the shared group, or if files in a directory should be

The Seven-Step Model of Migration

Irrespective of the migration approach adopted, the Seven-step Model of Cloud Migration creates a more rational point of view towards the migration process and offers the ability to imbibe several best practices throughout the journey Step 1: Assess Cloud migration assessments are conducted to understand the complexities in the migration process at the code, design and architectural levels. The investment and the recurring costs are also evaluated along with gauging the tools, test cases, functionalities and other features related to the configuration. Step 2: Isolate The applications to be migrated to the cloud from the internal data center are freed of dependencies pertaining to the environment and the existing system. This step cuts a clearer picture about the complexity of the migration process. Step 3: Map Most organisations hold a detailed mapping of their environment with all the systems and applications. This information can be used to distinguish between the

RequestsDependencyWarning: urllib3 (1.24.1) or chardet (3.0.4) doesn't match a supported version

import tweepy /usr/lib/python2.7/dist-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.24.1) or chardet (3.0.4) doesn't match a supported version!   RequestsDependencyWarning) Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/usr/local/lib/python2.7/dist-packages/tweepy/__init__.py", line 14, in <module>     from tweepy.api import API   File "/usr/local/lib/python2.7/dist-packages/tweepy/api.py", line 12, in <module>     from tweepy.binder import bind_api   File "/usr/local/lib/python2.7/dist-packages/tweepy/binder.py", line 11, in <module>     import requests   File "/usr/lib/python2.7/dist-packages/requests/__init__.py", line 97, in <module>     from . import utils   File "/usr/lib/python2.7/dist-packages/requests/utils.py", line 26, in <module>     from ._internal_utils import to_native_string   File "/usr/lib/python2.

tag