57
votes
Accepted
cat a very large number of files together in correct order
Using find, sort and xargs:
find . -maxdepth 1 -type f -name 'file_*.pdb' -print0 |
sort -zV |
xargs -0 cat >all.pdb
The find command finds all relevant files, then prints their pathnames out to ...
44
votes
Accepted
What is the difference between `a[bc]d` (square brackets) and `a{b,c}d` (braces)?
The two are quite different.
a[bc]d is a filename pattern (in shells other than fish). It will expand to the two filenames abd and acd if those are names of existing files in the current directory.
...
31
votes
Accepted
Bash brace expansion after a path slash
The brace expansion syntax accepts commas, but it does not accept a space after the comma. In many programming languages, spaces after commas are commonplace, but not here. In Bash, the presence of an ...
31
votes
Accepted
Apply brace expansion in "reverse order"
You could do:
$ eval echo '{a..c}'{1..3}
a1 b1 c1 a2 b2 c2 a3 b3 c3
Which then tells the shell to evaluate:
echo {a..c}1 {a..c}2 {a..c}3
26
votes
Accepted
Why doesn't systemctl\ {restart,status}\ sshd\; work?
It is a form of Brace expansion done in the shell. The brace-expansion idea is right, but the way it was used is incorrect here. When you meant to do:
systemctl\ {restart,status}\ sshd\;
The shell ...
26
votes
Accepted
Avoiding non-zero exit code when running `ls` using multiple patterns
Most of your questions are already answered at Why is nullglob not default?.
One thing to bear in mind is that:
ls -d /some/{path1,path2}/*
In csh/tcsh/zsh/bash/ksh (but not fish, see below) is the ...
25
votes
Accepted
How do bash loops work precisely?
Ordinary for loops always iterate over a static set of strings. This is regardless of whether the strings are generated by brace expansions or by filename globbing patterns, or some other expansion of ...
24
votes
Bash brace expansion after a path slash
string{foo, bar} isn't brace expansion; it's the two words string{foo, and bar}. To use brace expansion, the braces need to be within the same word. You'll have to either remove the extra space if you ...
22
votes
Why does "cat {foo}" not output foo, but "cat {foo,bar}" does?
By definition, brace expansion in GNU Bash requires either a sequence expression or a series of comma-separated values:
Patterns to be brace expanded take the form of an optional preamble, followed ...
21
votes
Why does "cat {foo}" not output foo, but "cat {foo,bar}" does?
{subfolder1} evaluates to {subfolder1}, since there are no alternatives. Use subfolder1 instead.
19
votes
Accepted
Nested brace expansion mystery in Bash
Well, it is unravelled one layer at a time:
X{{a..c},{1..3}}Y
is documented as being expanded to X{a..c}Y X{1..3}Y (that's X{A,B}Y expanded to XA XB with A being {a..c} and B being {1..3}), ...
19
votes
Why doesn't systemctl\ {restart,status}\ sshd\; work?
This is called brace expansion (as the tag indicates).
What did I do wrong here? Why doesn't it work?
Consider the stages involved in reading and executing a command line in bash (for example):
...
19
votes
Accepted
Why does bash give the following result after brace expansion?
Because you are telling it to :) This is what bash sees:
file{[1,2],3}.txt
The [ and ] have no special meaning in this context, globs aren't expanded within brace expansions: if a brace expansion ...
18
votes
Accepted
Unwanted space in brace expansion
echo prints its arguments separated by spaces, even if they include (or generate) newline characters. Additionally it adds one newline character at the end of its output (unless it's echo -n).
Use ...
18
votes
Accepted
Bash expansion asymmetry when opening and creating files
What you are using are not regular expressions, but a combination of brace expansion and filename expansion (a.k.a globbing). That is important because while brace expansion simply expands the string ...
15
votes
Ignore "no matches" from zsh when using brace expansion with glob *.{a,b,}test
In
ls -d ./foldername/*.{a,b,}test
{a,b,...} is not a glob operator, that's brace expansion, that's first expanded to:
ls -d ./foldername/*.atest ./foldername/*.btest ./foldername/*.test
And each ...
14
votes
cat a very large number of files together in correct order
With zsh (where that {1..15000} operator comes from):
autoload zargs # best in ~/.zshrc
zargs file_{1..15000}.pdb -- cat > file_all.pdb
Or for all file_<digits>.pdb files in numerical order:
...
13
votes
Accepted
mkdir -p dir with braces created wrongly
Bash, like any POSIX shell, splits commands into tokens before expanding words (which, in Bash, includes brace expansion).
mkdir -p /root/backups/{db, dirs}
contains a space, so the command is first ...
13
votes
Accepted
How to use bash tricks to type out list of redundant strings which vary only in a couple characters?
For expanding filenames (or device nodes) that exist already, then filename globbing is usually what you want:
The first would expand to event1 to event4, the second to any and all eventXX that exist:...
13
votes
Prepend and append a string to each element of $* in shell
${var/pattern/replacement} is a ksh93 parameter expansion operator, also supported by zsh, mksh, and bash, though with variations (mksh's currently can't operate on arrays).
ksh93
In ksh93, you'd do ${...
12
votes
Accepted
Ignore "no matches" from zsh when using brace expansion with glob *.{a,b,}test
It may be best to do this with find:
find ./foldername -maxdepth 1 -name '*.atest' -o -name '*.btest' -o -name '*.test'
12
votes
cat a very large number of files together in correct order
A for loop is possible, and very simple.
for i in file_{1..15000}.pdb; do cat $i >> file_all.pdb; done
The downside is that you invoke cat a hell of a lot of times. But if you can't remember ...
12
votes
Accepted
Why are bash brace expansions not working for commands?
Your brace expansion is not valid. A brace expansion must be one word in the shell.
A word is a string delimited by unquoted spaces (or tabs or newlines, by default), and the string {chown httpd,...
11
votes
Accepted
Why in bash {{a,b}.{c,d}} expands to {a.c} {a.d} {b.c} {b.d}
In bash, the expansion of {word} is {word} when the word does not contain a range (..) or a comma.
In your case word happens to contain two brace expansions. These are expanded left to right, so you ...
10
votes
Accepted
How does curly brace expansion work in the shell?
They are called brace expansion.
It is one of several expansions done by bash, zsh and ksh, filename expansion *.txt being another one of them. Brace expansion is not covered by the POSIX standard ...
10
votes
Apply brace expansion in "reverse order"
For this particular case, I think that the option given by Stéphane Chazelas is the best one.
On the other hand, when you expand more complex things, this option doesn't scale well. So, you can ...
10
votes
Accepted
What does {A,B} mean in shell?
Brace Expansion:
https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html
In your particular example, the shell will expand the brace expansion /app/{extra,bin}/masterpdfeditor5 to the ...
10
votes
Accepted
rsync: How to exclude multiple file types?
First of all, your first example works - what's wrong with using that?
If you really don't want to do that, try --exclude=*.{jpg,mp4}, which will (in some shells) expand to --exclude=*.jpg --exclude=*....
10
votes
Store expanded array in a variable using a brace expansion
To assign to an array, put the elements in parentheses:
nodes=(node{1..3})
When using the array, you need to tell bash explicitly that you want to expand it as an array.
mycommand "${nodes[@]}&...
9
votes
Loop in macOS not working
Your script is written for zsh but you are executing it with bash.
bash does not support using variables as ranges in brace-expansions.
To resolve this, simply arrange for the script or function be ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
brace-expansion × 156bash × 117
shell × 25
zsh × 19
wildcards × 14
shell-script × 13
variable-substitution × 13
command-line × 11
array × 6
command-substitution × 6
linux × 5
string × 5
cp × 5
find × 4
variable × 4
files × 3
rsync × 3
io-redirection × 3
curl × 3
autocomplete × 3
cat × 3
for × 3
pattern-matching × 3
environment-variables × 2
macos × 2