[sf-lug] path name expansion

Michael Paoli Michael.Paoli at cal.berkeley.edu
Fri Aug 19 04:05:30 PDT 2016


bash(1):

Brace expansion is a mechanism by which arbitrary strings may be gener-
ated.  This mechanism is similar to pathname expansion, but  the  file-
names generated need not exist.

After word splitting, unless the -f option has  been  set,  bash  scans
each  word  for the characters *, ?, and [.  If one of these characters
appears, then the word is regarded as a pattern, and replaced  with  an
alphabetically  sorted list of filenames matching the pattern (see Pat-
tern Matching below).  If no matching  filenames  are  found,  and  the
shell  option  nullglob is not enabled, the word is left unchanged.

Note specifically, "If no matching filenames are found" ...
"the word is left unchanged".

So, in your example,
Dir*/file{1..3}
is equivalent to
Dir*/file1 Dir*/file2 Dir*/file3
but since those don't exist, they're left unchanged,
and hence the touch command fails, for lack of a
literal Dir* directory (or sym link that resolves
to a directory).
Among other things, you could do something like:
$ (for d in Dir*/; do touch "$d"file{1..3}; done)
e.g.:
$ ls
$ mkdir DirA DirB DirC
$ (for d in Dir*/; do touch "$d"file{1..3}; done)
$ ls -1d */*
DirA/file1
DirA/file2
DirA/file3
DirB/file1
DirB/file2
DirB/file3
DirC/file1
DirC/file2
DirC/file3
$
Or you could do something like:
$ touch Dir{A..C}/file{1..3}
e.g.:
$ ls
$ mkdir Dir{A..C}
$ touch Dir{A..C}/file{1..3}
$ ls -1d */*
DirA/file1
DirA/file2
DirA/file3
DirB/file1
DirB/file2
DirB/file3
DirC/file1
DirC/file2
DirC/file3
$

And no brace expansion in dash(1) - you're probably using bash(1).
What do these show you?:
$ ls -l /proc/$$/exe
$ set | grep '^BASH_VERSION='

And "default" shell?  Default in what context?
Default shell if not specified when creating account?
Default shell on user account(s) on live DVD, or when created with  
default install?
Or default shell if the shell field of /etc/passwd is null?
All three of those defaults may be distinct - and there are other
contexts for what is the default shell - e.g. file readable and executable,
but not binary, and lacking #! notation on first line - what interpreter will
be used on the file by default if one attempts to execute the file?  That may
also depend upon the interpreter under which one attempts to execute the file.

RTFM - Read The Fine Manual.  :-)

> From: "Alex Kleider" <akleider at sonic.net>
> Subject: [sf-lug] path name expansion
> Date: Thu, 18 Aug 2016 21:11:11 -0700

> I've been reading The Linux Command Line
> Third Internet Edition
> William Shotts
>
> and while playing around with and modifying examples have discovered  
> the following:
>
> alex at X301n3:~/Test$ rm -r *
> alex at X301n3:~/Test$ mkdir Dir{A..C}
> alex at X301n3:~/Test$ ls
> DirA  DirB  DirC
> alex at X301n3:~/Test$ touch Dir*/file{1..3}
> touch: cannot touch ‘Dir*/file1’: No such file or directory
> touch: cannot touch ‘Dir*/file2’: No such file or directory
> touch: cannot touch ‘Dir*/file3’: No such file or directory
> alex at X301n3:~/Test$ ls Dir*
> DirA:
>
> DirB:
>
> DirC:
> alex at X301n3:~/Test$ ls -d Dir*
> DirA  DirB  DirC
>
> From what I read, the touch Dir*/file{1..3] command should have  
> created three empty files in each of the three directories.
>
> Where have I misunderstood?
>
> Alex
>
> ps I'm running Ubuntu 14.04LTS (still) and the shell, I believe, is  
> dash, not bash.
> pps Sanity check:
> alex at X301n3:~/Test$ touch file{1..3}
> alex at X301n3:~/Test$ ls
> DirA  DirB  DirC  file1  file2  file3





More information about the sf-lug mailing list