Double Asterisk Brings Double Expedience To Pathname Expansion


If yous are a Linux command-line user, nigh likely, yous are familiar amongst the purpose of the unmarried asterisk ('*') inward pathname expansion (aka globbing). How the asterisk behaves is standardized across all shells (bash, zsh, tcsh, etc). For example, the ls * ascendancy lists the files too the immediate sub-directories of the electrical flow directory.


$ ls *

The unmarried asterisk, however, is non recursive: it does non traverse beyond the target directory. You may purpose the find ascendancy to generate a recursive listing of pathnames. H5N1 simpler solution is the purpose of the double asterisk ('**').

Unlike the unmarried asterisk, the double asterisk is non standardized. Different shells introduced the characteristic at dissimilar times amongst slightly dissimilar behavior. This post service focuses on the purpose of '**' for the bash shell.

The double asterisk characteristic for bash showtime appears amongst bash4. To uncovering out which bash version yous are running, execute the next command:


$ bash --version
GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)
...

Before yous purpose '**', yous must showtime enable the globstar compaction option:


$ shopt -s globstar

With globstar enabled, yous may purpose '**' for pathname expansion.


$ ls **/abc.txt

In the to a higher house example, the ls ascendancy returns whatsoever occurrence of the file abc.txt inward the electrical flow directory too sub-directories.

Notes:

  1. By default, the double asterisk does non expand to include a hidden file. For example, the next ascendancy volition non uncovering .htaccess because it is a hidden file.


    $ ls **/.htaccess

    To let hidden files inward '**' output, enable the dotglob compaction option:


    $ shopt -s dotglob

  2. When yous create a pathname expansion using '*' or '**', yous endure the gamble that a returned filename is the same every minute a command-line flag, e.g., -r. To mitigate that risk, precede '**' amongst '--' every minute below. The double dash marks the location where command-line flags end, too positional parameters begin.


    $ ls -- **

  3. Under bash, '**' expands to follow symbolic links. This behavior, however, is shell-specific. For zsh, expanding the double asterisk does non follow a symbolic link.

The double dash is a useful tool to add together to your everyday command-line usage.

Berlangganan update artikel terbaru via email:

0 Response to "Double Asterisk Brings Double Expedience To Pathname Expansion"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel